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