source: tags/1.0.0/Target/UTargetPHP.pas

Last change on this file was 87, checked in by chronos, 7 years ago
  • Added: New CSharp target.
  • Fixed: Use configured memory size in targets.
File size: 2.6 KB
Line 
1unit UTargetPHP;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, UTarget, UBFTarget;
9
10type
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
24implementation
25
26{ TTargetPHP }
27
28constructor TTargetPHP.Create;
29begin
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}
45end;
46
47function TTargetPHP.GetMemoryCell: string;
48begin
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 + ']';
55end;
56
57procedure TTargetPHP.Compile;
58begin
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;
93end;
94
95procedure TTargetPHP.Run;
96begin
97 inherited Run;
98 RunFromFile;
99end;
100
101end.
102
Note: See TracBrowser for help on using the repository browser.