Changeset 143 for branches/easy compiler/USourceGenerator.pas
- Timestamp:
- Jan 16, 2018, 3:40:08 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/easy compiler/USourceGenerator.pas
r142 r143 13 13 TSourceGenerator = class 14 14 private 15 Indent: Integer; 15 16 function GenerateBeginEnd(BeginEnd: TSourceBeginEnd): string; 17 function IndentStr: string; 16 18 public 17 19 function Generate(SourceCode: TSourceCode): string; … … 27 29 I: Integer; 28 30 begin 31 Indent := 0; 29 32 Result := ''; 30 33 if SourceCode.Variables.Count > 0 then 31 Result := Result + 'var' + LineEnding; 34 Result := Result + IndentStr + 'var' + LineEnding; 35 Inc(Indent); 32 36 with SourceCode do 33 37 for I := 0 to Variables.Count - 1 do 34 38 with TSourceVariable(Variables[I]) do begin 35 Result := Result + ' '+ Name + ': ';39 Result := Result + IndentStr + Name + ': '; 36 40 if ValueType.Name = 'String' then 37 41 Result := Result + 'string' … … 41 45 Result := Result + ';' + LineEnding; 42 46 end; 47 Dec(Indent); 43 48 Result := Result + GenerateBeginEnd(SourceCode.Main) + '.' + LineEnding; 44 49 end; … … 46 51 function TSourceGenerator.GenerateBeginEnd(BeginEnd: TSourceBeginEnd): string; 47 52 var 48 Output: string;49 53 Instruction: TSourceInstruction; 50 54 I: Integer; … … 68 72 69 73 begin 70 Output := ''; 71 Output := Output + 'begin' + LineEnding; 74 Result := ''; 75 Result := Result + IndentStr + 'begin' + LineEnding; 76 Inc(Indent); 72 77 with BeginEnd do 73 78 for I := 0 to Instructions.Count - 1 do begin … … 76 81 with TSourceFunctionCall(Instruction) do begin 77 82 if Name = 'print' then begin 78 Output := Output + 'Write(' + GenerateRef(Parameters[0]) + ');' +83 Result := Result + IndentStr + 'Write(' + GenerateRef(Parameters[0]) + ');' + 79 84 LineEnding; 80 85 end else 81 86 if Name = 'println' then begin 82 Output := Output + 'WriteLn(' + GenerateRef(Parameters[0]) + ');' +87 Result := Result + IndentStr + 'WriteLn(' + GenerateRef(Parameters[0]) + ');' + 83 88 LineEnding; 84 89 end else 85 90 if Name = 'assign' then begin 86 Output := Output + ' '+ GenerateRef(Parameters[0]) + ' := ' +91 Result := Result + IndentStr + GenerateRef(Parameters[0]) + ' := ' + 87 92 GenerateRef(Parameters[1]) + ';' + LineEnding; 88 93 end else 89 94 if Name = 'inputln' then begin 90 Output := Output + 'ReadLn(' + GenerateRef(Parameters[0]) + ');' + LineEnding;95 Result := Result + IndentStr + 'ReadLn(' + GenerateRef(Parameters[0]) + ');' + LineEnding; 91 96 end else 92 97 if Name = 'increment' then begin 93 Output := Output + 'Inc(' + GenerateRef(Parameters[0]) + ', ' +98 Result := Result + IndentStr + 'Inc(' + GenerateRef(Parameters[0]) + ', ' + 94 99 GenerateRef(Parameters[1]) + ');' + LineEnding; 95 100 end else … … 97 102 end else 98 103 if Instruction is TSourceBeginEnd then begin 99 Output := Output + GenerateBeginEnd(TSourceBeginEnd(Instruction)) +104 Result := Result + GenerateBeginEnd(TSourceBeginEnd(Instruction)) + 100 105 ';' + LineEnding; 101 106 end else 102 107 raise Exception.Create('Unsupported instruction'); 103 108 end; 104 Output := Output + 'end'; 105 Result := Output; 109 Dec(Indent); 110 Result := Result + IndentStr + 'end'; 111 end; 112 113 function TSourceGenerator.IndentStr: string; 114 begin 115 SetLength(Result, Indent * 2); 116 if Indent > 0 then 117 FillChar(Result[1], Indent * 2, ' '); 106 118 end; 107 119
Note:
See TracChangeset
for help on using the changeset viewer.