Changeset 20 for trunk


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
Files:
8 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);');
  • trunk/Languages/LazFuckIDE.cs.po

    r19 r20  
    150150msgstr "UloÅŸit jako..."
    151151
     152#: tmainform.ashrinksource.caption
     153msgid "Shrink source"
     154msgstr ""
     155
    152156#: tmainform.aviewcompiled.caption
    153157msgid "Compiled"
     
    181185msgstr "Zobrazení"
    182186
    183 #: tmainform.menuitem21.caption
    184 msgid "New Item21"
    185 msgstr ""
    186 
    187187#: tmainform.menuitem24.caption
    188188msgid "Tools"
  • trunk/Languages/LazFuckIDE.po

    r19 r20  
    141141msgstr ""
    142142
     143#: tmainform.ashrinksource.caption
     144msgid "Shrink source"
     145msgstr ""
     146
    143147#: tmainform.aviewcompiled.caption
    144148msgid "Compiled"
     
    172176msgstr ""
    173177
    174 #: tmainform.menuitem21.caption
    175 msgid "New Item21"
    176 msgstr ""
    177 
    178178#: tmainform.menuitem24.caption
    179179msgid "Tools"
  • trunk/UMainForm.lfm

    r19 r20  
    7979    end
    8080    object ToolButton5: TToolButton
    81       Left = 93
     81      Left = 116
    8282      Top = 2
    8383      Width = 10
     
    8686    end
    8787    object ToolButton6: TToolButton
    88       Left = 103
     88      Left = 149
    8989      Top = 2
    9090      Action = AProgramRun
     
    9696    end
    9797    object ToolButton8: TToolButton
    98       Left = 149
     98      Left = 93
    9999      Top = 2
    100100      Action = AOptions
    101101    end
    102102    object ToolButton9: TToolButton
     103      Left = 218
     104      Top = 2
     105      Action = ABreakpointToggle
     106    end
     107    object ToolButton10: TToolButton
    103108      Left = 172
    104109      Top = 2
    105       Action = ABreakpointToggle
     110      Action = AProgramPause
     111    end
     112    object ToolButton11: TToolButton
     113      Left = 195
     114      Top = 2
     115      Action = AProgramStop
    106116    end
    107117  end
     
    402412      object MenuItemTarget: TMenuItem
    403413        Caption = 'Target'
    404         object MenuItem21: TMenuItem
    405           Caption = 'New Item21'
    406         end
    407414      end
    408415      object MenuItem17: TMenuItem
     
    531538      object MenuItem25: TMenuItem
    532539        Action = AFormatSource
     540      end
     541      object MenuItem27: TMenuItem
     542        Action = AShrinkSource
    533543      end
    534544    end
     
    727737      Caption = 'Format source'
    728738      OnExecute = AFormatSourceExecute
     739    end
     740    object AShrinkSource: TAction
     741      Caption = 'Shrink source'
     742      OnExecute = AShrinkSourceExecute
    729743    end
    730744  end
     
    12151229      Action = AFormatSource
    12161230    end
     1231    object MenuItem21: TMenuItem
     1232      Action = AShrinkSource
     1233    end
    12171234  end
    12181235end
  • trunk/UMainForm.lrt

    r19 r20  
    55TMAINFORM.MENUITEM9.CAPTION=Program
    66TMAINFORM.MENUITEMTARGET.CAPTION=Target
    7 TMAINFORM.MENUITEM21.CAPTION=New Item21
    87TMAINFORM.MENUITEM17.CAPTION=-
    98TMAINFORM.MENUITEM24.CAPTION=Tools
     
    3130TMAINFORM.AOPTIONS.CAPTION=Options
    3231TMAINFORM.AFORMATSOURCE.CAPTION=Format source
     32TMAINFORM.ASHRINKSOURCE.CAPTION=Shrink source
  • trunk/UMainForm.pas

    r19 r20  
    1818    AAbout: TAction;
    1919    ABreakpointToggle: TAction;
     20    AShrinkSource: TAction;
    2021    AFormatSource: TAction;
    2122    AOptions: TAction;
     
    5455    MenuItem2: TMenuItem;
    5556    MenuItem20: TMenuItem;
     57    MenuItem21: TMenuItem;
    5658    MenuItem22: TMenuItem;
    5759    MenuItem23: TMenuItem;
     
    5961    MenuItem25: TMenuItem;
    6062    MenuItem26: TMenuItem;
     63    MenuItem27: TMenuItem;
    6164    MenuItemTarget: TMenuItem;
    62     MenuItem21: TMenuItem;
    6365    MenuItem3: TMenuItem;
    6466    MenuItem4: TMenuItem;
     
    7577    ToolBar1: TToolBar;
    7678    ToolButton1: TToolButton;
     79    ToolButton10: TToolButton;
     80    ToolButton11: TToolButton;
    7781    ToolButton2: TToolButton;
    7882    ToolButton3: TToolButton;
     
    96100    procedure AProjectSaveExecute(Sender: TObject);
    97101    procedure AProgramRunExecute(Sender: TObject);
     102    procedure AShrinkSourceExecute(Sender: TObject);
    98103    procedure AViewCompiledExecute(Sender: TObject);
    99104    procedure AViewInterpretterExecute(Sender: TObject);
     
    115120  public
    116121    Modified: Boolean;
     122    CompilerIndex: Integer;
    117123    ProjectFileName: string;
    118124    BrainFuckCompiler: TBrainFuckCompiler;
     
    180186begin
    181187  with TMenuItem(Sender) do begin
     188    CompilerIndex := MenuIndex;
    182189    Checked := True;
    183190    for I := 0 to Parent.Count - 1 do
     
    219226    NewMenuItem.Caption := TBrainFuckCompiler(Compilers[I]).Name;
    220227    NewMenuItem.OnClick := MenuItemTargetClick;
    221     if I = 0 then NewMenuItem.Checked := True;
     228    if I = CompilerIndex then NewMenuItem.Checked := True;
    222229    MenuItemTarget.Add(NewMenuItem);
    223230  end;
     
    251258  BrainFuckInterpreter.Source := MemoSource.Text;
    252259  BrainFuckInterpreter.Run;
     260end;
     261
     262procedure TMainForm.AShrinkSourceExecute(Sender: TObject);
     263var
     264  Source: string;
     265  Pos: Integer;
     266  I: Integer;
     267begin
     268  Source := MemoSource.Text;
     269  Pos := 1;
     270  for I := 1 to Length(Source) do begin
     271    if Source[I] > ' ' then begin
     272      Source[Pos] := Source[I];
     273      Inc(Pos);
     274    end;
     275  end;
     276  SetLength(Source, Pos - 1);
     277  MemoSource.Text := Source;
    253278end;
    254279
     
    310335
    311336procedure TMainForm.ACompileExecute(Sender: TObject);
    312 var
    313   I: Integer;
    314 begin
    315   I := 0;
    316   while (I < MenuItemTarget.Count) and (not MenuItemTarget.Items[I].Checked) do Inc(I);
    317   with TBrainFuckCompiler(Compilers[I]) do begin
     337begin
     338  with TBrainFuckCompiler(Compilers[CompilerIndex]) do begin
     339    Optimization := coNormal;
    318340    Source := MemoSource.Text;
    319341    ProgramName := ExtractFileNameOnly(ProjectFileName);
Note: See TracChangeset for help on using the changeset viewer.