Changeset 33 for trunk/Target
- Timestamp:
- Feb 18, 2012, 11:08:44 PM (13 years ago)
- Location:
- trunk/Target
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Target/UTarget.pas
r32 r33 18 18 TRunState = (rsStopped, rsPaused, rsRunning); 19 19 20 TStepOperation = (soNormal, soStepIn, soStepOut); 21 22 TDebugStep = class 23 SourcePosition: Integer; 24 TargetPosition: Integer; 25 Operation: TStepOperation; 26 end; 27 28 { TDebugStepList } 29 30 TDebugStepList = class(TListObject) 31 function SearchBySourcePos(Pos: Integer): TDebugStep; 32 function SearchByTargetPos(Pos: Integer): TDebugStep; 33 procedure AddStep(SourcePos, TargetPos: Integer; Operation: TStepOperation); 34 end; 35 20 36 { TTarget } 21 37 22 38 TTarget = class 23 39 private 40 FCompiled: Boolean; 24 41 protected 25 FSource: string; 42 FSourceCode: string; 43 FTargetCode: string; 26 44 Indent: Integer; 27 45 FState: TRunState; 28 46 FOnChangeState: TNotifyEvent; 29 procedure SetSource(AValue: string); virtual; 47 procedure SetSourceCode(AValue: string); virtual; 48 function GetTargetCode: string; virtual; 30 49 procedure AddLine(Text: string); 31 50 function LongFileName(FileName: string): string; … … 33 52 Name: string; 34 53 ProgramName: string; 35 Output: string;36 54 Optimization: TCompilerOptimization; 37 55 CompilerPath: string; … … 40 58 ProjectFileName: string; 41 59 Capabilities: TTargetCapabilities; 60 BreakPointers: TListInteger; 61 DebugSteps: TDebugStepList; 42 62 constructor Create; virtual; 63 destructor Destroy; override; 43 64 procedure OptimizeSource; 44 65 procedure Compile; virtual; … … 55 76 property State: TRunState read FState; 56 77 property OnChangeState: TNotifyEvent read FOnChangeState write FOnChangeState; 57 property Source: string write SetSource; 78 property SourceCode: string write SetSourceCode; 79 property TargetCode: string read GetTargetCode; 80 property Compiled: Boolean read FCompiled write FCompiled; 58 81 end; 59 82 … … 73 96 74 97 implementation 98 99 { TDebugStepList } 100 101 function TDebugStepList.SearchBySourcePos(Pos: Integer 102 ): TDebugStep; 103 var 104 I: Integer; 105 begin 106 I := 0; 107 while (I < Count) and (TDebugStep(Items[I]).SourcePosition < Pos) do Inc(I); 108 if I < Count then Result := TDebugStep(Items[I]) 109 else Result := nil; 110 end; 111 112 function TDebugStepList.SearchByTargetPos(Pos: Integer 113 ): TDebugStep; 114 var 115 I: Integer; 116 begin 117 I := 0; 118 while (I < Count) and (TDebugStep(Items[I]).TargetPosition < Pos) do Inc(I); 119 if I < Count then Result := TDebugStep(Items[I]) 120 else Result := nil; 121 end; 122 123 procedure TDebugStepList.AddStep(SourcePos, TargetPos: Integer; 124 Operation: TStepOperation); 125 var 126 NewItem: TDebugStep; 127 begin 128 NewItem := TDebugStep.Create; 129 NewItem.SourcePosition := SourcePos; 130 NewItem.TargetPosition := TargetPos; 131 NewItem.Operation := Operation; 132 Add(NewItem); 133 end; 75 134 76 135 … … 122 181 { TTarget } 123 182 124 procedure TTarget.SetSource(AValue: string); 125 begin 126 FSource := AValue; 183 function TTarget.GetTargetCode: string; 184 begin 185 Result := FTargetCode; 186 end; 187 188 procedure TTarget.SetSourceCode(AValue: string); 189 begin 190 FSourceCode := AValue; 127 191 end; 128 192 129 193 procedure TTarget.AddLine(Text: string); 130 194 begin 131 Output := Output+ DupeString(' ', Indent) + Text + LineEnding;195 FTargetCode := FTargetCode + DupeString(' ', Indent) + Text + LineEnding; 132 196 end; 133 197 … … 145 209 constructor TTarget.Create; 146 210 begin 211 inherited; 147 212 Optimization := coNormal; 213 BreakPointers := TListInteger.Create; 214 DebugSteps := TDebugStepList.Create; 215 end; 216 217 destructor TTarget.Destroy; 218 begin 219 DebugSteps.Free;; 220 BreakPointers.Free; 221 inherited Destroy; 148 222 end; 149 223 … … 156 230 procedure TTarget.Compile; 157 231 begin 158 232 Compiled := True; 159 233 end; 160 234 … … 170 244 with TStringList.Create do 171 245 try 172 Text := Output;246 Text := FTargetCode; 173 247 SaveToFile(CompiledFile); 174 248 finally -
trunk/Target/UTargetC.pas
r32 r33 50 50 Result := 1; 51 51 if Optimization = coNormal then 52 while ((I + 1) <= Length(FSource )) and (FSource[I + 1] = C) do begin52 while ((I + 1) <= Length(FSourceCode)) and (FSourceCode[I + 1] = C) do begin 53 53 Inc(Result); 54 54 Inc(I) … … 58 58 begin 59 59 Indent := 0; 60 Output:= '';60 FTargetCode := ''; 61 61 62 62 AddLine('#include <stdio.h>'); … … 71 71 AddLine('Pos = 0;'); 72 72 I := 1; 73 while (I <= Length(FSource )) do begin74 case FSource [I] of73 while (I <= Length(FSourceCode)) do begin 74 case FSourceCode[I] of 75 75 '>': begin 76 76 Sum := CheckOccurence('>'); … … 119 119 with TStringList.Create do 120 120 try 121 Text := Output;121 Text := FTargetCode; 122 122 SaveToFile(CompiledFile); 123 123 finally -
trunk/Target/UTargetDelphi.pas
r32 r33 44 44 Result := 1; 45 45 if Optimization = coNormal then 46 while ((I + 1) <= Length(FSource )) and (FSource[I + 1] = C) do begin46 while ((I + 1) <= Length(FSourceCode)) and (FSourceCode[I + 1] = C) do begin 47 47 Inc(Result); 48 48 Inc(I) … … 52 52 begin 53 53 Indent := 0; 54 Output:= '';54 FTargetCode := ''; 55 55 56 56 AddLine('program ' + ProgramName + ';'); … … 66 66 AddLine('Pos := 0;'); 67 67 I := 1; 68 while (I <= Length(FSource )) do begin69 case FSource [I] of68 while (I <= Length(FSourceCode)) do begin 69 case FSourceCode[I] of 70 70 '>': begin 71 71 Sum := CheckOccurence('>'); -
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; -
trunk/Target/UTargetPHP.pas
r32 r33 48 48 Result := 1; 49 49 if Optimization = coNormal then 50 while ((I + 1) <= Length(FSource )) and (FSource[I + 1] = C) do begin50 while ((I + 1) <= Length(FSourceCode)) and (FSourceCode[I + 1] = C) do begin 51 51 Inc(Result); 52 52 Inc(I) … … 56 56 begin 57 57 Indent := 0; 58 Output:= '';58 FTargetCode := ''; 59 59 60 60 AddLine('<?php // ' + ProgramName); … … 62 62 AddLine('$Memory = str_repeat("\0", 30000);'); 63 63 AddLine('$Position = 0;'); 64 for I := 1 to Length(FSource ) do begin65 case FSource [I] of64 for I := 1 to Length(FSourceCode) do begin 65 case FSourceCode[I] of 66 66 '>': begin 67 67 Sum := CheckOccurence('>');
Note:
See TracChangeset
for help on using the changeset viewer.