Changeset 14 for trunk/UBrainFuck.pas
- Timestamp:
- Feb 11, 2012, 6:27:10 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UBrainFuck.pas
r13 r14 43 43 TBrainFuckInterpretter = class 44 44 private 45 FCellSize: Integer; 45 46 FOnChangeState: TNotifyEvent; 46 47 FState: TRunState; … … 48 49 FThread: TBrainFuckInterpretterThread; 49 50 FStepCount: Integer; 51 function GetMemorySize: Integer; 50 52 function GetSource: string; 53 procedure SetMemorySize(AValue: Integer); 51 54 procedure SetSource(AValue: string); 52 55 procedure SetState(AValue: TRunState); 53 56 procedure Write(Value: Byte); 54 57 function Read: Byte; 58 function TryRead(Output: Byte): Boolean; 55 59 function ReadCode: Char; 56 60 procedure SetThread(State: Boolean); … … 61 65 SourcePosition: Integer; 62 66 SourceBreakpoint: array of Boolean; 63 Memory: array of Byte;67 Memory: array of Integer; 64 68 MemoryPosition: Integer; 65 69 Output: string; … … 77 81 property StepCount: Integer read FStepCount; 78 82 property Source: string read GetSource write SetSource; 83 property MemorySize: Integer read GetMemorySize write SetMemorySize; 84 property CellSize: Integer read FCellSize write FCellSize; 79 85 end; 80 86 … … 88 94 SJumpTableInsistent = 'Jump table is inconsistent'; 89 95 SJumpTableColision = 'Jump table colision'; 96 SMemoryCellOutOfRange = 'Memory cell %s value out of range'; 90 97 91 98 { TBrainFuckInterpretterThread } … … 128 135 end; 129 136 137 function TBrainFuckInterpretter.GetMemorySize: Integer; 138 begin 139 Result := Length(Memory); 140 end; 141 142 procedure TBrainFuckInterpretter.SetMemorySize(AValue: Integer); 143 begin 144 SetLength(Memory, AValue); 145 end; 146 130 147 procedure TBrainFuckInterpretter.SetSource(AValue: string); 131 148 var … … 140 157 function TBrainFuckInterpretter.Read: Byte; 141 158 begin 142 while InputPosition >= Length(Input) do begin159 while (InputPosition >= Length(Input)) and (FState <> rsStopped) do begin 143 160 Sleep(1); 144 161 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; 166 end; 167 168 function TBrainFuckInterpretter.TryRead(Output: Byte): Boolean; 169 begin 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; 147 176 end; 148 177 … … 217 246 begin 218 247 case ReadCode of 219 '>': if MemoryPosition < Length(Memory)then Inc(MemoryPosition)248 '>': if MemoryPosition < MemorySize then Inc(MemoryPosition) 220 249 else raise Exception.Create(SProgramUpperLimit); 221 250 '<': if MemoryPosition > 0 then Dec(MemoryPosition) 222 251 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); 225 254 '.': Write(Memory[MemoryPosition]); 226 255 ',': Memory[MemoryPosition] := Read; … … 282 311 constructor TBrainFuckInterpretter.Create; 283 312 begin 284 SetLength(Memory, 32000); 313 MemorySize := 30000; 314 CellSize := 256; 285 315 end; 286 316 287 317 destructor TBrainFuckInterpretter.Destroy; 288 318 begin 319 FState := rsStopped; 289 320 SetThread(False); 290 321 inherited Destroy;
Note:
See TracChangeset
for help on using the changeset viewer.