Changeset 6 for trunk/UBrainFuck.pas
- Timestamp:
- Feb 9, 2012, 3:22:38 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UBrainFuck.pas
r5 r6 18 18 end; 19 19 20 TRunState = (rsStopped, rsPaused, rsRunning); 21 20 22 { TBrainFuckInterpreter } 21 23 22 24 TBrainFuckInterpreter = class 23 25 private 26 FOnChangeState: TNotifyEvent; 27 FState: TRunState; 28 procedure SetState(AValue: TRunState); 24 29 procedure Write(Value: Byte); 25 30 function Read: Byte; … … 39 44 procedure SingleStep; 40 45 procedure Run; 46 procedure Pause; 47 procedure Stop; 41 48 constructor Create; 42 49 destructor Destroy; override; 50 property State: TRunState read FState; 51 property OnChangeState: TNotifyEvent read FOnChangeState write FOnChangeState; 43 52 end; 44 53 … … 58 67 end; 59 68 69 procedure TBrainFuckInterpreter.SetState(AValue: TRunState); 70 begin 71 if FState = AValue then Exit; 72 FState := AValue; 73 if Assigned(FOnChangeState) then FOnChangeState(Self); 74 end; 75 60 76 function TBrainFuckInterpreter.Read: Byte; 61 77 var 62 78 Character: string; 63 79 begin 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); 70 87 end; 71 88 … … 140 157 procedure TBrainFuckInterpreter.Run; 141 158 begin 159 SetState(rsRunning); 142 160 Reset; 143 161 while SourcePosition < Length(Source) do begin 144 162 SingleStep; 145 163 Application.ProcessMessages; 146 end; 164 while State = rsPaused do begin 165 Sleep(1); 166 Application.ProcessMessages; 167 end; 168 end; 169 SetState(rsStopped); 170 end; 171 172 procedure TBrainFuckInterpreter.Pause; 173 begin 174 if State = rsRunning then SetState(rsPaused); 175 end; 176 177 procedure TBrainFuckInterpreter.Stop; 178 begin 179 SetState(rsStopped); 147 180 end; 148 181
Note:
See TracChangeset
for help on using the changeset viewer.