source: trunk/Target/TargetFPC.pas

Last change on this file was 146, checked in by chronos, 11 months ago
  • Fixed: Build on Windows.
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 FRunExtension := '';
36 {$ENDIF}
37 {$IFDEF UNIX}
38 FCompiledExtension := '';
39 CompilerPath := '/usr/bin/fpc';
40 FRunExtension := '';
41 {$ENDIF}
42end;
43
44function TTargetFPC.GetMemoryCell: string;
45begin
46 Result := 'Memory[Pos';
47 if FProgram[FProgramIndex].RelIndex > 0 then
48 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
49 else if FProgram[FProgramIndex].RelIndex < 0 then
50 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
51 Result := Result + ']';
52end;
53
54procedure TTargetFPC.Compile;
55begin
56 inherited;
57 Indent := 0;
58 FTargetCode := '';
59
60 AddLine('program ' + ProgramName + ';');
61 AddLine('var');
62 AddLine(' Memory: array[0..' + IntToStr(MemorySize) + '] of Byte;');
63 AddLine(' Pos: Integer;');
64 AddLine(' ReadChar: Char;');
65 AddLine('begin');
66 Inc(Indent);
67 AddLine('Pos := 0;');
68 FProgramIndex := 0;
69 while FProgramIndex < FProgram.Count do begin
70 case FProgram[FProgramIndex].Command of
71 cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
72 cmPointerDec: AddLine('Dec(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
73 cmInc: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
74 cmDec: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
75 cmSet: AddLine(GetMemoryCell + ' := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
76 cmMultiply: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
77 cmOutput: AddLine('Write(Chr(' + GetMemoryCell + '));');
78 cmInput: AddLine('Read(ReadChar); ' + GetMemoryCell + ' := Ord(ReadChar);');
79 cmLoopStart: begin
80 AddLine('while ' + GetMemoryCell + ' <> 0 do begin');
81 Inc(Indent);
82 end;
83 cmLoopEnd: begin
84 Dec(Indent);
85 AddLine('end;');
86 end;
87 end;
88 Inc(FProgramIndex);
89 end;
90 Dec(Indent);
91 AddLine('end.');
92
93 CompileToFile;
94end;
95
96procedure TTargetFPC.Run;
97begin
98 inherited;
99 RunFromFile;
100end;
101
102end.
103
Note: See TracBrowser for help on using the repository browser.