source: tags/1.3.0/Target/TargetPHP.pas

Last change on this file was 162, checked in by chronos, 3 months ago

Merged revision(s) 161 from trunk:

  • Fixed: All targets compilation and run.
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 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}
42end;
43
44function TTargetPHP.GetMemoryCell: string;
45begin
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 + ']';
52end;
53
54procedure TTargetPHP.Compile;
55begin
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;
90end;
91
92procedure TTargetPHP.Run;
93begin
94 inherited;
95 RunFromFile;
96end;
97
98end.
99
Note: See TracBrowser for help on using the repository browser.