Changeset 33 for trunk/Target/UTargetInterpretter.pas
- Timestamp:
- Feb 18, 2012, 11:08:44 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Target/UTargetInterpretter.pas
r32 r33 18 18 end; 19 19 20 TBrainFuckCommand = (cmNo ne, cmInc, cmDec, cmPointerInc, cmPointerDec,21 cmOutput, cmInput, cmLoopStart, cmLoopEnd );20 TBrainFuckCommand = (cmNoOperation, cmInc, cmDec, cmPointerInc, cmPointerDec, 21 cmOutput, cmInput, cmLoopStart, cmLoopEnd, cmDebug); 22 22 23 23 TCommandHandler = procedure of object; … … 45 45 procedure CommandLoopStart; 46 46 procedure CommandLoopEnd; 47 procedure SingleStep; 48 procedure Reset; 47 49 protected 48 procedure SetSource(AValue: string); override;50 function GetTargetCode: string; override; 49 51 public 50 F Source: array of TBrainFuckCommand;52 FProgram: array of TBrainFuckCommand; 51 53 SourceJump: array of Integer; 52 54 SourcePosition: Integer; … … 58 60 Input: string; 59 61 InputPosition: Integer; 60 procedure Reset; 61 procedure SingleStep; 62 procedure Compile; override; 62 63 procedure Run; override; 63 64 procedure Pause; override; … … 72 73 end; 73 74 75 const 76 BrainFuckCommandText: array[TBrainFuckCommand] of Char = ( 77 ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@'); 78 74 79 75 80 implementation … … 88 93 begin 89 94 repeat 90 while (Parent.SourcePosition < Length(Parent.F Source)) and (Parent.State <> rsStopped) do begin95 while (Parent.SourcePosition < Length(Parent.FProgram)) and (Parent.State <> rsStopped) do begin 91 96 Parent.SingleStep; 92 97 while Parent.State = rsPaused do begin … … 117 122 end; 118 123 119 procedure TTargetInterpretter. SetSource(AValue: string);124 procedure TTargetInterpretter.Compile; 120 125 var 121 126 I: Integer; 122 127 Pos: Integer; 123 128 begin 124 SetLength(FSource, Length(AValue)); 129 DebugSteps.Clear; 130 SetLength(FProgram, Length(FSourceCode)); 125 131 Pos := 0; 126 for I := 1 to Length(AValue) do begin 127 case AValue[I] of 128 '+': FSource[Pos] := cmInc; 129 '-': FSource[Pos] := cmDec; 130 '>': FSource[Pos] := cmPointerInc; 131 '<': FSource[Pos] := cmPointerDec; 132 ',': FSource[Pos] := cmInput; 133 '.': FSource[Pos] := cmOutput; 134 '[': FSource[Pos] := cmLoopStart; 135 ']': FSource[Pos] := cmLoopEnd; 132 for I := 1 to Length(FSourceCode) do begin 133 case FSourceCode[I] of 134 '+': begin 135 FProgram[Pos] := cmInc; 136 DebugSteps.AddStep(I - 1, Pos, soNormal); 137 end; 138 '-': begin 139 FProgram[Pos] := cmDec; 140 DebugSteps.AddStep(I - 1, Pos, soNormal); 141 end; 142 '>': begin 143 FProgram[Pos] := cmPointerInc; 144 DebugSteps.AddStep(I - 1, Pos, soNormal); 145 end; 146 '<': begin 147 FProgram[Pos] := cmPointerDec; 148 DebugSteps.AddStep(I - 1, Pos, soNormal); 149 end; 150 ',': begin 151 FProgram[Pos] := cmInput; 152 DebugSteps.AddStep(I - 1, Pos, soNormal); 153 end; 154 '.': begin 155 FProgram[Pos] := cmOutput; 156 DebugSteps.AddStep(I - 1, Pos, soNormal); 157 end; 158 '[': begin 159 FProgram[Pos] := cmLoopStart; 160 DebugSteps.AddStep(I - 1, Pos, soStepIn); 161 end; 162 ']': begin 163 FProgram[Pos] := cmLoopEnd; 164 DebugSteps.AddStep(I - 1, Pos, soStepOut); 165 end 136 166 else Dec(Pos); 137 167 end; 138 168 Inc(Pos); 139 169 end; 140 SetLength(F Source, Pos);170 SetLength(FProgram, Pos); 141 171 end; 142 172 … … 160 190 I: Integer; 161 191 begin 162 SetLength(SourceJump, Length(F Source));192 SetLength(SourceJump, Length(FProgram)); 163 193 //FillChar(Pointer(SourceJump)^, Length(SourceJump), 0); 164 for I := 0 to Length(F Source) - 1 do194 for I := 0 to Length(FProgram) - 1 do 165 195 SourceJump[I] := 0; 166 196 SetLength(Loop, 0); 167 for I := 0 to Length(F Source) - 1 do begin168 case F Source[I] of197 for I := 0 to Length(FProgram) - 1 do begin 198 case FProgram[I] of 169 199 cmLoopStart: begin 170 200 SetLength(Loop, Length(Loop) + 1); … … 252 282 end; 253 283 284 function TTargetInterpretter.GetTargetCode: string; 285 var 286 I: Integer; 287 begin 288 SetLength(Result, Length(FProgram)); 289 for I := 0 to Length(FProgram) - 1 do 290 Result[I + 1] := BrainFuckCommandText[FProgram[I]]; 291 end; 292 254 293 procedure TTargetInterpretter.SingleStep; 255 294 begin 256 FCommandTable[F Source[SourcePosition]];295 FCommandTable[FProgram[SourcePosition]]; 257 296 Inc(SourcePosition); 258 297 Inc(FStepCount); … … 281 320 inherited; 282 321 Name := 'Interpretter'; 283 Capabilities := [tcRun, tcPause, tcStop]; 322 Capabilities := [tcRun, tcPause, tcStop, tcCompile, tcStepOut, tcStepInto, 323 tcStepOver, tcRunToCursor]; 284 324 MemorySize := 30000; 285 325 CellSize := 256;
Note:
See TracChangeset
for help on using the changeset viewer.