Changeset 20 for trunk/Compiler


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.
Location:
trunk/Compiler
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/UCompiler.pas

    r19 r20  
    4040constructor TBrainFuckCompiler.Create;
    4141begin
    42 
     42  Optimization := coNormal;
    4343end;
    4444
  • 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);
  • trunk/Compiler/UCompilerPHP.pas

    r19 r20  
    3131var
    3232  I: Integer;
     33  Sum: Integer;
     34
     35function CheckOccurence(C: Char): Integer;
     36begin
     37  Result := 1;
     38  if Optimization = coNormal then
     39  while ((I + 1) <= Length(Source)) and (Source[I + 1] = C) do begin
     40    Inc(Result);
     41    Inc(I)
     42  end;
     43end;
     44
    3345begin
    3446  Indent := 0;
     
    4153  for I := 1 to Length(Source) do begin
    4254    case Source[I] of
    43       '>': AddLine('$Position++;');
    44       '<': AddLine('$Position--;');
    45       '+': AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) + 1);');
    46       '-': AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) - 1);');
     55      '>': begin
     56        Sum := CheckOccurence('>');
     57        AddLine('$Position = $Position + ' + IntToStr(Sum) + ';');
     58      end;
     59      '<': begin
     60        Sum := CheckOccurence('<');
     61        AddLine('$Position = $Position - ' + IntToStr(Sum) + ';');
     62      end;
     63      '+': begin
     64        Sum := CheckOccurence('+');
     65        AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) + ' + IntToStr(Sum) + ');');
     66      end;
     67      '-': begin
     68        Sum := CheckOccurence('-');
     69        AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) - ' + IntToStr(Sum) + ');');
     70      end;
    4771      '.': AddLine('echo($Memory[$Position]);');
    4872      ',': AddLine('$Memory[$Position] = fgetc(STDIN);');
Note: See TracChangeset for help on using the changeset viewer.