Ignore:
Timestamp:
Feb 11, 2012, 10:56:03 PM (13 years ago)
Author:
chronos
Message:
  • Added: Action to shrink source code by remove all non printable characters.
  • Added: Optimalization for generated code by Delphi and PHP compiler. All same language commands laying side by side will be converted to single generated operation.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/UCompilerDelphi.pas

    r19 r20  
    3030var
    3131  I: Integer;
     32  Sum: Integer;
     33
     34function CheckOccurence(C: Char): Integer;
     35begin
     36  Result := 1;
     37  if Optimization = coNormal then
     38  while ((I + 1) <= Length(Source)) and (Source[I + 1] = C) do begin
     39    Inc(Result);
     40    Inc(I)
     41  end;
     42end;
     43
    3244begin
    3345  Indent := 0;
     
    3951  AddLine('');
    4052  AddLine('var');
    41   AddLine('  Memory: array[0..30000] of Char;');
    42   AddLine('  Position: Integer;');
     53  AddLine('  Memory: array[0..30000] of Byte;');
     54  AddLine('  Pos: Integer;');
    4355  AddLine('begin');
    4456  Inc(Indent);
    45   for I := 1 to Length(Source) do begin
     57  I := 1;
     58  while (I <= Length(Source)) do begin
    4659    case Source[I] of
    47       '>': AddLine('Inc(Position);');
    48       '<': AddLine('Dec(Position);');
    49       '+': AddLine('Memory[Position] := Succ(Memory[Position]);');
    50       '-': AddLine('Memory[Position] := Pred(Memory[Position]);');
    51       '.': AddLine('Write(Memory[Position]);');
    52       ',': AddLine('Read(Memory[Position]);');
     60      '>': begin
     61        Sum := CheckOccurence('>');
     62        AddLine('Inc(Pos, ' + IntToStr(Sum) + ');');
     63      end;
     64      '<': begin
     65        Sum := CheckOccurence('<');
     66        AddLine('Dec(Pos, ' + IntToStr(Sum) + ');');
     67      end;
     68      '+': begin
     69        Sum := CheckOccurence('+');
     70        AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(Sum) + ';');
     71      end;
     72      '-': begin
     73        Sum := CheckOccurence('-');
     74        AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(Sum) + ';');
     75      end;
     76      '.': AddLine('Write(Chr(Memory[Pos]));');
     77      ',': AddLine('Read(Chr(Memory[Pos]));');
    5378      '[': begin
    54         AddLine('while Memory[Position] <> #0 do begin');
     79        AddLine('while Memory[Pos] <> 0 do begin');
    5580        Inc(Indent);
    5681      end;
     
    6085      end;
    6186    end;
     87    Inc(I);
    6288  end;
    6389  Dec(Indent);
Note: See TracChangeset for help on using the changeset viewer.