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

    r48 r52  
    3535  Capabilities := [tcCompile, tcRun];
    3636  {$IFDEF Windows}
    37   CompilerPath := 'c:\Program Files\MinGW\bin\gcc.exe -o %1:s';
     37  CompilerPath := 'c:\Program Files\MinGW\bin\gcc.exe';
    3838  {$ENDIF}
    3939  {$IFDEF Linux}
     
    4444procedure TTargetC.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 
    5947begin
    6048  inherited;
     
    7260  AddLine('');
    7361  AddLine('Pos = 0;');
    74   I := 1;
    75   while (I <= Length(FSourceCode)) do begin
    76     case FSourceCode[I] of
    77       '>': begin
    78         Sum := CheckOccurence('>');
     62  FProgramIndex := 0;
     63  while (FProgramIndex < Length(FProgram)) do begin
     64    case FProgram[FProgramIndex] of
     65      cmPointerInc: begin
     66        Sum := CheckOccurence(cmPointerInc);
    7967        AddLine('Pos = Pos + ' + IntToStr(Sum) + ';');
    8068      end;
    81       '<': begin
    82         Sum := CheckOccurence('<');
     69      cmPointerDec: begin
     70        Sum := CheckOccurence(cmPointerDec);
    8371        AddLine('Pos = Pos - ' + IntToStr(Sum) + ';');
    8472      end;
    85       '+': begin
    86         Sum := CheckOccurence('+');
     73      cmInc: begin
     74        Sum := CheckOccurence(cmInc);
    8775        AddLine('Memory[Pos] = Memory[Pos] + ' + IntToStr(Sum) + ';');
    8876      end;
    89       '-': begin
    90         Sum := CheckOccurence('-');
     77      cmDec: begin
     78        Sum := CheckOccurence(cmDec);
    9179        AddLine('Memory[Pos] = Memory[Pos] - ' + IntToStr(Sum) + ';');
    9280      end;
    93       '.': AddLine('putchar(Memory[Pos]);');
    94       ',': AddLine('Memory[Pos] = getchar();');
    95       '[': begin
    96         AddLine('while(Memory[Pos] != 0)');
    97         AddLine('{');
    98         Inc(Indent);
     81      cmOutput: AddLine('putchar(Memory[Pos]);');
     82      cmInput: AddLine('Memory[Pos] = getchar();');
     83      cmLoopStart: begin
     84        if CheckClear then begin
     85          AddLine('Memory[Pos] = 0;');
     86          Inc(FProgramIndex, 2);
     87        end else begin
     88          AddLine('while(Memory[Pos] != 0)');
     89          AddLine('{');
     90          Inc(Indent);
     91        end;
    9992      end;
    100       ']': begin
     93      cmLoopEnd: begin
    10194        Dec(Indent);
    10295        AddLine('}');
    10396      end;
    10497    end;
    105     Inc(I);
     98    Inc(FProgramIndex);
    10699  end;
    107100  AddLine('return 0;');
Note: See TracChangeset for help on using the changeset viewer.