Ignore:
Timestamp:
Jul 26, 2012, 3:11:08 PM (12 years ago)
Author:
chronos
Message:
  • Modified: Optimization functions moved to shared place in TTarget.
  • Modified: Text source is loaded to program source array of brainfuck commands for better processing.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Target/UTargetPHP.pas

    r49 r52  
    4444procedure TTargetPHP.Compile;
    4545var
    46   I: Integer;
    4746  Sum: Integer;
    48 
    49 function CheckOccurence(C: Char): Integer;
    50 begin
    51   Result := 1;
    52   if Optimization = coNormal then
    53   while ((I + 1) <= Length(FSourceCode)) and (FSourceCode[I + 1] = C) do begin
    54     Inc(Result);
    55     Inc(I)
    56   end;
    57 end;
    58 
    59 function CheckClear: Boolean;
    60 begin
    61   Result := (FSourceCode[I] = '[') and (Length(FSourceCode) >= I + 2) and
    62     (FSourceCode[I + 1] = '-') and (FSourceCode[I + 2] = ']');
    63 end;
    64 
    6547begin
    6648  inherited;
     
    7254  AddLine('$Memory = str_repeat("\0", 30000);');
    7355  AddLine('$Position = 0;');
    74   I := 1;
    75   while (I <= Length(FSourceCode)) do begin
    76     case FSourceCode[I] of
    77       '>': begin
    78         Sum := CheckOccurence('>');
     56  FProgramIndex := 0;
     57  while (FProgramIndex < Length(FProgram)) do begin
     58    case FProgram[FProgramIndex] of
     59      cmPointerInc: begin
     60        Sum := CheckOccurence(cmPointerInc);
    7961        AddLine('$Position = $Position + ' + IntToStr(Sum) + ';');
    8062      end;
    81       '<': begin
    82         Sum := CheckOccurence('<');
     63      cmPointerDec: begin
     64        Sum := CheckOccurence(cmPointerDec);
    8365        AddLine('$Position = $Position - ' + IntToStr(Sum) + ';');
    8466      end;
    85       '+': begin
    86         Sum := CheckOccurence('+');
     67      cmInc: begin
     68        Sum := CheckOccurence(cmInc);
    8769        AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) + ' + IntToStr(Sum) + ');');
    8870      end;
    89       '-': begin
    90         Sum := CheckOccurence('-');
     71      cmDec: begin
     72        Sum := CheckOccurence(cmDec);
    9173        AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) - ' + IntToStr(Sum) + ');');
    9274      end;
    93       '.': AddLine('echo($Memory[$Position]);');
    94       ',': AddLine('$Memory[$Position] = fgetc(STDIN);');
    95       '[': begin
     75      cmOutput: AddLine('echo($Memory[$Position]);');
     76      cmInput: AddLine('$Memory[$Position] = fgetc(STDIN);');
     77      cmLoopStart: begin
    9678        if CheckClear then begin
    9779          AddLine('$Memory[$Position] = "\0";');
    98           Inc(I, 2);
     80          Inc(FProgramIndex, 2);
    9981        end else begin
    10082          AddLine('while($Memory[$Position] != "\0") {');
     
    10284        end;
    10385      end;
    104       ']': begin
     86      cmLoopEnd: begin
    10587        Dec(Indent);
    10688        AddLine('}');
    10789      end;
    10890    end;
    109     Inc(I);
     91    Inc(FProgramIndex);
    11092  end;
    11193  AddLine('');
Note: See TracChangeset for help on using the changeset viewer.