Changeset 52 for trunk/Target/UTargetPHP.pas
- Timestamp:
- Jul 26, 2012, 3:11:08 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Target/UTargetPHP.pas
r49 r52 44 44 procedure TTargetPHP.Compile; 45 45 var 46 I: Integer;47 46 Sum: Integer; 48 49 function CheckOccurence(C: Char): Integer;50 begin51 Result := 1;52 if Optimization = coNormal then53 while ((I + 1) <= Length(FSourceCode)) and (FSourceCode[I + 1] = C) do begin54 Inc(Result);55 Inc(I)56 end;57 end;58 59 function CheckClear: Boolean;60 begin61 Result := (FSourceCode[I] = '[') and (Length(FSourceCode) >= I + 2) and62 (FSourceCode[I + 1] = '-') and (FSourceCode[I + 2] = ']');63 end;64 65 47 begin 66 48 inherited; … … 72 54 AddLine('$Memory = str_repeat("\0", 30000);'); 73 55 AddLine('$Position = 0;'); 74 I := 1;75 while ( I <= Length(FSourceCode)) do begin76 case F SourceCode[I] of77 '>': begin78 Sum := CheckOccurence( '>');56 FProgramIndex := 0; 57 while (FProgramIndex < Length(FProgram)) do begin 58 case FProgram[FProgramIndex] of 59 cmPointerInc: begin 60 Sum := CheckOccurence(cmPointerInc); 79 61 AddLine('$Position = $Position + ' + IntToStr(Sum) + ';'); 80 62 end; 81 '<': begin82 Sum := CheckOccurence( '<');63 cmPointerDec: begin 64 Sum := CheckOccurence(cmPointerDec); 83 65 AddLine('$Position = $Position - ' + IntToStr(Sum) + ';'); 84 66 end; 85 '+': begin86 Sum := CheckOccurence( '+');67 cmInc: begin 68 Sum := CheckOccurence(cmInc); 87 69 AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) + ' + IntToStr(Sum) + ');'); 88 70 end; 89 '-': begin90 Sum := CheckOccurence( '-');71 cmDec: begin 72 Sum := CheckOccurence(cmDec); 91 73 AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) - ' + IntToStr(Sum) + ');'); 92 74 end; 93 '.': AddLine('echo($Memory[$Position]);');94 ',': AddLine('$Memory[$Position] = fgetc(STDIN);');95 '[': begin75 cmOutput: AddLine('echo($Memory[$Position]);'); 76 cmInput: AddLine('$Memory[$Position] = fgetc(STDIN);'); 77 cmLoopStart: begin 96 78 if CheckClear then begin 97 79 AddLine('$Memory[$Position] = "\0";'); 98 Inc( I, 2);80 Inc(FProgramIndex, 2); 99 81 end else begin 100 82 AddLine('while($Memory[$Position] != "\0") {'); … … 102 84 end; 103 85 end; 104 ']': begin86 cmLoopEnd: begin 105 87 Dec(Indent); 106 88 AddLine('}'); 107 89 end; 108 90 end; 109 Inc( I);91 Inc(FProgramIndex); 110 92 end; 111 93 AddLine('');
Note:
See TracChangeset
for help on using the changeset viewer.