source: trunk/Target/TargetJava.pas

Last change on this file was 145, checked in by chronos, 11 months ago
  • Modified: Remove U prefix from unit names.
  • Modified: Updated Common package.
File size: 2.9 KB
Line 
1unit TargetJava;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Target, BFTarget, Dialogs;
7
8type
9
10 { TTargetJava }
11
12 TTargetJava = class(TBFTarget)
13 private
14 function GetMemoryCell: string;
15 public
16 constructor Create; override;
17 procedure Compile; override;
18 procedure Run; override;
19 end;
20
21
22implementation
23
24{ TTargetJava }
25
26constructor TTargetJava.Create;
27begin
28 inherited;
29 FName := 'Java';
30 FSourceExtension := '.java';
31 FCompiledExtension := '.class';
32 FRunExtension := '';
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
55procedure TTargetJava.Compile;
56begin
57 inherited;
58 Indent := 0;
59 FTargetCode := '';
60
61 AddLine('import java.io.*;');
62 AddLine;
63 AddLine('public class ' + ProgramName + ' {');
64 Inc(Indent);
65 AddLine('public static void main(String args[]) throws IOException {');
66 Inc(Indent);
67 AddLine('char Memory[];');
68 AddLine('int Pos;');
69 AddLine('char ReadChar;');
70 AddLine;
71 AddLine('Memory = new char[' + IntToStr(MemorySize) + '];');
72 AddLine('Pos = 0;');
73 FProgramIndex := 0;
74 while FProgramIndex < FProgram.Count do begin
75 case FProgram[FProgramIndex].Command of
76 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
77 cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
78 cmInc: AddLine(GetMemoryCell + ' = (char)((int)' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
79 cmDec: AddLine(GetMemoryCell + ' = (char)((int)' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
80 cmOutput: AddLine('System.out.print(' + GetMemoryCell + ');');
81 cmInput: AddLine(GetMemoryCell + ' = (char)System.in.read();');
82 cmSet: AddLine(GetMemoryCell + ' = (char)' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
83 cmMultiply: AddLine(GetMemoryCell + ' = (char)((int)' + GetMemoryCell + ' + (int)Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
84 cmLoopStart: begin
85 AddLine('while(' + GetMemoryCell + ' != 0)');
86 AddLine('{');
87 Inc(Indent);
88 end;
89 cmLoopEnd: begin
90 Dec(Indent);
91 AddLine('}');
92 end;
93 end;
94 Inc(FProgramIndex);
95 end;
96 Dec(Indent);
97 AddLine('}');
98 Dec(Indent);
99 AddLine('}');
100
101 CompileToFile;
102end;
103
104procedure TTargetJava.Run;
105begin
106 inherited;
107 RunFromFile;
108end;
109
110end.
111
Note: See TracBrowser for help on using the repository browser.