source: tags/1.3.0/Target/TargetFPC.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: 2.6 KB
Line 
1unit TargetFPC;
2
3interface
4
5uses
6 Classes, SysUtils, Target, BFTarget;
7
8type
9
10 { TTargetFPC }
11
12 TTargetFPC = 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
21implementation
22
23{ TTargetFPC }
24
25constructor TTargetFPC.Create;
26begin
27 inherited;
28 FName := 'FPC';
29 FSourceExtension := '.pas';
30 FImageIndex := 22;
31 FCapabilities := [tcCompile, tcRun];
32 {$IFDEF WINDOWS}
33 FCompiledExtension := '.exe';
34 CompilerPath := 'fpc.exe';
35 {$ENDIF}
36 {$IFDEF UNIX}
37 FCompiledExtension := '';
38 CompilerPath := '/usr/bin/fpc';
39 {$ENDIF}
40end;
41
42function TTargetFPC.GetMemoryCell: string;
43begin
44 Result := 'Memory[Pos';
45 if FProgram[FProgramIndex].RelIndex > 0 then
46 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
47 else if FProgram[FProgramIndex].RelIndex < 0 then
48 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
49 Result := Result + ']';
50end;
51
52procedure TTargetFPC.Compile;
53begin
54 inherited;
55 Indent := 0;
56 FTargetCode := '';
57
58 AddLine('program ' + ProgramName + ';');
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;
92end;
93
94procedure TTargetFPC.Run;
95begin
96 inherited;
97 RunFromFile;
98end;
99
100end.
101
Note: See TracBrowser for help on using the repository browser.