Changeset 20 for trunk/Compiler
- Timestamp:
- Feb 11, 2012, 10:56:03 PM (13 years ago)
- Location:
- trunk/Compiler
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Compiler/UCompiler.pas
r19 r20 40 40 constructor TBrainFuckCompiler.Create; 41 41 begin 42 42 Optimization := coNormal; 43 43 end; 44 44 -
trunk/Compiler/UCompilerDelphi.pas
r19 r20 30 30 var 31 31 I: Integer; 32 Sum: Integer; 33 34 function CheckOccurence(C: Char): Integer; 35 begin 36 Result := 1; 37 if Optimization = coNormal then 38 while ((I + 1) <= Length(Source)) and (Source[I + 1] = C) do begin 39 Inc(Result); 40 Inc(I) 41 end; 42 end; 43 32 44 begin 33 45 Indent := 0; … … 39 51 AddLine(''); 40 52 AddLine('var'); 41 AddLine(' Memory: array[0..30000] of Char;');42 AddLine(' Pos ition: Integer;');53 AddLine(' Memory: array[0..30000] of Byte;'); 54 AddLine(' Pos: Integer;'); 43 55 AddLine('begin'); 44 56 Inc(Indent); 45 for I := 1 to Length(Source) do begin 57 I := 1; 58 while (I <= Length(Source)) do begin 46 59 case Source[I] of 47 '>': AddLine('Inc(Position);'); 48 '<': AddLine('Dec(Position);'); 49 '+': AddLine('Memory[Position] := Succ(Memory[Position]);'); 50 '-': AddLine('Memory[Position] := Pred(Memory[Position]);'); 51 '.': AddLine('Write(Memory[Position]);'); 52 ',': AddLine('Read(Memory[Position]);'); 60 '>': begin 61 Sum := CheckOccurence('>'); 62 AddLine('Inc(Pos, ' + IntToStr(Sum) + ');'); 63 end; 64 '<': begin 65 Sum := CheckOccurence('<'); 66 AddLine('Dec(Pos, ' + IntToStr(Sum) + ');'); 67 end; 68 '+': begin 69 Sum := CheckOccurence('+'); 70 AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(Sum) + ';'); 71 end; 72 '-': begin 73 Sum := CheckOccurence('-'); 74 AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(Sum) + ';'); 75 end; 76 '.': AddLine('Write(Chr(Memory[Pos]));'); 77 ',': AddLine('Read(Chr(Memory[Pos]));'); 53 78 '[': begin 54 AddLine('while Memory[Pos ition] <> #0 do begin');79 AddLine('while Memory[Pos] <> 0 do begin'); 55 80 Inc(Indent); 56 81 end; … … 60 85 end; 61 86 end; 87 Inc(I); 62 88 end; 63 89 Dec(Indent); -
trunk/Compiler/UCompilerPHP.pas
r19 r20 31 31 var 32 32 I: Integer; 33 Sum: Integer; 34 35 function CheckOccurence(C: Char): Integer; 36 begin 37 Result := 1; 38 if Optimization = coNormal then 39 while ((I + 1) <= Length(Source)) and (Source[I + 1] = C) do begin 40 Inc(Result); 41 Inc(I) 42 end; 43 end; 44 33 45 begin 34 46 Indent := 0; … … 41 53 for I := 1 to Length(Source) do begin 42 54 case Source[I] of 43 '>': AddLine('$Position++;'); 44 '<': AddLine('$Position--;'); 45 '+': AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) + 1);'); 46 '-': AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) - 1);'); 55 '>': begin 56 Sum := CheckOccurence('>'); 57 AddLine('$Position = $Position + ' + IntToStr(Sum) + ';'); 58 end; 59 '<': begin 60 Sum := CheckOccurence('<'); 61 AddLine('$Position = $Position - ' + IntToStr(Sum) + ';'); 62 end; 63 '+': begin 64 Sum := CheckOccurence('+'); 65 AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) + ' + IntToStr(Sum) + ');'); 66 end; 67 '-': begin 68 Sum := CheckOccurence('-'); 69 AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) - ' + IntToStr(Sum) + ');'); 70 end; 47 71 '.': AddLine('echo($Memory[$Position]);'); 48 72 ',': AddLine('$Memory[$Position] = fgetc(STDIN);');
Note:
See TracChangeset
for help on using the changeset viewer.