Changeset 6 for trunk/UBrainFuck.pas


Ignore:
Timestamp:
Feb 9, 2012, 3:22:38 PM (12 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.
File:
1 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
Note: See TracChangeset for help on using the changeset viewer.