Changeset 142 for branches/easy compiler/USourceGenerator.pas
- Timestamp:
- Jan 16, 2018, 3:19:23 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/easy compiler/USourceGenerator.pas
r141 r142 12 12 13 13 TSourceGenerator = class 14 private 15 function GenerateBeginEnd(BeginEnd: TSourceBeginEnd): string; 16 public 14 17 function Generate(SourceCode: TSourceCode): string; 15 18 end; … … 21 24 22 25 function TSourceGenerator.Generate(SourceCode: TSourceCode): string; 26 var 27 I: Integer; 28 begin 29 Result := ''; 30 if SourceCode.Variables.Count > 0 then 31 Result := Result + 'var' + LineEnding; 32 with SourceCode do 33 for I := 0 to Variables.Count - 1 do 34 with TSourceVariable(Variables[I]) do begin 35 Result := Result + ' ' + Name + ': '; 36 if ValueType.Name = 'String' then 37 Result := Result + 'string' 38 else if ValueType.Name = 'Integer' then 39 Result := Result + 'Integer' 40 else raise Exception.Create('Unsupported type'); 41 Result := Result + ';' + LineEnding; 42 end; 43 Result := Result + GenerateBeginEnd(SourceCode.Main) + '.' + LineEnding; 44 end; 45 46 function TSourceGenerator.GenerateBeginEnd(BeginEnd: TSourceBeginEnd): string; 23 47 var 24 48 Output: string; … … 45 69 begin 46 70 Output := ''; 47 if SourceCode.Variables.Count > 0 then48 Output := Output + 'var' + LineEnding;49 with SourceCode do50 for I := 0 to Variables.Count - 1 do51 with TSourceVariable(Variables[I]) do begin52 Output := Output + ' ' + Name + ': ';53 if ValueType.Name = 'String' then54 Output := Output + 'string'55 else if ValueType.Name = 'Integer' then56 Output := Output + 'Integer'57 else raise Exception.Create('Unsupported type');58 Output := Output + ';' + LineEnding;59 end;60 71 Output := Output + 'begin' + LineEnding; 61 with SourceCodedo72 with BeginEnd do 62 73 for I := 0 to Instructions.Count - 1 do begin 63 74 Instruction := TSourceInstruction(Instructions[I]); 64 if Instruction is TSource InstructionFunctionthen65 with TSource InstructionFunction(Instruction) do begin75 if Instruction is TSourceFunctionCall then 76 with TSourceFunctionCall(Instruction) do begin 66 77 if Name = 'print' then begin 67 78 Output := Output + ' Write(' + GenerateRef(Parameters[0]) + ');' + … … 84 95 end else 85 96 raise Exception.Create('Unsupported instruction name.'); 86 end else raise Exception.Create('Unsupported instruction'); 97 end else 98 if Instruction is TSourceBeginEnd then begin 99 Output := Output + GenerateBeginEnd(TSourceBeginEnd(Instruction)) + 100 ';' + LineEnding; 101 end else 102 raise Exception.Create('Unsupported instruction'); 87 103 end; 88 Output := Output + 'end .' + LineEnding;104 Output := Output + 'end'; 89 105 Result := Output; 90 106 end;
Note:
See TracChangeset
for help on using the changeset viewer.