Changeset 147 for branches/easy compiler/USourceGenerator.pas
- Timestamp:
- Jan 17, 2018, 3:34:13 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/easy compiler/USourceGenerator.pas
r146 r147 15 15 Indent: Integer; 16 16 function GenerateBeginEnd(BeginEnd: TCommandBeginEnd): string; 17 function GenerateCommand(Command: TSourceCommand): string; 18 function GenerateRef(Reference: TSourceReference): string; 17 19 function IndentStr: string; 18 20 public … … 49 51 end; 50 52 51 function TSourceGenerator.Generate BeginEnd(BeginEnd: TCommandBeginEnd): string;53 function TSourceGenerator.GenerateRef(Reference: TSourceReference): string; 52 54 var 53 Instruction: TSourceCommand;54 I: Integer;55 55 Value: TSourceValue; 56 57 function GenerateRef(Reference: TSourceReference): string;58 56 begin 59 57 Result := ''; … … 71 69 end; 72 70 71 function TSourceGenerator.GenerateCommand(Command: TSourceCommand): string; 72 begin 73 Result := ''; 74 if Command is TCommandFunctionCall then 75 with TCommandFunctionCall(Command) do begin 76 if Name = 'print' then begin 77 Result := Result + IndentStr + 'Write(' + GenerateRef(TSourceReference(Parameters[0])) + ');' + 78 LineEnding; 79 end else 80 if Name = 'println' then begin 81 Result := Result + IndentStr + 'WriteLn(' + GenerateRef(TSourceReference(Parameters[0])) + ');' + 82 LineEnding; 83 end else 84 if Name = 'assign' then begin 85 Result := Result + IndentStr + GenerateRef(TSourceReference(Parameters[0])) + ' := ' + 86 GenerateRef(TSourceReference(Parameters[1])) + ';' + LineEnding; 87 end else 88 if Name = 'inputln' then begin 89 Result := Result + IndentStr + 'ReadLn(' + GenerateRef(TSourceReference(Parameters[0])) + ');' + LineEnding; 90 end else 91 if Name = 'increment' then begin 92 Result := Result + IndentStr + 'Inc(' + GenerateRef(TSourceReference(Parameters[0])) + ', ' + 93 GenerateRef(TSourceReference(Parameters[1])) + ');' + LineEnding; 94 end else 95 if Name = 'decrement' then begin 96 Result := Result + IndentStr + 'Dec(' + GenerateRef(TSourceReference(Parameters[0])) + ', ' + 97 GenerateRef(TSourceReference(Parameters[1])) + ');' + LineEnding; 98 end else 99 raise Exception.Create('Unsupported instruction name.'); 100 end else 101 if Command is TCommandBeginEnd then begin 102 Result := Result + GenerateBeginEnd(TCommandBeginEnd(Command)) + 103 ';' + LineEnding; 104 end else 105 if Command is TCommandBreak then begin 106 Result := Result + IndentStr + 'Break;' + LineEnding; 107 end else 108 if Command is TCommandContinue then begin 109 Result := Result + IndentStr + 'Continue;' + LineEnding; 110 end else 111 if Command is TCommandRepeat then begin 112 Result := Result + IndentStr + 'repeat' + LineEnding; 113 Inc(Indent); 114 Result := Result + IndentStr + GenerateCommand(TCommandRepeat(Command).Command) + LineEnding; 115 Dec(Indent); 116 Result := Result + IndentStr + 'until False;' + LineEnding; 117 end else 118 if Command is TCommandIfZero then begin 119 Result := Result + IndentStr + 'if ' + TCommandIfZero(Command).Variable.Variable.Name + ' = 0 then '; 120 end else 121 raise Exception.Create('Unsupported instruction'); 122 end; 123 124 function TSourceGenerator.GenerateBeginEnd(BeginEnd: TCommandBeginEnd): string; 125 var 126 I: Integer; 73 127 begin 74 128 Result := ''; … … 76 130 Inc(Indent); 77 131 with BeginEnd do 78 for I := 0 to Instructions.Count - 1 do begin 79 Instruction := TSourceCommand(Instructions[I]); 80 if Instruction is TCommandFunctionCall then 81 with TCommandFunctionCall(Instruction) do begin 82 if Name = 'print' then begin 83 Result := Result + IndentStr + 'Write(' + GenerateRef(TSourceReference(Parameters[0])) + ');' + 84 LineEnding; 85 end else 86 if Name = 'println' then begin 87 Result := Result + IndentStr + 'WriteLn(' + GenerateRef(TSourceReference(Parameters[0])) + ');' + 88 LineEnding; 89 end else 90 if Name = 'assign' then begin 91 Result := Result + IndentStr + GenerateRef(TSourceReference(Parameters[0])) + ' := ' + 92 GenerateRef(TSourceReference(Parameters[1])) + ';' + LineEnding; 93 end else 94 if Name = 'inputln' then begin 95 Result := Result + IndentStr + 'ReadLn(' + GenerateRef(TSourceReference(Parameters[0])) + ');' + LineEnding; 96 end else 97 if Name = 'increment' then begin 98 Result := Result + IndentStr + 'Inc(' + GenerateRef(TSourceReference(Parameters[0])) + ', ' + 99 GenerateRef(TSourceReference(Parameters[1])) + ');' + LineEnding; 100 end else 101 raise Exception.Create('Unsupported instruction name.'); 102 end else 103 if Instruction is TCommandBeginEnd then begin 104 Result := Result + GenerateBeginEnd(TCommandBeginEnd(Instruction)) + 105 ';' + LineEnding; 106 end else 107 if Instruction is TCommandIfZero then begin 108 Result := Result + IndentStr + 'if ' + TCommandIfZero(Instruction).Variable.Name + ' = 0 then '; 109 end else 110 raise Exception.Create('Unsupported instruction'); 132 for I := 0 to Commands.Count - 1 do begin 133 Result := Result + GenerateCommand(TSourceCommand(Commands[I])); 111 134 end; 112 135 Dec(Indent);
Note:
See TracChangeset
for help on using the changeset viewer.