Changeset 6 for trunk


Ignore:
Timestamp:
Feb 9, 2012, 3:22:38 PM (13 years ago)
Author:
chronos
Message:
  • Modified: Interpretter now wait for key press if Read instruction is executed and no input character is available.
  • Added: Actions to control program executions.
Location:
trunk
Files:
10 added
1 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/UBrainFuck.pas

    r5 r6  
    1818  end;
    1919
     20  TRunState = (rsStopped, rsPaused, rsRunning);
     21
    2022  { TBrainFuckInterpreter }
    2123
    2224  TBrainFuckInterpreter = class
    2325  private
     26    FOnChangeState: TNotifyEvent;
     27    FState: TRunState;
     28    procedure SetState(AValue: TRunState);
    2429    procedure Write(Value: Byte);
    2530    function Read: Byte;
     
    3944    procedure SingleStep;
    4045    procedure Run;
     46    procedure Pause;
     47    procedure Stop;
    4148    constructor Create;
    4249    destructor Destroy; override;
     50    property State: TRunState read FState;
     51    property OnChangeState: TNotifyEvent read FOnChangeState write FOnChangeState;
    4352  end;
    4453
     
    5867end;
    5968
     69procedure TBrainFuckInterpreter.SetState(AValue: TRunState);
     70begin
     71  if FState = AValue then Exit;
     72  FState := AValue;
     73  if Assigned(FOnChangeState) then FOnChangeState(Self);
     74end;
     75
    6076function TBrainFuckInterpreter.Read: Byte;
    6177var
    6278  Character: string;
    6379begin
    64   if InputPosition < Length(Input) then begin
    65     Character := Copy(Input, InputPosition, 1);
    66     Result := Ord(Character[1]);
    67     Inc(InputPosition);
    68   end else
    69     raise Exception.Create(SReadInputError);
     80  while InputPosition >= Length(Input) do begin
     81    Sleep(1);
     82    Application.ProcessMessages;
     83  end;
     84  Character := Copy(Input, InputPosition, 1);
     85  Result := Ord(Character[1]);
     86  Inc(InputPosition);
    7087end;
    7188
     
    140157procedure TBrainFuckInterpreter.Run;
    141158begin
     159  SetState(rsRunning);
    142160  Reset;
    143161  while SourcePosition < Length(Source) do begin
    144162    SingleStep;
    145163    Application.ProcessMessages;
    146   end;
     164    while State = rsPaused do begin
     165      Sleep(1);
     166      Application.ProcessMessages;
     167    end;
     168  end;
     169  SetState(rsStopped);
     170end;
     171
     172procedure TBrainFuckInterpreter.Pause;
     173begin
     174  if State = rsRunning then SetState(rsPaused);
     175end;
     176
     177procedure TBrainFuckInterpreter.Stop;
     178begin
     179  SetState(rsStopped);
    147180end;
    148181
  • trunk/UInterpreterForm.lfm

    r5 r6  
    11object InterpreterForm: TInterpreterForm
    2   Left = 242
     2  Left = 262
    33  Height = 509
    4   Top = 116
     4  Top = 127
    55  Width = 762
    66  Caption = 'Runtime'
     
    1010  object Panel3: TPanel
    1111    Left = 0
    12     Height = 509
    13     Top = 0
     12    Height = 483
     13    Top = 26
    1414    Width = 323
    1515    Align = alLeft
    1616    BevelOuter = bvNone
    17     ClientHeight = 509
     17    ClientHeight = 483
    1818    ClientWidth = 323
    1919    TabOrder = 0
     
    3434        Width = 309
    3535        Anchors = [akTop, akLeft, akRight, akBottom]
     36        OnKeyPress = MemoInputKeyPress
     37        ScrollBars = ssAutoBoth
    3638        TabOrder = 0
    3739      end
     
    5658    object Panel2: TPanel
    5759      Left = 0
    58       Height = 262
     60      Height = 236
    5961      Top = 247
    6062      Width = 323
    6163      Align = alClient
    6264      BevelOuter = bvNone
    63       ClientHeight = 262
     65      ClientHeight = 236
    6466      ClientWidth = 323
    6567      TabOrder = 2
     
    7476      object MemoOutput: TMemo
    7577        Left = 8
    76         Height = 237
     78        Height = 211
    7779        Top = 22
    7880        Width = 309
    7981        Anchors = [akTop, akLeft, akRight, akBottom]
     82        ScrollBars = ssAutoBoth
    8083        TabOrder = 0
    8184      end
     
    8487  object Splitter2: TSplitter
    8588    Left = 323
    86     Height = 509
    87     Top = 0
     89    Height = 483
     90    Top = 26
    8891    Width = 5
    8992  end
    9093  object Panel4: TPanel
    9194    Left = 328
    92     Height = 509
    93     Top = 0
     95    Height = 483
     96    Top = 26
    9497    Width = 434
    9598    Align = alClient
    9699    BevelOuter = bvNone
    97     ClientHeight = 509
     100    ClientHeight = 483
    98101    ClientWidth = 434
    99102    TabOrder = 2
     
    148151    object ListViewMemory: TListView
    149152      Left = 8
    150       Height = 384
     153      Height = 358
    151154      Top = 120
    152155      Width = 422
     
    179182    end
    180183  end
     184  object ToolBar1: TToolBar
     185    Left = 0
     186    Height = 26
     187    Top = 0
     188    Width = 762
     189    Caption = 'ToolBar1'
     190    Images = MainForm.ImageList1
     191    TabOrder = 3
     192    object ToolButton1: TToolButton
     193      Left = 1
     194      Top = 2
     195      Action = MainForm.AProgramRun
     196    end
     197    object ToolButton2: TToolButton
     198      Left = 24
     199      Top = 2
     200      Action = MainForm.AProgramPause
     201    end
     202    object ToolButton3: TToolButton
     203      Left = 47
     204      Top = 2
     205      Action = MainForm.AProgramStop
     206    end
     207  end
    181208  object Timer1: TTimer
    182209    Interval = 500
  • trunk/UInterpreterForm.pas

    r5 r6  
    3636    Splitter2: TSplitter;
    3737    Timer1: TTimer;
     38    ToolBar1: TToolBar;
     39    ToolButton1: TToolButton;
     40    ToolButton2: TToolButton;
     41    ToolButton3: TToolButton;
    3842    procedure ListViewMemoryData(Sender: TObject; Item: TListItem);
     43    procedure MemoInputKeyPress(Sender: TObject; var Key: char);
    3944    procedure Timer1Timer(Sender: TObject);
    4045  private
     
    4247  public
    4348    procedure RefreshListViewMemory;
     49    procedure UpadateInterface;
    4450  end;
    4551
     
    7177end;
    7278
     79procedure TInterpreterForm.UpadateInterface;
     80begin
     81  MainForm.UpdateInterface;
     82end;
     83
    7384procedure TInterpreterForm.ListViewMemoryData(Sender: TObject; Item: TListItem);
    7485var
     
    8697end;
    8798
     99procedure TInterpreterForm.MemoInputKeyPress(Sender: TObject; var Key: char);
     100begin
     101  with MainForm.BrainFuckInterpreter do Input := Input + Key;
     102end;
     103
    88104end.
    89105
  • trunk/UMainForm.lfm

    r5 r6  
    6868      Left = 103
    6969      Top = 2
    70       Action = ARun
     70      Action = AProgramRun
    7171    end
    7272    object ToolButton7: TToolButton
     
    369369        }
    370370      end
     371      object MenuItem17: TMenuItem
     372        Caption = '-'
     373      end
    371374      object MenuItem12: TMenuItem
    372         Action = ARun
     375        Action = AProgramRun
    373376        Bitmap.Data = {
    374377          36040000424D3604000000000000360000002800000010000000100000000100
     
    407410          0000000000000000000000000000000000000000000000000000
    408411        }
     412      end
     413      object MenuItem15: TMenuItem
     414        Action = AProgramPause
     415      end
     416      object MenuItem16: TMenuItem
     417        Action = AProgramStop
    409418      end
    410419    end
     
    499508      ImageIndex = 8
    500509    end
    501     object ARun: TAction
     510    object AProgramRun: TAction
    502511      Caption = 'Run'
    503512      ImageIndex = 1
    504       OnExecute = ARunExecute
     513      OnExecute = AProgramRunExecute
    505514    end
    506515    object AAbout: TAction
     
    510519      Caption = 'Help'
    511520      ImageIndex = 6
     521    end
     522    object AProgramPause: TAction
     523      Caption = 'Pause'
     524      ImageIndex = 11
     525      OnExecute = AProgramPauseExecute
     526    end
     527    object AProgramStop: TAction
     528      Caption = 'Stop'
     529      ImageIndex = 10
     530      OnExecute = AProgramStopExecute
    512531    end
    513532  end
     
    516535    top = 96
    517536    Bitmap = {
    518       4C690A0000001000000010000000000000000000000000000000000000000000
     537      4C690C0000001000000010000000000000000000000000000000000000000000
    519538      0000000000000000000000000000000000000000000000000000000000000000
    520539      000000000000000000000000000000000000000000FF000000FF000000FF0000
     
    836855      00FF000000FF0000000000000000000000000000000000000000000000000000
    837856      0000000000000000000000000000000000000000000000000000000000000000
     857      0000000000000000000000000000000000000000000000000000000000000000
     858      0000000000000000000000000000000000000000000000000000000000000000
     859      0000000000000000000000000000000000000000000000000000000000000000
     860      0000000000000000000000000000000000000000000000000000000000000000
     861      0000000000000000000000000000000000000000000000000000000000000000
     862      0000000000000000000000000000000000000000000000000000000000000000
     863      0000000000000000000000000000000000000000000000000000000000000000
     864      0000000000000000000000000000000000000000000000000000000000000000
     865      0000000000000000000000000000000000000000000000000000000000000000
     866      FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000
     867      0000000000000000000000000000000000000000000000000000000000000000
     868      FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000
     869      0000000000000000000000000000000000000000000000000000000000000000
     870      FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000
     871      0000000000000000000000000000000000000000000000000000000000000000
     872      FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000
     873      0000000000000000000000000000000000000000000000000000000000000000
     874      FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000
     875      0000000000000000000000000000000000000000000000000000000000000000
     876      FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000
     877      0000000000000000000000000000000000000000000000000000000000000000
     878      FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000
     879      0000000000000000000000000000000000000000000000000000000000000000
     880      FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000
     881      0000000000000000000000000000000000000000000000000000000000000000
     882      0000000000000000000000000000000000000000000000000000000000000000
     883      0000000000000000000000000000000000000000000000000000000000000000
     884      0000000000000000000000000000000000000000000000000000000000000000
     885      0000000000000000000000000000000000000000000000000000000000000000
     886      0000000000000000000000000000000000000000000000000000000000000000
     887      0000000000000000000000000000000000000000000000000000000000000000
     888      0000000000000000000000000000000000000000000000000000000000000000
     889      0000000000000000000000000000000000000000000000000000000000000000
     890      0000000000000000000000000000000000000000000000000000000000000000
     891      0000000000000000000000000000000000000000000000000000000000000000
     892      0000000000000000000000000000000000000000000000000000000000000000
     893      0000000000000000000000000000000000000000000000000000DE9077BFDA8A
     894      70FFD88367FFD57C61FF0000000000000000DE9077BFDA8A70FFD88367FFD57C
     895      61FF000000000000000000000000000000000000000000000000D9866CBFEBB0
     896      9DFFF0BBABFFD27457FF0000000000000000D9866CBFEBB09DFFF0BBABFFD274
     897      57FF000000000000000000000000000000000000000000000000D57C61BFE8A7
     898      93FFEDB6A3FFCD6849FF0000000000000000D57C61BFE8A793FFEDB6A3FFCD68
     899      49FF000000000000000000000000000000000000000000000000D27457BFE5A1
     900      8BFFEBAF9AFFC95E3EFF0000000000000000D27457BFE5A18BFFEBAF9AFFC95E
     901      3EFF000000000000000000000000000000000000000000000000CD6849BFE198
     902      81FFE8A793FFC45432FF0000000000000000CD6849BFE19881FFE8A793FFC454
     903      32FF000000000000000000000000000000000000000000000000C86A4DBFE7A5
     904      90FFE5A18BFFBF4A27FF0000000000000000C86A4DBFE7A590FFE5A18BFFBF4A
     905      27FF000000000000000000000000000000000000000000000000B95435BFE299
     906      84FFE29A85FFB5401DFF0000000000000000B95435BFE29984FFE29A85FFB540
     907      1DFF000000000000000000000000000000000000000000000000BF4A27C0D985
     908      6BFFDF957EFFAA3A18FF0000000000000000BF4A27C0D9856BFFDF957EFFAA3A
     909      18FF000000000000000000000000000000000000000000000000B5401DBFD57C
     910      61FFDE9077FF993414FF0000000000000000B5401DBFD57C61FFDE9077FF9934
     911      14FF000000000000000000000000000000000000000000000000AA3A18BFD375
     912      58FFDC8B71FF8A2C0FFF0000000000000000AA3A18BFD37558FFDC8B71FF8A2C
     913      0FFF000000000000000000000000000000000000000000000000993414BFCF6F
     914      50FFDA886DFF7F270BFF0000000000000000993414BFCF6F50FFDA886DFF7F27
     915      0BFF0000000000000000000000000000000000000000000000008A2C0FBF842A
     916      0EFF7C260BFF7A250AFF00000000000000008A2C0FBF842A0EFF7C260BFF7A25
     917      0AFF000000000000000000000000000000000000000000000000000000000000
     918      0000000000000000000000000000000000000000000000000000000000000000
     919      0000000000000000000000000000000000000000000000000000000000000000
     920      0000000000000000000000000000000000000000000000000000000000000000
    838921      0000000000000000000000000000
    839922    }
  • trunk/UMainForm.pas

    r5 r6  
    1616    ACompile: TAction;
    1717    AAbout: TAction;
     18    AProgramPause: TAction;
     19    AProgramStop: TAction;
    1820    AHelp: TAction;
    19     ARun: TAction;
     21    AProgramRun: TAction;
    2022    AProjectNew: TAction;
    2123    AExit: TAction;
     
    3436    MenuItem13: TMenuItem;
    3537    MenuItem14: TMenuItem;
     38    MenuItem15: TMenuItem;
     39    MenuItem16: TMenuItem;
     40    MenuItem17: TMenuItem;
    3641    MenuItem2: TMenuItem;
    3742    MenuItem3: TMenuItem;
     
    5560    ToolButton7: TToolButton;
    5661    procedure AExitExecute(Sender: TObject);
     62    procedure AProgramPauseExecute(Sender: TObject);
     63    procedure AProgramStopExecute(Sender: TObject);
    5764    procedure AProjectCloseExecute(Sender: TObject);
    5865    procedure AProjectNewExecute(Sender: TObject);
     
    6067    procedure AProjectSaveAsExecute(Sender: TObject);
    6168    procedure AProjectSaveExecute(Sender: TObject);
    62     procedure ARunExecute(Sender: TObject);
     69    procedure AProgramRunExecute(Sender: TObject);
    6370    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    6471    procedure FormCreate(Sender: TObject);
     
    6774    procedure MemoSourceChange(Sender: TObject);
    6875  private
    69     { private declarations }
     76    procedure BrainFuckInterpreterChangeState(Sender: TObject);
    7077  public
    7178    Modified: Boolean;
     
    95102begin
    96103  Modified := True;
     104  UpdateInterface;
     105end;
     106
     107procedure TMainForm.BrainFuckInterpreterChangeState(Sender: TObject);
     108begin
    97109  UpdateInterface;
    98110end;
     
    109121  MemoSource.Enabled := ProjectFileName <> '';
    110122  AProjectClose.Enabled := ProjectFileName <> '';
    111   ARun.Enabled := ProjectFileName <> '';
     123  AProgramRun.Enabled := (ProjectFileName <> '') and (BrainFuckInterpreter.State = rsStopped);
     124  AProgramPause.Enabled := (ProjectFileName <> '') and (BrainFuckInterpreter.State = rsRunning);
     125  AProgramStop.Enabled := (ProjectFileName <> '') and (BrainFuckInterpreter.State <> rsStopped);
    112126  ACompile.Enabled := ProjectFileName <> '';
    113127end;
     
    116130begin
    117131  BrainFuckInterpreter := TBrainFuckInterpreter.Create;
     132  BrainFuckInterpreter.OnChangeState := BrainFuckInterpreterChangeState;
    118133end;
    119134
     
    123138end;
    124139
    125 procedure TMainForm.ARunExecute(Sender: TObject);
     140procedure TMainForm.AProgramRunExecute(Sender: TObject);
    126141begin
    127142  InterpreterForm.Show;
     
    138153begin
    139154  Close;
     155end;
     156
     157procedure TMainForm.AProgramPauseExecute(Sender: TObject);
     158begin
     159  MainForm.BrainFuckInterpreter.Pause
     160end;
     161
     162procedure TMainForm.AProgramStopExecute(Sender: TObject);
     163begin
     164  MainForm.BrainFuckInterpreter.Stop;
    140165end;
    141166
Note: See TracChangeset for help on using the changeset viewer.