Changeset 86 for trunk/Target
- Timestamp:
- Aug 29, 2017, 5:12:18 PM (7 years ago)
- Location:
- trunk/Target
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Target/UTargetC.pas
r72 r86 15 15 TTargetC = class(TBFTarget) 16 16 private 17 function GetMemoryCell: string; 17 18 public 18 19 constructor Create; override; … … 46 47 end; 47 48 49 function TTargetC.GetMemoryCell: string; 50 begin 51 Result := 'Memory[Pos'; 52 if FProgram[FProgramIndex].RelIndex > 0 then 53 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex) 54 else if FProgram[FProgramIndex].RelIndex < 0 then 55 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex)); 56 Result := Result + ']'; 57 end; 58 48 59 procedure TTargetC.Compile; 49 60 begin … … 67 78 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 68 79 cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 69 cmInc: AddLine('Memory[Pos] = Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 70 cmDec: AddLine('Memory[Pos] = Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 71 cmOutput: AddLine('putchar(Memory[Pos]);'); 72 cmInput: AddLine('Memory[Pos] = getchar();'); 73 cmSet: AddLine('Memory[Pos] = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 80 cmInc: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 81 cmDec: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 82 cmOutput: AddLine('putchar(' + GetMemoryCell + ');'); 83 cmInput: AddLine(GetMemoryCell + ' = getchar();'); 84 cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 85 cmMultipy: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 74 86 cmLoopStart: begin 75 AddLine('while( Memory[Pos]!= 0)');87 AddLine('while(' + GetMemoryCell + ' != 0)'); 76 88 AddLine('{'); 77 89 Inc(Indent); -
trunk/Target/UTargetDelphi.pas
r72 r86 14 14 TTargetDelphi = class(TBFTarget) 15 15 private 16 function GetMemoryCell: string; 16 17 public 17 18 constructor Create; override; … … 38 39 end; 39 40 41 function TTargetDelphi.GetMemoryCell: string; 42 begin 43 Result := 'Memory[Pos'; 44 if FProgram[FProgramIndex].RelIndex > 0 then 45 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex) 46 else if FProgram[FProgramIndex].RelIndex < 0 then 47 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex)); 48 Result := Result + ']'; 49 end; 50 40 51 procedure TTargetDelphi.Compile; 41 52 begin … … 60 71 cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 61 72 cmPointerDec: AddLine('Dec(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 62 cmInc: AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 63 cmDec: AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 64 cmSet: AddLine('Memory[Pos] := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 65 cmOutput: AddLine('Write(Chr(Memory[Pos]));'); 66 cmInput: AddLine('Read(ReadChar); Memory[Pos] := Ord(ReadChar);'); 73 cmInc: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 74 cmDec: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 75 cmSet: AddLine(GetMemoryCell + ' := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 76 cmMultipy: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 77 cmOutput: AddLine('Write(Chr(' + GetMemoryCell + '));'); 78 cmInput: AddLine('Read(ReadChar); ' + GetMemoryCell + ' := Ord(ReadChar);'); 67 79 cmLoopStart: begin 68 AddLine('while Memory[Pos]<> 0 do begin');80 AddLine('while ' + GetMemoryCell + ' <> 0 do begin'); 69 81 Inc(Indent); 70 82 end; -
trunk/Target/UTargetFPC.pas
r66 r86 14 14 TTargetFPC = class(TBFTarget) 15 15 private 16 function GetMemoryCell: string; 16 17 public 17 18 constructor Create; override; … … 43 44 end; 44 45 46 function TTargetFPC.GetMemoryCell: string; 47 begin 48 Result := 'Memory[Pos'; 49 if FProgram[FProgramIndex].RelIndex > 0 then 50 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex) 51 else if FProgram[FProgramIndex].RelIndex < 0 then 52 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex)); 53 Result := Result + ']'; 54 end; 55 45 56 procedure TTargetFPC.Compile; 46 57 begin … … 50 61 51 62 AddLine('program ' + ProgramName + ';'); 52 AddLine('');53 63 AddLine('var'); 54 64 AddLine(' Memory: array[0..30000] of Byte;'); … … 63 73 cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 64 74 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) + ';'); 68 cmOutput: AddLine('Write(Chr(Memory[Pos]));'); 69 cmInput: AddLine('Read(ReadChar); Memory[Pos] := Ord(ReadChar);'); 75 cmInc: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 76 cmDec: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 77 cmSet: AddLine(GetMemoryCell + ' := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 78 cmMultipy: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 79 cmOutput: AddLine('Write(Chr(' + GetMemoryCell + '));'); 80 cmInput: AddLine('Read(ReadChar); ' + GetMemoryCell + ' := Ord(ReadChar);'); 70 81 cmLoopStart: begin 71 AddLine('while Memory[Pos]<> 0 do begin');82 AddLine('while ' + GetMemoryCell + ' <> 0 do begin'); 72 83 Inc(Indent); 73 84 end; -
trunk/Target/UTargetInterpretter.pas
r80 r86 44 44 procedure CommandLoopEnd; 45 45 procedure CommandSet; 46 procedure CommandMultiply; 46 47 procedure PrepareBreakPoints; 47 48 protected … … 75 76 const 76 77 BrainFuckCommandText: array[TMachineCommand] of Char = ( 77 ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@', '=' );78 ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@', '=', '*'); 78 79 79 80 … … 189 190 end; 190 191 if InputPosition <= Length(Input) then begin 191 Memory[MemoryPosition] := Ord(Input[InputPosition]); 192 Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] := 193 Ord(Input[InputPosition]); 192 194 Inc(InputPosition); 193 195 end; … … 198 200 if OutputPosition > Length(Output) then 199 201 SetLength(Output, Length(Output) + 1 + Length(Output) div 4); 200 Output[OutputPosition] := Char(Memory[MemoryPosition]); 202 Output[OutputPosition] := Char(Memory[MemoryPosition + 203 FProgram[FProgramIndex].RelIndex]); 201 204 Inc(OutputPosition); 202 205 end; … … 204 207 procedure TTargetInterpretter.CommandLoopStart; 205 208 begin 206 if Memory[MemoryPosition ] = 0 then209 if Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] = 0 then 207 210 FProgramIndex := FProgram[FProgramIndex].Parameter; 208 211 end; … … 210 213 procedure TTargetInterpretter.CommandLoopEnd; 211 214 begin 212 if Memory[MemoryPosition ] > 0 then215 if Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] > 0 then 213 216 FProgramIndex := FProgram[FProgramIndex].Parameter - 1; 214 217 end; … … 216 219 procedure TTargetInterpretter.CommandInc; 217 220 begin 218 Memory[MemoryPosition] := ((Memory[MemoryPosition] + FProgram[FProgramIndex].Parameter) mod CellSize); 221 Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] := 222 ((Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] + 223 FProgram[FProgramIndex].Parameter) mod CellSize); 219 224 end; 220 225 221 226 procedure TTargetInterpretter.CommandDec; 222 227 begin 223 Memory[MemoryPosition] := ((Memory[MemoryPosition] - FProgram[FProgramIndex].Parameter) mod CellSize); 228 Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] := 229 ((Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] - 230 FProgram[FProgramIndex].Parameter) mod CellSize); 224 231 end; 225 232 … … 240 247 procedure TTargetInterpretter.CommandSet; 241 248 begin 242 Memory[MemoryPosition] := FProgram[FProgramIndex].Parameter mod CellSize; 249 Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] := 250 FProgram[FProgramIndex].Parameter mod CellSize; 251 end; 252 253 procedure TTargetInterpretter.CommandMultiply; 254 begin 255 Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] := 256 (Memory[MemoryPosition + FProgram[FProgramIndex].RelIndex] + 257 Memory[MemoryPosition] * FProgram[FProgramIndex].Parameter) mod CellSize; 243 258 end; 244 259 … … 411 426 // Extended commands 412 427 FCommandTable[cmSet] := CommandSet; 428 FCommandTable[cmMultipy] := CommandMultiply; 413 429 end; 414 430 -
trunk/Target/UTargetJava.pas
r72 r86 14 14 TTargetJava = class(TBFTarget) 15 15 private 16 function GetMemoryCell: string; 16 17 public 17 18 constructor Create; override; … … 44 45 end; 45 46 47 function TTargetJava.GetMemoryCell: string; 48 begin 49 Result := 'Memory[Pos'; 50 if FProgram[FProgramIndex].RelIndex > 0 then 51 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex) 52 else if FProgram[FProgramIndex].RelIndex < 0 then 53 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex)); 54 Result := Result + ']'; 55 end; 56 46 57 procedure TTargetJava.Compile; 47 58 begin … … 67 78 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 68 79 cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 69 cmInc: AddLine('Memory[Pos] = (char)((int)Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 70 cmDec: AddLine('Memory[Pos] = (char)((int)Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 71 cmOutput: AddLine('System.out.print(Memory[Pos]);'); 72 cmInput: AddLine('Memory[Pos] = (char)System.in.read();'); 73 cmSet: AddLine('Memory[Pos] = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 80 cmInc: AddLine(GetMemoryCell + ' = (char)((int)' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 81 cmDec: AddLine(GetMemoryCell + ' = (char)((int)' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 82 cmOutput: AddLine('System.out.print(' + GetMemoryCell + ');'); 83 cmInput: AddLine(GetMemoryCell + ' = (char)System.in.read();'); 84 cmSet: AddLine(GetMemoryCell + ' = (char)' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 85 cmMultipy: AddLine(GetMemoryCell + ' = (char)((int)' + GetMemoryCell + ' + (int)Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 74 86 cmLoopStart: begin 75 AddLine('while( Memory[Pos]!= 0)');87 AddLine('while(' + GetMemoryCell + ' != 0)'); 76 88 AddLine('{'); 77 89 Inc(Indent); -
trunk/Target/UTargetPHP.pas
r72 r86 14 14 TTargetPHP = class(TBFTarget) 15 15 private 16 function GetMemoryCell: string; 16 17 public 17 18 constructor Create; override; … … 44 45 end; 45 46 47 function TTargetPHP.GetMemoryCell: string; 48 begin 49 Result := '$Memory[$Position'; 50 if FProgram[FProgramIndex].RelIndex > 0 then 51 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex) 52 else if FProgram[FProgramIndex].RelIndex < 0 then 53 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex)); 54 Result := Result + ']'; 55 end; 56 46 57 procedure TTargetPHP.Compile; 47 58 begin … … 59 70 cmPointerInc: AddLine('$Position = $Position + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 60 71 cmPointerDec: AddLine('$Position = $Position - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 61 cmInc: AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 62 cmDec: AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 63 cmOutput: AddLine('echo($Memory[$Position]);'); 64 cmInput: AddLine('$Memory[$Position] = fgetc(STDIN);'); 65 cmSet: AddLine('$Memory[$Position] = chr(' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 72 cmInc: AddLine(GetMemoryCell + ' = chr(ord(' + GetMemoryCell + ') + ' + 73 IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 74 cmDec: AddLine(GetMemoryCell + ' = chr(ord(' + GetMemoryCell + ') - ' + 75 IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 76 cmOutput: AddLine('echo(' + GetMemoryCell + ');'); 77 cmInput: AddLine(GetMemoryCell + ' = fgetc(STDIN);'); 78 cmSet: AddLine(GetMemoryCell + ' = chr(' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 79 cmMultipy: AddLine(GetMemoryCell + ' = chr(ord(' + GetMemoryCell + ') + ord($Memory[$Position]) * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 66 80 cmLoopStart: begin 67 AddLine('while( $Memory[$Position]!= "\0") {');81 AddLine('while(' + GetMemoryCell + ' != "\0") {'); 68 82 Inc(Indent); 69 83 end; -
trunk/Target/UTargetPython.pas
r82 r86 14 14 TTargetPython = class(TBFTarget) 15 15 private 16 function GetMemoryCell: string; 16 17 public 17 18 constructor Create; override; … … 42 43 ExecutorPath := '/usr/bin/python'; 43 44 {$ENDIF} 45 end; 46 47 function TTargetPython.GetMemoryCell: string; 48 begin 49 Result := 'memory[position'; 50 if FProgram[FProgramIndex].RelIndex > 0 then 51 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex) 52 else if FProgram[FProgramIndex].RelIndex < 0 then 53 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex)); 54 Result := Result + ']'; 44 55 end; 45 56 … … 97 108 cmPointerInc: AddLine('position += ' + IntToStr(FProgram[FProgramIndex].Parameter)); 98 109 cmPointerDec: AddLine('position -= ' + IntToStr(FProgram[FProgramIndex].Parameter)); 99 cmInc: AddLine( 'memory[position]+= ' + IntToStr(FProgram[FProgramIndex].Parameter));100 cmDec: AddLine( 'memory[position]-= ' + IntToStr(FProgram[FProgramIndex].Parameter));110 cmInc: AddLine(GetMemoryCell + ' += ' + IntToStr(FProgram[FProgramIndex].Parameter)); 111 cmDec: AddLine(GetMemoryCell + ' -= ' + IntToStr(FProgram[FProgramIndex].Parameter)); 101 112 cmOutput: begin 102 AddLine('sys.stdout.write(chr( memory[position]))');113 AddLine('sys.stdout.write(chr(' + GetMemoryCell + '))'); 103 114 AddLine('sys.stdout.flush()'); 104 115 end; 105 cmInput: AddLine('memory[position] = ord(getchar())'); 106 cmSet: AddLine('memory[position] = ' + IntToStr(FProgram[FProgramIndex].Parameter)); 116 cmInput: AddLine(GetMemoryCell + ' = ord(getchar())'); 117 cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter)); 118 cmMultipy: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + memory[position] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 107 119 cmLoopStart: begin 108 AddLine('while( memory[position]!= 0):');120 AddLine('while(' + GetMemoryCell + ' != 0):'); 109 121 Inc(Indent); 110 122 end;
Note:
See TracChangeset
for help on using the changeset viewer.