Changeset 9


Ignore:
Timestamp:
Feb 9, 2012, 4:10:10 PM (12 years ago)
Author:
chronos
Message:
  • Added: Easy compiler to Delphi.
Location:
trunk
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/LazFuckIDE.lpi

    r8 r9  
    3939      </Item2>
    4040    </RequiredPackages>
    41     <Units Count="5">
     41    <Units Count="6">
    4242      <Unit0>
    4343        <Filename Value="LazFuckIDE.lpr"/>
     
    6969        <UnitName Value="UApplicationInfo"/>
    7070      </Unit4>
     71      <Unit5>
     72        <Filename Value="UCompiledForm.pas"/>
     73        <IsPartOfProject Value="True"/>
     74        <ComponentName Value="CompiledForm"/>
     75        <ResourceBaseClass Value="Form"/>
     76        <UnitName Value="UCompiledForm"/>
     77      </Unit5>
    7178    </Units>
    7279  </ProjectOptions>
  • trunk/LazFuckIDE.lpr

    r8 r9  
    88  {$ENDIF}{$ENDIF}
    99  Interfaces, // this includes the LCL widgetset
    10   Forms, UMainForm, UBrainFuck, UInterpreterForm, UApplicationInfo
     10  Forms, UMainForm, UBrainFuck, UInterpreterForm, UApplicationInfo,
     11UCompiledForm
    1112  { you can add units after this };
    1213
     
    1920  Application.CreateForm(TMainForm, MainForm);
    2021  Application.CreateForm(TInterpreterForm, InterpreterForm);
     22  Application.CreateForm(TCompiledForm, CompiledForm);
    2123  Application.Run;
    2224end.
  • trunk/UBrainFuck.pas

    r8 r9  
    66
    77uses
    8   Classes, SysUtils, Dialogs, Forms;
     8  Classes, SysUtils, Dialogs, Forms, StrUtils;
    99
    1010type
    1111  TBrainFuckInterpretter = class;
    1212
    13   { TBrainFuck }
    14 
    15   TBrainFuck = class
    16     Source: TStringList;
    17     Output: TStringList;
     13  TCompilerTarget = (ctDelphi);
     14  { TBrainFuckCompiler }
     15
     16  TBrainFuckCompiler = class
     17  private
     18    Indent: Integer;
     19    procedure AddLine(Text: string);
     20  public
     21    Source: string;
     22    Output: string;
     23    Target: TCompilerTarget;
    1824    procedure Compile;
    1925  end;
     
    218224end;
    219225
    220 { TBrainFuck }
    221 
    222 procedure TBrainFuck.Compile;
    223 begin
    224 
     226{ TBrainFuckCompiler }
     227
     228procedure TBrainFuckCompiler.AddLine(Text: string);
     229begin
     230  Output := Output + DupeString(' ', Indent) + Text + LineEnding;
     231end;
     232
     233procedure TBrainFuckCompiler.Compile;
     234var
     235  I: Integer;
     236begin
     237  Indent := 0;
     238  Output := '';
     239
     240  AddLine('program Brainfuck;');
     241  AddLine('');
     242  AddLine('{$APPTYPE CONSOLE}');
     243  AddLine('');
     244  AddLine('uses');
     245  AddLine('  Classes;');
     246  AddLine('  ');
     247  AddLine('var');
     248  AddLine('  Memory: array[0..30000] of Char;');
     249  AddLine('  Position: Integer;');
     250  AddLine('begin');
     251  for I := 1 to Length(Source) do begin
     252    case Source[I] of
     253      '>': AddLine('Inc(Position);');
     254      '<': AddLine('Dec(Position);');
     255      '+': AddLine('Memory[Position] := Memory[Position] + 1');
     256      '-': AddLine('Memory[Position] := Memory[Position] - 1');
     257      '.': AddLine('Write(Memory[Position]);');
     258      ',': AddLine('Read(Memory[Position]);');
     259      '[': begin
     260        AddLine('while Memory[Position] <> 0 do begin');
     261        Inc(Indent, 2);
     262      end;
     263      ']': begin
     264        Dec(Indent, 2);
     265        AddLine('end;');
     266      end;
     267    end;
     268  end;
     269  AddLine('end.');
    225270end;
    226271
  • trunk/UMainForm.lfm

    r6 r9  
    11object MainForm: TMainForm
    2   Left = 270
     2  Left = 258
    33  Height = 465
    4   Top = 120
     4  Top = 121
    55  Width = 643
    66  Caption = 'LazFuck'
     
    369369        }
    370370      end
     371      object MenuItem40: TMenuItem
     372        Caption = 'Target'
     373        object MenuItem21: TMenuItem
     374          Caption = 'New Item21'
     375        end
     376      end
    371377      object MenuItem17: TMenuItem
    372378        Caption = '-'
     
    416422      object MenuItem16: TMenuItem
    417423        Action = AProgramStop
     424      end
     425    end
     426    object MenuItem18: TMenuItem
     427      Caption = 'View'
     428      object MenuItem19: TMenuItem
     429        Action = AViewInterpretter
     430      end
     431      object MenuItem20: TMenuItem
     432        Action = AViewCompiled
    418433      end
    419434    end
     
    507522      Caption = 'Compile'
    508523      ImageIndex = 8
     524      OnExecute = ACompileExecute
    509525    end
    510526    object AProgramRun: TAction
     
    529545      ImageIndex = 10
    530546      OnExecute = AProgramStopExecute
     547    end
     548    object AViewInterpretter: TAction
     549      Caption = 'Interpretter'
     550      OnExecute = AViewInterpretterExecute
     551    end
     552    object AViewCompiled: TAction
     553      Caption = 'Compiled'
     554      OnExecute = AViewCompiledExecute
    531555    end
    532556  end
  • trunk/UMainForm.pas

    r7 r9  
    1616    ACompile: TAction;
    1717    AAbout: TAction;
     18    AViewCompiled: TAction;
     19    AViewInterpretter: TAction;
    1820    AProgramPause: TAction;
    1921    AProgramStop: TAction;
     
    3941    MenuItem16: TMenuItem;
    4042    MenuItem17: TMenuItem;
     43    MenuItem18: TMenuItem;
     44    MenuItem19: TMenuItem;
    4145    MenuItem2: TMenuItem;
     46    MenuItem20: TMenuItem;
     47    MenuItem40: TMenuItem;
     48    MenuItem21: TMenuItem;
    4249    MenuItem3: TMenuItem;
    4350    MenuItem4: TMenuItem;
     
    5966    ToolButton6: TToolButton;
    6067    ToolButton7: TToolButton;
     68    procedure ACompileExecute(Sender: TObject);
    6169    procedure AExitExecute(Sender: TObject);
    6270    procedure AProgramPauseExecute(Sender: TObject);
     
    6876    procedure AProjectSaveExecute(Sender: TObject);
    6977    procedure AProgramRunExecute(Sender: TObject);
     78    procedure AViewCompiledExecute(Sender: TObject);
     79    procedure AViewInterpretterExecute(Sender: TObject);
    7080    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    7181    procedure FormCreate(Sender: TObject);
     
    7888    Modified: Boolean;
    7989    ProjectFileName: string;
     90    BrainFuckCompiler: TBrainFuckCompiler;
    8091    BrainFuckInterpreter: TBrainFuckInterpretter;
    8192    procedure UpdateInterface;
     
    90101
    91102uses
    92   UInterpreterForm, UApplicationInfo;
     103  UInterpreterForm, UApplicationInfo, UCompiledForm;
    93104
    94105{ TMainForm }
     
    131142  BrainFuckInterpreter := TBrainFuckInterpretter.Create;
    132143  BrainFuckInterpreter.OnChangeState := BrainFuckInterpreterChangeState;
     144  BrainFuckCompiler := TBrainFuckCompiler.Create;
    133145end;
    134146
    135147procedure TMainForm.FormDestroy(Sender: TObject);
    136148begin
     149  BrainFuckCompiler.Free;
    137150  BrainFuckInterpreter.Free;
    138151end;
     
    146159end;
    147160
     161procedure TMainForm.AViewCompiledExecute(Sender: TObject);
     162begin
     163  CompiledForm.Show;
     164end;
     165
     166procedure TMainForm.AViewInterpretterExecute(Sender: TObject);
     167begin
     168  InterpreterForm.Show;
     169end;
     170
    148171procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: boolean);
    149172begin
     173  if Modified then AProjectSaveAs.Execute;
    150174end;
    151175
     
    153177begin
    154178  Close;
     179end;
     180
     181procedure TMainForm.ACompileExecute(Sender: TObject);
     182begin
     183  BrainFuckCompiler.Source := MemoSource.Text;
     184  BrainFuckCompiler.Compile;
     185  CompiledForm.MemoCompiled.Text := BrainFuckCompiler.Output;
     186  CompiledForm.Show;
    155187end;
    156188
Note: See TracChangeset for help on using the changeset viewer.