1 | unit TargetDelphi;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Target, BFTarget;
|
---|
7 |
|
---|
8 | type
|
---|
9 |
|
---|
10 | { TTargetDelphi }
|
---|
11 |
|
---|
12 | TTargetDelphi = 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 | implementation
|
---|
22 |
|
---|
23 | { TTargetDelphi }
|
---|
24 |
|
---|
25 | constructor TTargetDelphi.Create;
|
---|
26 | begin
|
---|
27 | inherited;
|
---|
28 | FName := 'Delphi';
|
---|
29 | FSourceExtension := '.pas';
|
---|
30 | FImageIndex := 22;
|
---|
31 | FCapabilities := [tcCompile];
|
---|
32 | {$IFDEF Windows}
|
---|
33 | FCapabilities := [tcCompile, tcRun];
|
---|
34 | CompilerPath := 'c:\Program Files\Embarcadero\RAD Studio\9.0\bin\DCC32.EXE';
|
---|
35 | FCompiledExtension := '.exe';
|
---|
36 | {$ENDIF}
|
---|
37 | end;
|
---|
38 |
|
---|
39 | function TTargetDelphi.GetMemoryCell: string;
|
---|
40 | begin
|
---|
41 | Result := 'Memory[Pos';
|
---|
42 | if FProgram[FProgramIndex].RelIndex > 0 then
|
---|
43 | Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
|
---|
44 | else if FProgram[FProgramIndex].RelIndex < 0 then
|
---|
45 | Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
|
---|
46 | Result := Result + ']';
|
---|
47 | end;
|
---|
48 |
|
---|
49 | procedure TTargetDelphi.Compile;
|
---|
50 | begin
|
---|
51 | inherited;
|
---|
52 | Indent := 0;
|
---|
53 | FTargetCode := '';
|
---|
54 |
|
---|
55 | AddLine('program ' + ProgramName + ';');
|
---|
56 | AddLine('');
|
---|
57 | AddLine('{$APPTYPE CONSOLE}');
|
---|
58 | AddLine('');
|
---|
59 | AddLine('var');
|
---|
60 | AddLine(' Memory: array[0..' + IntToStr(MemorySize) + '] of Byte;');
|
---|
61 | AddLine(' Pos: Integer;');
|
---|
62 | AddLine(' ReadChar: Char;');
|
---|
63 | AddLine('begin');
|
---|
64 | Inc(Indent);
|
---|
65 | AddLine('Pos := 0;');
|
---|
66 | FProgramIndex := 0;
|
---|
67 | while FProgramIndex < FProgram.Count do begin
|
---|
68 | case FProgram[FProgramIndex].Command of
|
---|
69 | cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
|
---|
70 | cmPointerDec: AddLine('Dec(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
|
---|
71 | cmInc: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
72 | cmDec: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
73 | cmSet: AddLine(GetMemoryCell + ' := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
74 | cmMultiply: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
75 | cmOutput: AddLine('Write(Chr(' + GetMemoryCell + '));');
|
---|
76 | cmInput: AddLine('Read(ReadChar); ' + GetMemoryCell + ' := Ord(ReadChar);');
|
---|
77 | cmLoopStart: begin
|
---|
78 | AddLine('while ' + GetMemoryCell + ' <> 0 do begin');
|
---|
79 | Inc(Indent);
|
---|
80 | end;
|
---|
81 | cmLoopEnd: begin
|
---|
82 | Dec(Indent);
|
---|
83 | AddLine('end;');
|
---|
84 | end;
|
---|
85 | end;
|
---|
86 | Inc(FProgramIndex);
|
---|
87 | end;
|
---|
88 | Dec(Indent);
|
---|
89 | AddLine('end.');
|
---|
90 |
|
---|
91 | CompileToFile;
|
---|
92 | end;
|
---|
93 |
|
---|
94 | procedure TTargetDelphi.Run;
|
---|
95 | begin
|
---|
96 | inherited;
|
---|
97 | RunFromFile;
|
---|
98 | end;
|
---|
99 |
|
---|
100 | end.
|
---|
101 |
|
---|