Changeset 143
- Timestamp:
- Jan 16, 2018, 3:40:08 PM (7 years ago)
- Location:
- branches/easy compiler
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/easy compiler/USourceCode.pas
r142 r143 36 36 37 37 TSourceTypes = class(TObjectList) 38 Parent: TSourceTypes; 38 39 function AddNew(Name: string; ClassType: TSourceValueClass): TSourceType; 39 40 function Search(Name: string): TSourceType; … … 50 51 51 52 TSourceVariables = class(TObjectList) 53 Parent: TSourceVariables; 52 54 function AddNew(Name: string; ValueType: TSourceType): TSourceVariable; 53 55 function Search(Name: string): TSourceVariable; … … 107 109 108 110 TSourceConstants = class(TObjectList) 111 Parent: TSourceConstants; 109 112 function AddNewString(Value: string; Name: string = ''): TSourceConstant; 110 113 function AddNewInteger(Value: Integer; Name: string = ''): TSourceConstant; … … 141 144 procedure InitFunctions; 142 145 public 146 Functions: TSourceFunctions; 143 147 Types: TSourceTypes; 144 148 Variables: TSourceVariables; 145 149 Constants: TSourceConstants; 146 Functions: TSourceFunctions;147 150 Main: TSourceBeginEnd; 148 151 constructor Create; … … 186 189 Break; 187 190 end; 191 if not Assigned(Result) and Assigned(Parent) then 192 Result := Parent.Search(Name); 188 193 end; 189 194 … … 278 283 Break; 279 284 end; 285 if not Assigned(Result) and Assigned(Parent) then 286 Result := Parent.Search(Name); 280 287 end; 281 288 … … 312 319 Break; 313 320 end; 321 if not Assigned(Result) and Assigned(Parent) then 322 Result := Parent.Search(Name); 314 323 end; 315 324 -
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.