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