source: tags/1.2.0/Target/UTargetPHP.pas

Last change on this file was 133, checked in by chronos, 3 years ago
  • Added: Compile multiple action for compilation of code with mutliple compilers at once.
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;
31 FName := 'PHP';
32 FSourceExtension := '.php';
33 FRunExtension := '.php';
34 FCompiledExtension := '.php';
35 FImageIndex := 21;
36 FCapabilities := [tcCompile, tcRun];
37 {$IFDEF WINDOWS}
38 CompilerPath := '';
39 ExecutorPath := 'c:\Program Files\PHP\php.exe';
40 {$ENDIF}
41 {$IFDEF UNIX}
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 < FProgram.Count 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 cmMultiply: 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;
98 RunFromFile;
99end;
100
101end.
102
Note: See TracBrowser for help on using the repository browser.