source: tags/1.1.0/Target/UTargetFPC.pas

Last change on this file was 87, checked in by chronos, 7 years ago
  • Added: New CSharp target.
  • Fixed: Use configured memory size in targets.
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 Create;
30 Name := 'FPC';
31 SourceExtension := '.pas';
32 ImageIndex := 22;
33 Capabilities := [tcCompile, tcRun];
34 {$IFDEF Linux}
35 CompiledExtension := '';
36 CompilerPath := '/usr/bin/fpc';
37 RunExtension := '';
38 {$ENDIF}
39 {$IFDEF Windows}
40 CompiledExtension := '.exe';
41 CompilerPath := 'fpc.exe';
42 RunExtension := '';
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 < Length(FProgram)) 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 cmMultipy: 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 Run;
101 RunFromFile;
102end;
103
104end.
105
Note: See TracBrowser for help on using the repository browser.