| 1 | unit UTargetDelphi;
|
|---|
| 2 |
|
|---|
| 3 | {$mode Delphi}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, UTarget, UBFTarget;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 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 |
|
|---|
| 23 | implementation
|
|---|
| 24 |
|
|---|
| 25 | { TTargetDelphi }
|
|---|
| 26 |
|
|---|
| 27 | constructor TTargetDelphi.Create;
|
|---|
| 28 | begin
|
|---|
| 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}
|
|---|
| 39 | end;
|
|---|
| 40 |
|
|---|
| 41 | function TTargetDelphi.GetMemoryCell: string;
|
|---|
| 42 | begin
|
|---|
| 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 + ']';
|
|---|
| 49 | end;
|
|---|
| 50 |
|
|---|
| 51 | procedure TTargetDelphi.Compile;
|
|---|
| 52 | begin
|
|---|
| 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;
|
|---|
| 94 | end;
|
|---|
| 95 |
|
|---|
| 96 | procedure TTargetDelphi.Run;
|
|---|
| 97 | begin
|
|---|
| 98 | inherited Run;
|
|---|
| 99 | RunFromFile;
|
|---|
| 100 | end;
|
|---|
| 101 |
|
|---|
| 102 | end.
|
|---|
| 103 |
|
|---|