Changeset 12 for trunk/UBrainFuck.pas
- Timestamp:
- Feb 11, 2012, 4:32:27 PM (13 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:ignore
-
old new 3 3 LazFuckIDE.lps 4 4 backup 5 LazFuckIDE
-
- Property svn:ignore
-
trunk/UBrainFuck.pas
r10 r12 12 12 13 13 TCompilerTarget = (ctDelphi); 14 TCompilerOptimization = (coNone, coNormal); 15 14 16 { TBrainFuckCompiler } 15 17 … … 23 25 Output: string; 24 26 Target: TCompilerTarget; 27 Optimization: TCompilerOptimization; 28 procedure OptimizeSource; 25 29 procedure Compile; 26 30 end; … … 43 47 FThreadState: Boolean; 44 48 FThread: TBrainFuckInterpretterThread; 49 FStepCount: Integer; 45 50 procedure SetState(AValue: TRunState); 46 51 procedure Write(Value: Byte); … … 48 53 function ReadCode: Char; 49 54 procedure SetThread(State: Boolean); 55 procedure PrepareJumpTable; 50 56 public 51 Source: string; 57 Source: array of Char; 58 SourceJump: array of Integer; 52 59 SourcePosition: Integer; 53 60 Memory: array of Byte; 54 61 MemoryPosition: Integer; 55 Loop: array of Integer;56 LoopCurrent: Integer;57 62 Output: string; 58 63 Input: string; 59 64 InputPosition: Integer; 60 StepCount: Integer;61 65 procedure Reset; 62 66 procedure SingleStep; … … 68 72 property State: TRunState read FState; 69 73 property OnChangeState: TNotifyEvent read FOnChangeState write FOnChangeState; 74 property StepCount: Integer read FStepCount; 70 75 end; 71 76 … … 77 82 SProgramUpperLimit = 'Program run over upper limit'; 78 83 SReadInputError = 'Read input error'; 84 SJumpTableInsistent = 'Jump table is inconsistent'; 85 SJumpTableColision = 'Jump table colision'; 79 86 80 87 { TBrainFuckInterpretterThread } … … 118 125 function TBrainFuckInterpretter.ReadCode: Char; 119 126 begin 120 Result := Source[SourcePosition + 1]127 Result := Source[SourcePosition]; 121 128 end; 122 129 … … 135 142 end; 136 143 144 procedure TBrainFuckInterpretter.PrepareJumpTable; 145 var 146 Loop: array of Integer; 147 LoopCurrent: Integer; 148 I: Integer; 149 begin 150 SetLength(SourceJump, Length(Source)); 151 FillChar(Pointer(SourceJump)^, Length(SourceJump), 0); 152 SetLength(Loop, 0); 153 for I := 0 to Length(Source) - 1 do begin 154 case Source[I] of 155 '[': begin 156 SetLength(Loop, Length(Loop) + 1); 157 Loop[High(Loop)] := I; 158 end; 159 ']': begin 160 if SourceJump[I] > 0 then raise Exception.Create(SJumpTableColision); 161 SourceJump[I] := Loop[High(Loop)]; 162 if SourceJump[Loop[High(Loop)]] > 0 then raise Exception.Create(SJumpTableColision); 163 SourceJump[Loop[High(Loop)]] := I; 164 SetLength(Loop, Length(Loop) - 1); 165 end; 166 end; 167 end; 168 if Length(Loop) > 0 then raise Exception.Create(SJumpTableInsistent); 169 end; 170 137 171 procedure TBrainFuckInterpretter.Reset; 138 var 139 I: Integer; 140 begin 172 begin 173 PrepareJumpTable; 141 174 SourcePosition := 0; 142 175 InputPosition := 0; 143 176 Output := ''; 144 177 MemoryPosition := 0; 145 for I := 0 to Length(Memory) - 1 do 146 Memory[I] := 0; 147 SetLength(Loop, 0); 148 StepCount := 0; 178 FillChar(Pointer(Memory)^, Length(Memory), 0); 179 FStepCount := 0; 149 180 end; 150 181 … … 152 183 var 153 184 CodeText: string; 154 Code: Char;155 185 C: Integer; 186 NewPos: Integer; 156 187 begin 157 188 case ReadCode of … … 166 197 '[': begin 167 198 if Memory[MemoryPosition] = 0 then begin 168 C := 1; 199 SourcePosition := SourceJump[SourcePosition]; 200 (*C := 1; 169 201 Inc(SourcePosition); 170 202 while C > 0 do begin … … 175 207 Inc(SourcePosition); 176 208 end; 177 Dec(SourcePosition); 209 Dec(SourcePosition);*) 210 //if NewPos <> SourcePosition then raise Exception.Create('Wrong pos: ' + IntToStr(SourcePosition) + ' ' + IntToStr(NewPos)); 178 211 end; 179 212 end; 180 213 ']': begin 181 214 if Memory[MemoryPosition] > 0 then begin 182 C := 1; 215 SourcePosition := SourceJump[SourcePosition] - 1; 216 (*C := 1; 183 217 Dec(SourcePosition); 184 218 while C > 0 do begin … … 189 223 Dec(SourcePosition); 190 224 end; 225 if NewPos <> SourcePosition then raise Exception.Create('Wrong pos: ' + IntToStr(SourcePosition) + ' ' + IntToStr(NewPos)); 226 *) 191 227 end; 192 228 end; 193 229 end; 194 230 Inc(SourcePosition); 195 Inc( StepCount);231 Inc(FStepCount); 196 232 end; 197 233 … … 230 266 begin 231 267 Output := Output + DupeString(' ', Indent) + Text + LineEnding; 268 end; 269 270 procedure TBrainFuckCompiler.OptimizeSource; 271 begin 272 // Remove redundand code 273 232 274 end; 233 275
Note:
See TracChangeset
for help on using the changeset viewer.