Changeset 14 for trunk


Ignore:
Timestamp:
Feb 11, 2012, 6:27:10 PM (12 years ago)
Author:
chronos
Message:
  • Added: Support for memory cell up to Integer size. Default 256.
  • Fixed: Application now can be closed if running program is waiting to new key press.
Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/LazFuckIDE.lpi

    r13 r14  
    3232      </local>
    3333    </RunParams>
    34     <RequiredPackages Count="2">
     34    <RequiredPackages Count="4">
    3535      <Item1>
    36         <PackageName Value="SynEdit"/>
     36        <PackageName Value="TemplateGenerics"/>
    3737      </Item1>
    3838      <Item2>
     39        <PackageName Value="Common"/>
     40      </Item2>
     41      <Item3>
     42        <PackageName Value="SynEdit"/>
     43      </Item3>
     44      <Item4>
    3945        <PackageName Value="LCL"/>
    40       </Item2>
     46      </Item4>
    4147    </RequiredPackages>
    4248    <Units Count="7">
  • trunk/LazFuckIDE.lpr

    r13 r14  
    1010  Interfaces, // this includes the LCL widgetset
    1111  Forms, UMainForm, UBrainFuck, UInterpreterForm, UApplicationInfo,
    12 UCompiledForm, UOptionsForm
     12UCompiledForm, UOptionsForm, Common, TemplateGenerics
    1313  { you can add units after this };
    1414
  • trunk/UBrainFuck.pas

    r13 r14  
    4343  TBrainFuckInterpretter = class
    4444  private
     45    FCellSize: Integer;
    4546    FOnChangeState: TNotifyEvent;
    4647    FState: TRunState;
     
    4849    FThread: TBrainFuckInterpretterThread;
    4950    FStepCount: Integer;
     51    function GetMemorySize: Integer;
    5052    function GetSource: string;
     53    procedure SetMemorySize(AValue: Integer);
    5154    procedure SetSource(AValue: string);
    5255    procedure SetState(AValue: TRunState);
    5356    procedure Write(Value: Byte);
    5457    function Read: Byte;
     58    function TryRead(Output: Byte): Boolean;
    5559    function ReadCode: Char;
    5660    procedure SetThread(State: Boolean);
     
    6165    SourcePosition: Integer;
    6266    SourceBreakpoint: array of Boolean;
    63     Memory: array of Byte;
     67    Memory: array of Integer;
    6468    MemoryPosition: Integer;
    6569    Output: string;
     
    7781    property StepCount: Integer read FStepCount;
    7882    property Source: string read GetSource write SetSource;
     83    property MemorySize: Integer read GetMemorySize write SetMemorySize;
     84    property CellSize: Integer read FCellSize write FCellSize;
    7985  end;
    8086
     
    8894  SJumpTableInsistent = 'Jump table is inconsistent';
    8995  SJumpTableColision = 'Jump table colision';
     96  SMemoryCellOutOfRange = 'Memory cell %s value out of range';
    9097
    9198{ TBrainFuckInterpretterThread }
     
    128135end;
    129136
     137function TBrainFuckInterpretter.GetMemorySize: Integer;
     138begin
     139  Result := Length(Memory);
     140end;
     141
     142procedure TBrainFuckInterpretter.SetMemorySize(AValue: Integer);
     143begin
     144  SetLength(Memory, AValue);
     145end;
     146
    130147procedure TBrainFuckInterpretter.SetSource(AValue: string);
    131148var
     
    140157function TBrainFuckInterpretter.Read: Byte;
    141158begin
    142   while InputPosition >= Length(Input) do begin
     159  while (InputPosition >= Length(Input)) and (FState <> rsStopped) do begin
    143160    Sleep(1);
    144161  end;
    145   Result := Ord(Input[InputPosition + 1]);
    146   Inc(InputPosition);
     162  if InputPosition < Length(Input) then begin
     163    Result := Ord(Input[InputPosition + 1]);
     164    Inc(InputPosition);
     165  end else Result := 0;
     166end;
     167
     168function TBrainFuckInterpretter.TryRead(Output: Byte): Boolean;
     169begin
     170  if InputPosition >= Length(Input) then Result := False
     171  else begin
     172    Output := Ord(Input[InputPosition + 1]);
     173    Inc(InputPosition);
     174    Result := True;
     175  end;
    147176end;
    148177
     
    217246begin
    218247  case ReadCode of
    219     '>': if MemoryPosition < Length(Memory) then Inc(MemoryPosition)
     248    '>': if MemoryPosition < MemorySize then Inc(MemoryPosition)
    220249      else raise Exception.Create(SProgramUpperLimit);
    221250    '<': if MemoryPosition > 0 then Dec(MemoryPosition)
    222251      else raise Exception.Create(SProgramLowerLimit);
    223     '+': Memory[MemoryPosition] := Memory[MemoryPosition] + 1;
    224     '-': Memory[MemoryPosition] := Memory[MemoryPosition] - 1;
     252    '+': Memory[MemoryPosition] := ((Memory[MemoryPosition] + 1) mod CellSize);
     253    '-': Memory[MemoryPosition] := ((Memory[MemoryPosition] - 1) mod CellSize);
    225254    '.': Write(Memory[MemoryPosition]);
    226255    ',': Memory[MemoryPosition] := Read;
     
    282311constructor TBrainFuckInterpretter.Create;
    283312begin
    284   SetLength(Memory, 32000);
     313  MemorySize := 30000;
     314  CellSize := 256;
    285315end;
    286316
    287317destructor TBrainFuckInterpretter.Destroy;
    288318begin
     319  FState := rsStopped;
    289320  SetThread(False);
    290321  inherited Destroy;
  • trunk/UMainForm.pas

    r13 r14  
    77uses
    88  Classes, SysUtils, FileUtil, SynEdit, Forms, Controls, Graphics, Dialogs,
    9   Menus, ActnList, StdCtrls, ComCtrls, UBrainFuck;
     9  Menus, ActnList, StdCtrls, ComCtrls, UBrainFuck, SpecializedList;
    1010
    1111type
     
    107107    BrainFuckCompiler: TBrainFuckCompiler;
    108108    BrainFuckInterpreter: TBrainFuckInterpretter;
     109    BreakPoints: TListInteger;
    109110    procedure UpdateInterface;
    110111    procedure UpdateStatusBar;
     
    185186procedure TMainForm.FormCreate(Sender: TObject);
    186187begin
     188  BreakPoints := TListInteger.Create;
    187189  BrainFuckInterpreter := TBrainFuckInterpretter.Create;
    188190  BrainFuckInterpreter.OnChangeState := BrainFuckInterpreterChangeState;
     
    194196  BrainFuckCompiler.Free;
    195197  BrainFuckInterpreter.Free;
     198  BreakPoints.Free;
    196199end;
    197200
     
    226229procedure TMainForm.AOptionsExecute(Sender: TObject);
    227230begin
     231  OptionsForm.LoadFromInterpretter(BrainFuckInterpreter);
    228232  if OptionsForm.ShowModal = mrOK then begin
    229 
     233    OptionsForm.SaveToInterpretter(BrainFuckInterpreter);
    230234  end;
    231235end;
  • trunk/UOptionsForm.lfm

    r13 r14  
    4040    Height = 27
    4141    Top = 6
    42     Width = 128
     42    Width = 130
     43    MaxValue = 65535
    4344    TabOrder = 2
    4445  end
     46  object Label2: TLabel
     47    Left = 8
     48    Height = 18
     49    Top = 40
     50    Width = 60
     51    Caption = 'Cell size:'
     52    ParentColor = False
     53  end
     54  object SpinEditCellSize: TSpinEdit
     55    Left = 136
     56    Height = 27
     57    Top = 40
     58    Width = 130
     59    MaxValue = 65535
     60    TabOrder = 3
     61  end
    4562end
  • trunk/UOptionsForm.pas

    r13 r14  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    9   Spin;
     9  Spin, UBrainFuck;
    1010
    1111type
     
    1717    ButtonCancel: TButton;
    1818    Label1: TLabel;
     19    Label2: TLabel;
     20    SpinEditCellSize: TSpinEdit;
    1921    SpinEditMemorySize: TSpinEdit;
    2022  private
    2123    { private declarations }
    2224  public
    23     { public declarations }
     25    procedure LoadFromInterpretter(Interpretter: TBrainFuckInterpretter);
     26    procedure SaveToInterpretter(Interpretter: TBrainFuckInterpretter);
    2427  end;
    2528
     
    3134{$R *.lfm}
    3235
     36{ TOptionsForm }
     37
     38procedure TOptionsForm.LoadFromInterpretter(Interpretter: TBrainFuckInterpretter
     39  );
     40begin
     41  SpinEditCellSize.Value := Interpretter.CellSize;
     42  SpinEditMemorySize.Value := Interpretter.MemorySize;
     43end;
     44
     45procedure TOptionsForm.SaveToInterpretter(Interpretter: TBrainFuckInterpretter);
     46begin
     47  Interpretter.CellSize := SpinEditCellSize.Value;
     48  Interpretter.MemorySize := SpinEditMemorySize.Value;
     49end;
     50
    3351end.
    3452
Note: See TracChangeset for help on using the changeset viewer.