source: trunk/Target/TargetPHP.pas

Last change on this file was 145, checked in by chronos, 11 months ago
  • Modified: Remove U prefix from unit names.
  • Modified: Updated Common package.
File size: 2.6 KB
Line 
1unit TargetPHP;
2
3interface
4
5uses
6 Classes, SysUtils, Target, BFTarget;
7
8type
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
22implementation
23
24{ TTargetPHP }
25
26constructor TTargetPHP.Create;
27begin
28 inherited;
29 FName := 'PHP';
30 FSourceExtension := '.php';
31 FRunExtension := '.php';
32 FCompiledExtension := '.php';
33 FImageIndex := 21;
34 FCapabilities := [tcCompile, tcRun];
35 {$IFDEF WINDOWS}
36 CompilerPath := '';
37 ExecutorPath := 'c:\Program Files\PHP\php.exe';
38 {$ENDIF}
39 {$IFDEF UNIX}
40 CompilerPath := '';
41 ExecutorPath := '/usr/bin/php';
42 {$ENDIF}
43end;
44
45function TTargetPHP.GetMemoryCell: string;
46begin
47 Result := '$Memory[$Position';
48 if FProgram[FProgramIndex].RelIndex > 0 then
49 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
50 else if FProgram[FProgramIndex].RelIndex < 0 then
51 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
52 Result := Result + ']';
53end;
54
55procedure TTargetPHP.Compile;
56begin
57 inherited;
58 Indent := 0;
59 FTargetCode := '';
60
61 AddLine('<?php // ' + ProgramName);
62 AddLine('');
63 AddLine('$Memory = str_repeat("\0", ' + IntToStr(MemorySize) + ');');
64 AddLine('$Position = 0;');
65 FProgramIndex := 0;
66 while FProgramIndex < FProgram.Count do begin
67 case FProgram[FProgramIndex].Command of
68 cmPointerInc: AddLine('$Position = $Position + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
69 cmPointerDec: AddLine('$Position = $Position - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
70 cmInc: AddLine(GetMemoryCell + ' = chr(ord(' + GetMemoryCell + ') + ' +
71 IntToStr(FProgram[FProgramIndex].Parameter) + ');');
72 cmDec: AddLine(GetMemoryCell + ' = chr(ord(' + GetMemoryCell + ') - ' +
73 IntToStr(FProgram[FProgramIndex].Parameter) + ');');
74 cmOutput: AddLine('echo(' + GetMemoryCell + ');');
75 cmInput: AddLine(GetMemoryCell + ' = fgetc(STDIN);');
76 cmSet: AddLine(GetMemoryCell + ' = chr(' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
77 cmMultiply: AddLine(GetMemoryCell + ' = chr(ord(' + GetMemoryCell + ') + ord($Memory[$Position]) * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');
78 cmLoopStart: begin
79 AddLine('while(' + GetMemoryCell + ' != "\0") {');
80 Inc(Indent);
81 end;
82 cmLoopEnd: begin
83 Dec(Indent);
84 AddLine('}');
85 end;
86 end;
87 Inc(FProgramIndex);
88 end;
89
90 CompileToFile;
91end;
92
93procedure TTargetPHP.Run;
94begin
95 inherited;
96 RunFromFile;
97end;
98
99end.
100
Note: See TracBrowser for help on using the repository browser.