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/UTargetDelphi.pas

    r48 r52  
    3939procedure TTargetDelphi.Compile;
    4040var
    41   I: Integer;
    4241  Sum: Integer;
    43 
    44 function CheckOccurence(C: Char): Integer;
    45 begin
    46   Result := 1;
    47   if Optimization = coNormal then
    48   while ((I + 1) <= Length(FSourceCode)) and (FSourceCode[I + 1] = C) do begin
    49     Inc(Result);
    50     Inc(I)
    51   end;
    52 end;
    53 
    54 function CheckClear: Boolean;
    55 begin
    56   Result := (FSourceCode[I] = '[') and (Length(FSourceCode) >= I + 2) and
    57     (FSourceCode[I + 1] = '-') and (FSourceCode[I + 2] = ']');
    58 end;
    59 
    6042begin
    6143  inherited;
     
    7456  Inc(Indent);
    7557  AddLine('Pos := 0;');
    76   I := 1;
    77   while (I <= Length(FSourceCode)) do begin
    78     case FSourceCode[I] of
    79       '>': begin
    80         Sum := CheckOccurence('>');
     58  FProgramIndex := 0;
     59  while (FProgramIndex < Length(FProgram)) do begin
     60    case FProgram[FProgramIndex] of
     61      cmPointerInc: begin
     62        Sum := CheckOccurence(cmPointerInc);
    8163        AddLine('Inc(Pos, ' + IntToStr(Sum) + ');');
    8264      end;
    83       '<': begin
    84         Sum := CheckOccurence('<');
     65      cmPointerDec: begin
     66        Sum := CheckOccurence(cmPointerDec);
    8567        AddLine('Dec(Pos, ' + IntToStr(Sum) + ');');
    8668      end;
    87       '+': begin
    88         Sum := CheckOccurence('+');
     69      cmInc: begin
     70        Sum := CheckOccurence(cmInc);
    8971        AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(Sum) + ';');
    9072      end;
    91       '-': begin
    92         Sum := CheckOccurence('-');
     73      cmDec: begin
     74        Sum := CheckOccurence(cmDec);
    9375        AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(Sum) + ';');
    9476      end;
    95       '.': AddLine('Write(Chr(Memory[Pos]));');
    96       ',': AddLine('Read(ReadChar); Memory[Pos] := Ord(ReadChar);');
    97       '[': begin
     77      cmOutput: AddLine('Write(Chr(Memory[Pos]));');
     78      cmInput: AddLine('Read(ReadChar); Memory[Pos] := Ord(ReadChar);');
     79      cmLoopStart: begin
    9880        if CheckClear then begin
    9981          AddLine('Memory[Pos] := 0;');
    100           Inc(I, 2);
     82          Inc(FProgramIndex, 2);
    10183        end else begin
    10284          AddLine('while Memory[Pos] <> 0 do begin');
     
    10486        end;
    10587      end;
    106       ']': begin
     88      cmLoopEnd: begin
    10789        Dec(Indent);
    10890        AddLine('end;');
    10991      end;
    11092    end;
    111     Inc(I);
     93    Inc(FProgramIndex);
    11294  end;
    11395  Dec(Indent);
Note: See TracChangeset for help on using the changeset viewer.