Changeset 64 for trunk/Target
- Timestamp:
- Dec 4, 2014, 2:59:28 PM (10 years ago)
- Location:
- trunk/Target
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Target/UTargetC.pas
r60 r64 65 65 FProgramIndex := 0; 66 66 while (FProgramIndex < Length(FProgram)) do begin 67 case FProgram[FProgramIndex] of 68 cmPointerInc: begin 69 Sum := CheckOccurence(cmPointerInc); 70 AddLine('Pos = Pos + ' + IntToStr(Sum) + ';'); 71 end; 72 cmPointerDec: begin 73 Sum := CheckOccurence(cmPointerDec); 74 AddLine('Pos = Pos - ' + IntToStr(Sum) + ';'); 75 end; 76 cmInc: begin 77 Sum := CheckOccurence(cmInc); 78 AddLine('Memory[Pos] = Memory[Pos] + ' + IntToStr(Sum) + ';'); 79 end; 80 cmDec: begin 81 Sum := CheckOccurence(cmDec); 82 AddLine('Memory[Pos] = Memory[Pos] - ' + IntToStr(Sum) + ';'); 83 end; 67 case FProgram[FProgramIndex].Command of 68 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 69 cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 70 cmInc: AddLine('Memory[Pos] = Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 71 cmDec: AddLine('Memory[Pos] = Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 84 72 cmOutput: AddLine('putchar(Memory[Pos]);'); 85 73 cmInput: AddLine('Memory[Pos] = getchar();'); 74 cmSet: AddLine('Memory[Pos] = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 86 75 cmLoopStart: begin 87 if CheckClear then begin 88 AddLine('Memory[Pos] = 0;'); 89 Inc(FProgramIndex, 2); 90 end else begin 91 AddLine('while(Memory[Pos] != 0)'); 92 AddLine('{'); 93 Inc(Indent); 94 end; 76 AddLine('while(Memory[Pos] != 0)'); 77 AddLine('{'); 78 Inc(Indent); 95 79 end; 96 80 cmLoopEnd: begin -
trunk/Target/UTargetDelphi.pas
r60 r64 59 59 FProgramIndex := 0; 60 60 while (FProgramIndex < Length(FProgram)) do begin 61 case FProgram[FProgramIndex] of 62 cmPointerInc: begin 63 Sum := CheckOccurence(cmPointerInc); 64 AddLine('Inc(Pos, ' + IntToStr(Sum) + ');'); 65 end; 66 cmPointerDec: begin 67 Sum := CheckOccurence(cmPointerDec); 68 AddLine('Dec(Pos, ' + IntToStr(Sum) + ');'); 69 end; 70 cmInc: begin 71 Sum := CheckOccurence(cmInc); 72 AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(Sum) + ';'); 73 end; 74 cmDec: begin 75 Sum := CheckOccurence(cmDec); 76 AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(Sum) + ';'); 77 end; 61 case FProgram[FProgramIndex].Command of 62 cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 63 cmPointerDec: AddLine('Dec(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 64 cmInc: AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 65 cmDec: AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 66 cmSet: AddLine('Memory[Pos] := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 78 67 cmOutput: AddLine('Write(Chr(Memory[Pos]));'); 79 68 cmInput: AddLine('Read(ReadChar); Memory[Pos] := Ord(ReadChar);'); 80 69 cmLoopStart: begin 81 if CheckClear then begin 82 AddLine('Memory[Pos] := 0;'); 83 Inc(FProgramIndex, 2); 84 end else begin 85 AddLine('while Memory[Pos] <> 0 do begin'); 86 Inc(Indent); 87 end; 70 AddLine('while Memory[Pos] <> 0 do begin'); 71 Inc(Indent); 88 72 end; 89 73 cmLoopEnd: begin -
trunk/Target/UTargetFPC.pas
r60 r64 44 44 45 45 procedure TTargetFPC.Compile; 46 var47 Sum: Integer;48 46 begin 49 47 inherited; … … 62 60 FProgramIndex := 0; 63 61 while (FProgramIndex < Length(FProgram)) do begin 64 case FProgram[FProgramIndex] of 65 cmPointerInc: begin 66 Sum := CheckOccurence(cmPointerInc); 67 AddLine('Inc(Pos, ' + IntToStr(Sum) + ');'); 68 end; 69 cmPointerDec: begin 70 Sum := CheckOccurence(cmPointerDec); 71 AddLine('Dec(Pos, ' + IntToStr(Sum) + ');'); 72 end; 73 cmInc: begin 74 Sum := CheckOccurence(cmInc); 75 AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(Sum) + ';'); 76 end; 77 cmDec: begin 78 Sum := CheckOccurence(cmDec); 79 AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(Sum) + ';'); 80 end; 62 case FProgram[FProgramIndex].Command of 63 cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 64 cmPointerDec: AddLine('Dec(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 65 cmInc: AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 66 cmDec: AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 67 cmSet: AddLine('Memory[Pos] := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 81 68 cmOutput: AddLine('Write(Chr(Memory[Pos]));'); 82 69 cmInput: AddLine('Read(ReadChar); Memory[Pos] := Ord(ReadChar);'); 83 70 cmLoopStart: begin 84 if CheckClear then begin 85 AddLine('Memory[Pos] := 0;'); 86 Inc(FProgramIndex, 2); 87 end else begin 88 AddLine('while Memory[Pos] <> 0 do begin'); 89 Inc(Indent); 90 end; 71 AddLine('while Memory[Pos] <> 0 do begin'); 72 Inc(Indent); 91 73 end; 92 74 cmLoopEnd: begin -
trunk/Target/UTargetInterpretter.pas
r52 r64 33 33 FThread: TTargetInterpretterThread; 34 34 FStepCount: Integer; 35 FCommandTable: array[T BrainFuckCommand] of TCommandHandler;35 FCommandTable: array[TMachineCommand] of TCommandHandler; 36 36 function GetMemorySize: Integer; 37 37 procedure SetMemorySize(AValue: Integer); … … 46 46 procedure CommandLoopStart; 47 47 procedure CommandLoopEnd; 48 procedure CommandSet; 48 49 procedure PrepareBreakPoints; 49 50 protected … … 81 82 82 83 const 83 BrainFuckCommandText: array[T BrainFuckCommand] of Char = (84 ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@' );84 BrainFuckCommandText: array[TMachineCommand] of Char = ( 85 ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@', '='); 85 86 86 87 … … 95 96 SMemoryCellOutOfRange = 'Memory cell %s value out of range'; 96 97 SProgramNotRunning = 'Program not running'; 98 SUnsupportedCommand = 'Unsupported command'; 97 99 98 100 { TTargetInterpretterThread } … … 111 113 SetStateSafe(rsPaused); 112 114 end else begin 113 FCommandTable[FProgram[FProgramIndex]]; 115 if Assigned(FCommandTable[FProgram[FProgramIndex].Command]) then 116 FCommandTable[FProgram[FProgramIndex].Command] 117 else raise Exception.Create(SUnsupportedCommand); 114 118 Inc(FProgramIndex); 115 119 Inc(FStepCount); … … 177 181 SetLength(Loop, 0); 178 182 for I := 0 to Length(FProgram) - 1 do begin 179 case FProgram[I] of183 case FProgram[I].Command of 180 184 cmLoopStart: begin 181 185 SetLength(Loop, Length(Loop) + 1); … … 194 198 end; 195 199 196 procedure TTargetInterpretter.CommandInc;197 begin198 Memory[MemoryPosition] := ((Memory[MemoryPosition] + 1) mod CellSize);199 end;200 201 procedure TTargetInterpretter.CommandDec;202 begin203 Memory[MemoryPosition] := ((Memory[MemoryPosition] - 1) mod CellSize);204 end;205 206 procedure TTargetInterpretter.CommandPointerInc;207 begin208 if MemoryPosition < MemorySize then Inc(MemoryPosition)209 else raise Exception.Create(SProgramUpperLimit);210 end;211 212 procedure TTargetInterpretter.CommandPointerDec;213 begin214 if MemoryPosition > 0 then Dec(MemoryPosition)215 else raise Exception.Create(SProgramLowerLimit);216 end;217 218 200 procedure TTargetInterpretter.CommandInput; 219 201 begin … … 245 227 if Memory[MemoryPosition] > 0 then 246 228 FProgramIndex := SourceJump[FProgramIndex] - 1; 229 end; 230 231 procedure TTargetInterpretter.CommandInc; 232 begin 233 Memory[MemoryPosition] := ((Memory[MemoryPosition] + FProgram[FProgramIndex].Parameter) mod CellSize); 234 end; 235 236 procedure TTargetInterpretter.CommandDec; 237 begin 238 Memory[MemoryPosition] := ((Memory[MemoryPosition] - FProgram[FProgramIndex].Parameter) mod CellSize); 239 end; 240 241 procedure TTargetInterpretter.CommandPointerInc; 242 begin 243 if MemoryPosition < MemorySize then Inc(MemoryPosition, FProgram[FProgramIndex].Parameter) 244 else raise Exception.Create(SProgramUpperLimit); 245 end; 246 247 procedure TTargetInterpretter.CommandPointerDec; 248 begin 249 if MemoryPosition > 0 then Dec(MemoryPosition, FProgram[FProgramIndex].Parameter) 250 else raise Exception.Create(SProgramLowerLimit); 251 end; 252 253 procedure TTargetInterpretter.CommandSet; 254 begin 255 Memory[MemoryPosition] := FProgram[FProgramIndex].Parameter mod CellSize; 247 256 end; 248 257 … … 286 295 I: Integer; 287 296 begin 288 SetLength(Result, Length(FProgram)); 289 for I := 0 to Length(FProgram) - 1 do 290 Result[I + 1] := BrainFuckCommandText[FProgram[I]]; 297 Result := ''; 298 for I := 0 to Length(FProgram) - 1 do begin 299 Result := Result + BrainFuckCommandText[FProgram[I].Command]; 300 if FProgram[I].Command in [cmInc, cmDec, cmSet, cmPointerInc, cmPointerDec] then begin 301 if FProgram[I].Parameter > 1 then 302 Result := Result + IntToStr(FProgram[I].Parameter); 303 end; 304 end; 291 305 end; 292 306 … … 402 416 MemorySize := 30000; 403 417 CellSize := 256; 418 // Base commands 404 419 FCommandTable[cmInc] := CommandInc; 405 420 FCommandTable[cmDec] := CommandDec; … … 410 425 FCommandTable[cmLoopStart] := CommandLoopStart; 411 426 FCommandTable[cmLoopEnd] := CommandLoopEnd; 427 // Extended commands 428 FCommandTable[cmSet] := CommandSet; 412 429 end; 413 430 -
trunk/Target/UTargetJava.pas
r60 r64 66 66 FProgramIndex := 0; 67 67 while (FProgramIndex < Length(FProgram)) do begin 68 case FProgram[FProgramIndex] of 69 cmPointerInc: begin 70 Sum := CheckOccurence(cmPointerInc); 71 AddLine('Pos = Pos + ' + IntToStr(Sum) + ';'); 72 end; 73 cmPointerDec: begin 74 Sum := CheckOccurence(cmPointerDec); 75 AddLine('Pos = Pos - ' + IntToStr(Sum) + ';'); 76 end; 77 cmInc: begin 78 Sum := CheckOccurence(cmInc); 79 AddLine('Memory[Pos] = (char)((int)Memory[Pos] + ' + IntToStr(Sum) + ');'); 80 end; 81 cmDec: begin 82 Sum := CheckOccurence(cmDec); 83 AddLine('Memory[Pos] = (char)((int)Memory[Pos] - ' + IntToStr(Sum) + ');'); 84 end; 68 case FProgram[FProgramIndex].Command of 69 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 70 cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 71 cmInc: AddLine('Memory[Pos] = (char)((int)Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 72 cmDec: AddLine('Memory[Pos] = (char)((int)Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 85 73 cmOutput: AddLine('System.out.print(Memory[Pos]);'); 86 74 cmInput: AddLine('Memory[Pos] = (char)System.in.read();'); 75 cmSet: AddLine('Memory[Pos] = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 87 76 cmLoopStart: begin 88 if CheckClear then begin 89 AddLine('Memory[Pos] = 0;'); 90 Inc(FProgramIndex, 2); 91 end else begin 92 AddLine('while(Memory[Pos] != 0)'); 93 AddLine('{'); 94 Inc(Indent); 95 end; 77 AddLine('while(Memory[Pos] != 0)'); 78 AddLine('{'); 79 Inc(Indent); 96 80 end; 97 81 cmLoopEnd: begin -
trunk/Target/UTargetPHP.pas
r60 r64 58 58 FProgramIndex := 0; 59 59 while (FProgramIndex < Length(FProgram)) do begin 60 case FProgram[FProgramIndex] of 61 cmPointerInc: begin 62 Sum := CheckOccurence(cmPointerInc); 63 AddLine('$Position = $Position + ' + IntToStr(Sum) + ';'); 64 end; 65 cmPointerDec: begin 66 Sum := CheckOccurence(cmPointerDec); 67 AddLine('$Position = $Position - ' + IntToStr(Sum) + ';'); 68 end; 69 cmInc: begin 70 Sum := CheckOccurence(cmInc); 71 AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) + ' + IntToStr(Sum) + ');'); 72 end; 73 cmDec: begin 74 Sum := CheckOccurence(cmDec); 75 AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) - ' + IntToStr(Sum) + ');'); 76 end; 60 case FProgram[FProgramIndex].Command of 61 cmPointerInc: AddLine('$Position = $Position + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 62 cmPointerDec: AddLine('$Position = $Position - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 63 cmInc: AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 64 cmDec: AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 77 65 cmOutput: AddLine('echo($Memory[$Position]);'); 78 66 cmInput: AddLine('$Memory[$Position] = fgetc(STDIN);'); 67 cmSet: AddLine('$Memory[$Position] = chr(' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 79 68 cmLoopStart: begin 80 if CheckClear then begin 81 AddLine('$Memory[$Position] = "\0";'); 82 Inc(FProgramIndex, 2); 83 end else begin 84 AddLine('while($Memory[$Position] != "\0") {'); 85 Inc(Indent); 86 end; 69 AddLine('while($Memory[$Position] != "\0") {'); 70 Inc(Indent); 87 71 end; 88 72 cmLoopEnd: begin
Note:
See TracChangeset
for help on using the changeset viewer.