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

    r48 r52  
    4545procedure TTargetJava.Compile;
    4646var
    47   I: Integer;
    4847  Sum: Integer;
    49 
    50 function CheckOccurence(C: Char): Integer;
    51 begin
    52   Result := 1;
    53   if Optimization = coNormal then
    54   while ((I + 1) <= Length(FSourceCode)) and (FSourceCode[I + 1] = C) do begin
    55     Inc(Result);
    56     Inc(I)
    57   end;
    58 end;
    59 
    6048begin
    6149  inherited;
     
    7563  AddLine('Memory = new char[30000];');
    7664  AddLine('Pos = 0;');
    77   I := 1;
    78   while (I <= Length(FSourceCode)) do begin
    79     case FSourceCode[I] of
    80       '>': begin
    81         Sum := CheckOccurence('>');
     65  FProgramIndex := 0;
     66  while (FProgramIndex < Length(FProgram)) do begin
     67    case FProgram[FProgramIndex] of
     68      cmPointerInc: begin
     69        Sum := CheckOccurence(cmPointerInc);
    8270        AddLine('Pos = Pos + ' + IntToStr(Sum) + ';');
    8371      end;
    84       '<': begin
    85         Sum := CheckOccurence('<');
     72      cmPointerDec: begin
     73        Sum := CheckOccurence(cmPointerDec);
    8674        AddLine('Pos = Pos - ' + IntToStr(Sum) + ';');
    8775      end;
    88       '+': begin
    89         Sum := CheckOccurence('+');
     76      cmInc: begin
     77        Sum := CheckOccurence(cmInc);
    9078        AddLine('Memory[Pos] = (char)((int)Memory[Pos] + ' + IntToStr(Sum) + ');');
    9179      end;
    92       '-': begin
    93         Sum := CheckOccurence('-');
     80      cmDec: begin
     81        Sum := CheckOccurence(cmDec);
    9482        AddLine('Memory[Pos] = (char)((int)Memory[Pos] - ' + IntToStr(Sum) + ');');
    9583      end;
    96       '.': AddLine('System.out.print(Memory[Pos]);');
    97       ',': AddLine('Memory[Pos] = (char)System.in.read();');
    98       '[': begin
    99         AddLine('while(Memory[Pos] != 0)');
    100         AddLine('{');
    101         Inc(Indent);
     84      cmOutput: AddLine('System.out.print(Memory[Pos]);');
     85      cmInput: AddLine('Memory[Pos] = (char)System.in.read();');
     86      cmLoopStart: begin
     87        if CheckClear then begin
     88          AddLine('Memory[Pos] = 0;');
     89          Inc(FProgramIndex, 2);
     90        end else begin
     91          AddLine('while(Memory[Pos] != 0)');
     92          AddLine('{');
     93          Inc(Indent);
     94        end;
    10295      end;
    103       ']': begin
     96      cmLoopEnd: begin
    10497        Dec(Indent);
    10598        AddLine('}');
    10699      end;
    107100    end;
    108     Inc(I);
     101    Inc(FProgramIndex);
    109102  end;
    110103  Dec(Indent);
Note: See TracChangeset for help on using the changeset viewer.