1 | unit UTargetJava;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, UTarget, UBFTarget, Dialogs;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TTargetJava }
|
---|
13 |
|
---|
14 | TTargetJava = class(TBFTarget)
|
---|
15 | private
|
---|
16 | function GetMemoryCell: string;
|
---|
17 | public
|
---|
18 | constructor Create; override;
|
---|
19 | procedure Compile; override;
|
---|
20 | procedure Run; override;
|
---|
21 | end;
|
---|
22 |
|
---|
23 |
|
---|
24 | implementation
|
---|
25 |
|
---|
26 | { TTargetJava }
|
---|
27 |
|
---|
28 | constructor TTargetJava.Create;
|
---|
29 | begin
|
---|
30 | inherited;
|
---|
31 | FName := 'Java';
|
---|
32 | FSourceExtension := '.java';
|
---|
33 | FCompiledExtension := '.class';
|
---|
34 | FRunExtension := '';
|
---|
35 | FImageIndex := 24;
|
---|
36 | FCapabilities := [tcCompile, tcRun];
|
---|
37 | {$IFDEF WINDOWS}
|
---|
38 | CompilerPath := 'c:\Program Files\Java\bin\javac.exe';
|
---|
39 | ExecutorPath := 'c:\Program Files\Java\bin\java.exe';
|
---|
40 | {$ENDIF}
|
---|
41 | {$IFDEF UNIX}
|
---|
42 | CompilerPath := '/usr/bin/javac';
|
---|
43 | ExecutorPath := '/usr/bin/java';
|
---|
44 | {$ENDIF}
|
---|
45 | end;
|
---|
46 |
|
---|
47 | function TTargetJava.GetMemoryCell: string;
|
---|
48 | begin
|
---|
49 | Result := 'Memory[Pos';
|
---|
50 | if FProgram[FProgramIndex].RelIndex > 0 then
|
---|
51 | Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
|
---|
52 | else if FProgram[FProgramIndex].RelIndex < 0 then
|
---|
53 | Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
|
---|
54 | Result := Result + ']';
|
---|
55 | end;
|
---|
56 |
|
---|
57 | procedure TTargetJava.Compile;
|
---|
58 | begin
|
---|
59 | inherited;
|
---|
60 | Indent := 0;
|
---|
61 | FTargetCode := '';
|
---|
62 |
|
---|
63 | AddLine('import java.io.*;');
|
---|
64 | AddLine;
|
---|
65 | AddLine('public class ' + ProgramName + ' {');
|
---|
66 | Inc(Indent);
|
---|
67 | AddLine('public static void main(String args[]) throws IOException {');
|
---|
68 | Inc(Indent);
|
---|
69 | AddLine('char Memory[];');
|
---|
70 | AddLine('int Pos;');
|
---|
71 | AddLine('char ReadChar;');
|
---|
72 | AddLine;
|
---|
73 | AddLine('Memory = new char[' + IntToStr(MemorySize) + '];');
|
---|
74 | AddLine('Pos = 0;');
|
---|
75 | FProgramIndex := 0;
|
---|
76 | while FProgramIndex < FProgram.Count do begin
|
---|
77 | case FProgram[FProgramIndex].Command of
|
---|
78 | cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
79 | cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
80 | cmInc: AddLine(GetMemoryCell + ' = (char)((int)' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
|
---|
81 | cmDec: AddLine(GetMemoryCell + ' = (char)((int)' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
|
---|
82 | cmOutput: AddLine('System.out.print(' + GetMemoryCell + ');');
|
---|
83 | cmInput: AddLine(GetMemoryCell + ' = (char)System.in.read();');
|
---|
84 | cmSet: AddLine(GetMemoryCell + ' = (char)' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
85 | cmMultiply: AddLine(GetMemoryCell + ' = (char)((int)' + GetMemoryCell + ' + (int)Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
|
---|
86 | cmLoopStart: begin
|
---|
87 | AddLine('while(' + GetMemoryCell + ' != 0)');
|
---|
88 | AddLine('{');
|
---|
89 | Inc(Indent);
|
---|
90 | end;
|
---|
91 | cmLoopEnd: begin
|
---|
92 | Dec(Indent);
|
---|
93 | AddLine('}');
|
---|
94 | end;
|
---|
95 | end;
|
---|
96 | Inc(FProgramIndex);
|
---|
97 | end;
|
---|
98 | Dec(Indent);
|
---|
99 | AddLine('}');
|
---|
100 | Dec(Indent);
|
---|
101 | AddLine('}');
|
---|
102 |
|
---|
103 | CompileToFile;
|
---|
104 | end;
|
---|
105 |
|
---|
106 | procedure TTargetJava.Run;
|
---|
107 | begin
|
---|
108 | inherited;
|
---|
109 | RunFromFile;
|
---|
110 | end;
|
---|
111 |
|
---|
112 | end.
|
---|
113 |
|
---|