source: tags/1.2.0/Target/UTargetFPC.pas

Last change on this file was 139, checked in by chronos, 3 years ago
  • Modified: Version 1.2.0 release related changes.
File size: 2.7 KB
Line 
1unit UTargetFPC;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, UTarget, UBFTarget;
9
10type
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
23implementation
24
25{ TTargetFPC }
26
27constructor TTargetFPC.Create;
28begin
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}
44end;
45
46function TTargetFPC.GetMemoryCell: string;
47begin
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 + ']';
54end;
55
56procedure TTargetFPC.Compile;
57begin
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;
96end;
97
98procedure TTargetFPC.Run;
99begin
100 inherited;
101 RunFromFile;
102end;
103
104end.
105
Note: See TracBrowser for help on using the repository browser.