Changeset 3 for trunk/UBrainFuck.pas
- Timestamp:
- Feb 9, 2012, 1:25:43 PM (13 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:ignore
-
old new 1 1 lib 2 LazFuckIDE.exe
-
- Property svn:ignore
-
trunk/UBrainFuck.pas
r2 r3 26 26 function ReadCode: Char; 27 27 public 28 Source: TStrings;28 Source: string; 29 29 SourcePosition: Integer; 30 30 Memory: array of Byte; … … 32 32 Loop: array of Integer; 33 33 LoopCurrent: Integer; 34 Output: TStrings;35 Input: TStrings;34 Output: string; 35 Input: string; 36 36 InputPosition: Integer; 37 37 procedure Reset; … … 42 42 end; 43 43 44 44 45 implementation 46 47 resourcestring 48 SProgramLowerLimit = 'Program run over lower limit'; 49 SProgramUpperLimit = 'Program run over upper limit'; 45 50 46 51 { TBrainFuckInterpreter } … … 48 53 procedure TBrainFuckInterpreter.Write(Value: Byte); 49 54 begin 50 Output .Text := Output.Text + Char(Value);55 Output := Output + Char(Value); 51 56 end; 52 57 … … 55 60 Character: string; 56 61 begin 57 Character := Copy(Input .Text, InputPosition, 1);62 Character := Copy(Input, InputPosition, 1); 58 63 Result := Ord(Character[1]); 59 64 Inc(InputPosition); … … 64 69 Code: string; 65 70 begin 66 Code := Copy(Source .Text, SourcePosition, 1);71 Code := Copy(Source, SourcePosition + 1, 1); 67 72 Result := Code[1]; 68 73 end; … … 74 79 SourcePosition := 0; 75 80 InputPosition := 0; 76 Output .Text:= '';81 Output := ''; 77 82 MemoryPosition := 0; 78 83 for I := 0 to Length(Memory) - 1 do … … 89 94 case ReadCode of 90 95 '>': Inc(MemoryPosition); 91 '<': Dec(MemoryPosition); 96 '<': if MemoryPosition > 0 then Dec(MemoryPosition) 97 else raise Exception.Create(SProgramLowerLimit); 92 98 '+': Memory[MemoryPosition] := Memory[MemoryPosition] + 1; 93 99 '-': Memory[MemoryPosition] := Memory[MemoryPosition] - 1; … … 97 103 if Memory[MemoryPosition] = 0 then begin 98 104 C := 1; 99 Inc( MemoryPosition);105 Inc(SourcePosition); 100 106 while C > 0 do begin 101 107 case ReadCode of … … 103 109 ']': Dec(C); 104 110 end; 105 Inc( MemoryPosition);111 Inc(SourcePosition); 106 112 end; 107 Dec( MemoryPosition);113 Dec(SourcePosition); 108 114 end; 109 115 end; … … 111 117 if Memory[MemoryPosition] > 0 then begin 112 118 C := 1; 113 Dec( MemoryPosition);119 Dec(SourcePosition); 114 120 while C > 0 do begin 115 121 case ReadCode of … … 117 123 '[': Dec(C); 118 124 end; 119 Dec( MemoryPosition);125 Dec(SourcePosition); 120 126 end; 121 127 end; 122 128 end; 123 129 end; 124 Inc( MemoryPosition);130 Inc(SourcePosition); 125 131 end; 126 132 … … 128 134 begin 129 135 Reset; 130 while SourcePosition < Length(Source .Text) do136 while SourcePosition < Length(Source) do 131 137 SingleStep; 132 138 end;
Note:
See TracChangeset
for help on using the changeset viewer.