| 1 | unit UTargetPHP;
|
|---|
| 2 |
|
|---|
| 3 | {$mode Delphi}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, UTarget, UBFTarget;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 |
|
|---|
| 12 | { TTargetPHP }
|
|---|
| 13 |
|
|---|
| 14 | TTargetPHP = 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 |
|
|---|
| 24 | implementation
|
|---|
| 25 |
|
|---|
| 26 | { TTargetPHP }
|
|---|
| 27 |
|
|---|
| 28 | constructor TTargetPHP.Create;
|
|---|
| 29 | begin
|
|---|
| 30 | inherited Create;
|
|---|
| 31 | Name := 'PHP';
|
|---|
| 32 | SourceExtension := '.php';
|
|---|
| 33 | RunExtension := '.php';
|
|---|
| 34 | CompiledExtension := '.php';
|
|---|
| 35 | ImageIndex := 21;
|
|---|
| 36 | Capabilities := [tcCompile, tcRun];
|
|---|
| 37 | {$IFDEF Windows}
|
|---|
| 38 | CompilerPath := '';
|
|---|
| 39 | ExecutorPath := 'c:\Program Files\PHP\php.exe';
|
|---|
| 40 | {$ENDIF}
|
|---|
| 41 | {$IFDEF Linux}
|
|---|
| 42 | CompilerPath := '';
|
|---|
| 43 | ExecutorPath := '/usr/bin/php';
|
|---|
| 44 | {$ENDIF}
|
|---|
| 45 | end;
|
|---|
| 46 |
|
|---|
| 47 | function TTargetPHP.GetMemoryCell: string;
|
|---|
| 48 | begin
|
|---|
| 49 | Result := '$Memory[$Position';
|
|---|
| 50 | if FProgram[FProgramIndex].RelIndex > 0 then
|
|---|
| 51 | Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
|
|---|
| 52 | else if FProgram[FProgramIndex].RelIndex < 0 then
|
|---|
| 53 | Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
|
|---|
| 54 | Result := Result + ']';
|
|---|
| 55 | end;
|
|---|
| 56 |
|
|---|
| 57 | procedure TTargetPHP.Compile;
|
|---|
| 58 | begin
|
|---|
| 59 | inherited;
|
|---|
| 60 | Indent := 0;
|
|---|
| 61 | FTargetCode := '';
|
|---|
| 62 |
|
|---|
| 63 | AddLine('<?php // ' + ProgramName);
|
|---|
| 64 | AddLine('');
|
|---|
| 65 | AddLine('$Memory = str_repeat("\0", ' + IntToStr(MemorySize) + ');');
|
|---|
| 66 | AddLine('$Position = 0;');
|
|---|
| 67 | FProgramIndex := 0;
|
|---|
| 68 | while (FProgramIndex < Length(FProgram)) do begin
|
|---|
| 69 | case FProgram[FProgramIndex].Command of
|
|---|
| 70 | cmPointerInc: AddLine('$Position = $Position + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
|---|
| 71 | cmPointerDec: AddLine('$Position = $Position - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
|---|
| 72 | cmInc: AddLine(GetMemoryCell + ' = chr(ord(' + GetMemoryCell + ') + ' +
|
|---|
| 73 | IntToStr(FProgram[FProgramIndex].Parameter) + ');');
|
|---|
| 74 | cmDec: AddLine(GetMemoryCell + ' = chr(ord(' + GetMemoryCell + ') - ' +
|
|---|
| 75 | IntToStr(FProgram[FProgramIndex].Parameter) + ');');
|
|---|
| 76 | cmOutput: AddLine('echo(' + GetMemoryCell + ');');
|
|---|
| 77 | cmInput: AddLine(GetMemoryCell + ' = fgetc(STDIN);');
|
|---|
| 78 | cmSet: AddLine(GetMemoryCell + ' = chr(' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
|
|---|
| 79 | cmMultipy: AddLine(GetMemoryCell + ' = chr(ord(' + GetMemoryCell + ') + ord($Memory[$Position]) * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
|
|---|
| 80 | cmLoopStart: begin
|
|---|
| 81 | AddLine('while(' + GetMemoryCell + ' != "\0") {');
|
|---|
| 82 | Inc(Indent);
|
|---|
| 83 | end;
|
|---|
| 84 | cmLoopEnd: begin
|
|---|
| 85 | Dec(Indent);
|
|---|
| 86 | AddLine('}');
|
|---|
| 87 | end;
|
|---|
| 88 | end;
|
|---|
| 89 | Inc(FProgramIndex);
|
|---|
| 90 | end;
|
|---|
| 91 |
|
|---|
| 92 | CompileToFile;
|
|---|
| 93 | end;
|
|---|
| 94 |
|
|---|
| 95 | procedure TTargetPHP.Run;
|
|---|
| 96 | begin
|
|---|
| 97 | inherited Run;
|
|---|
| 98 | RunFromFile;
|
|---|
| 99 | end;
|
|---|
| 100 |
|
|---|
| 101 | end.
|
|---|
| 102 |
|
|---|