source: tags/1.0.0/Target/UTargetDelphi.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 UTargetDelphi;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, UTarget, UBFTarget;
9
10type
11
12 { TTargetDelphi }
13
14 TTargetDelphi = 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{ TTargetDelphi }
26
27constructor TTargetDelphi.Create;
28begin
29 inherited Create;
30 Name := 'Delphi';
31 SourceExtension := '.pas';
32 ImageIndex := 22;
33 Capabilities := [tcCompile, tcRun];
34 {$IFDEF Windows}
35 CompilerPath := 'c:\Program Files\Embarcadero\RAD Studio\9.0\bin\DCC32.EXE';
36 CompiledExtension := '.exe';
37 RunExtension := '';
38 {$ENDIF}
39end;
40
41function TTargetDelphi.GetMemoryCell: string;
42begin
43 Result := 'Memory[Pos';
44 if FProgram[FProgramIndex].RelIndex > 0 then
45 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
46 else if FProgram[FProgramIndex].RelIndex < 0 then
47 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
48 Result := Result + ']';
49end;
50
51procedure TTargetDelphi.Compile;
52begin
53 inherited;
54 Indent := 0;
55 FTargetCode := '';
56
57 AddLine('program ' + ProgramName + ';');
58 AddLine('');
59 AddLine('{$APPTYPE CONSOLE}');
60 AddLine('');
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 < Length(FProgram)) 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 cmMultipy: 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 TTargetDelphi.Run;
97begin
98 inherited Run;
99 RunFromFile;
100end;
101
102end.
103
Note: See TracBrowser for help on using the repository browser.