Changeset 17 for trunk/UBrainFuck.pas
- Timestamp:
- Feb 11, 2012, 7:41:10 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UBrainFuck.pas
r14 r17 56 56 procedure Write(Value: Byte); 57 57 function Read: Byte; 58 function TryRead(Output: Byte): Boolean;59 function ReadCode: Char;60 58 procedure SetThread(State: Boolean); 61 59 procedure PrepareJumpTable; 60 procedure RemoveBlankCharacters; 62 61 public 63 62 FSource: array of Char; … … 68 67 MemoryPosition: Integer; 69 68 Output: string; 69 OutputPosition: Integer; 70 70 Input: string; 71 71 InputPosition: Integer; … … 115 115 procedure TBrainFuckInterpretter.Write(Value: Byte); 116 116 begin 117 Output := Output + Char(Value); 117 if OutputPosition > Length(Output) then 118 SetLength(Output, Length(Output) + 1 + Length(Output) div 4); 119 Output[OutputPosition] := Char(Value); 120 Inc(OutputPosition); 118 121 end; 119 122 … … 157 160 function TBrainFuckInterpretter.Read: Byte; 158 161 begin 159 while (InputPosition > =Length(Input)) and (FState <> rsStopped) do begin162 while (InputPosition > Length(Input)) and (FState <> rsStopped) do begin 160 163 Sleep(1); 161 164 end; 162 if InputPosition < Length(Input) then begin163 Result := Ord(Input[InputPosition + 1]);165 if InputPosition <= Length(Input) then begin 166 Result := Ord(Input[InputPosition]); 164 167 Inc(InputPosition); 165 168 end else Result := 0; 166 end;167 168 function TBrainFuckInterpretter.TryRead(Output: Byte): Boolean;169 begin170 if InputPosition >= Length(Input) then Result := False171 else begin172 Output := Ord(Input[InputPosition + 1]);173 Inc(InputPosition);174 Result := True;175 end;176 end;177 178 function TBrainFuckInterpretter.ReadCode: Char;179 begin180 Result := FSource[SourcePosition];181 169 end; 182 170 … … 224 212 end; 225 213 214 procedure TBrainFuckInterpretter.RemoveBlankCharacters; 215 var 216 I: Integer; 217 LastChar: Integer; 218 begin 219 LastChar := 0; 220 for I := 0 to Length(FSource) - 1 do 221 if FSource[I] in ['+','-','>','<','.',',','[',']'] then begin 222 FSource[LastChar] := FSource[I]; 223 Inc(LastChar); 224 end; 225 SetLength(FSource, LastChar); 226 end; 227 226 228 procedure TBrainFuckInterpretter.Reset; 227 229 var 228 230 I: Integer; 229 231 begin 232 RemoveBlankCharacters; 230 233 PrepareJumpTable; 231 234 SourcePosition := 0; 232 InputPosition := 0;235 InputPosition := 1; 233 236 Output := ''; 237 OutputPosition := 1; 234 238 MemoryPosition := 0; 235 239 //FillChar(Pointer(Memory)^, Length(Memory), 0); … … 245 249 NewPos: Integer; 246 250 begin 247 case ReadCodeof251 case FSource[SourcePosition] of 248 252 '>': if MemoryPosition < MemorySize then Inc(MemoryPosition) 249 253 else raise Exception.Create(SProgramUpperLimit);
Note:
See TracChangeset
for help on using the changeset viewer.