source: tags/1.3.0/Target/TargetJava.pas

Last change on this file was 162, checked in by chronos, 3 months ago

Merged revision(s) 161 from trunk:

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