source: tags/1.2.0/Target/UTargetJava.pas

Last change on this file was 133, checked in by chronos, 3 years ago
  • Added: Compile multiple action for compilation of code with mutliple compilers at once.
File size: 3.0 KB
Line 
1unit UTargetJava;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, UTarget, UBFTarget, Dialogs;
9
10type
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
24implementation
25
26{ TTargetJava }
27
28constructor TTargetJava.Create;
29begin
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}
45end;
46
47function TTargetJava.GetMemoryCell: string;
48begin
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 + ']';
55end;
56
57procedure TTargetJava.Compile;
58begin
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;
104end;
105
106procedure TTargetJava.Run;
107begin
108 inherited;
109 RunFromFile;
110end;
111
112end.
113
Note: See TracBrowser for help on using the repository browser.