Changeset 27 for branches/Syntetizer/USyntetizer.pas
- Timestamp:
- Oct 29, 2009, 11:07:49 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Syntetizer/USyntetizer.pas
r26 r27 11 11 TSyntetizer = class; 12 12 13 TElement = class 13 ISyntetizable = interface 14 procedure Syntetize(Syntetizer: TSyntetizer); 15 end; 16 17 TElement = class(TInterfacedObject) 18 end; 19 20 TCompilerDirective = class(TElement) 21 Variable: string; 22 Value: string; 23 procedure Syntetize(Syntetizer: TSyntetizer); 24 end; 25 26 TCommand = class(TInterfacedObject, ISyntetizable) 14 27 procedure Syntetize(Syntetizer: TSyntetizer); virtual; 15 28 end; 16 29 17 TCommand = class(TElement) 18 end; 19 20 { TBeginEnd } 30 TProcedureParameter = class(TElement) 31 Value: string; 32 end; 33 34 { TProcedureCall } 35 36 TProcedureCall = class(TCommand) 37 Name: string; 38 Parameters: TList; // TList<TProcedureParameter> 39 constructor Create; 40 destructor Destroy; override; 41 procedure Syntetize(Syntetizer: TSyntetizer); override; 42 end; 21 43 22 44 TBeginEnd = class(TCommand) 23 Commands: TList; // TList<TCommand> 45 Commands: TList; // TList<ICommand> 46 LastEnd: Boolean; 24 47 procedure Syntetize(Syntetizer: TSyntetizer); override; 25 48 constructor Create; … … 27 50 end; 28 51 29 { TProgram } 30 31 TProgram = class(TElement) 52 TVariableDeclaration = class(TInterfacedObject, ISyntetizable) 53 Name: string; 54 VarType: string; 55 procedure Syntetize(Syntetizer: TSyntetizer); 56 end; 57 58 { TVariableSection } 59 60 TVariableSection = class(TInterfacedObject, ISyntetizable) 61 Variables: TList; // TList<TVariableDeclaration> 62 procedure Syntetize(Syntetizer: TSyntetizer); 63 constructor Create; 64 destructor Destroy; override; 65 end; 66 67 TProgram = class(TInterfacedObject, ISyntetizable) 32 68 Name: string; 33 69 Code: TBeginend; 34 procedure Syntetize(Syntetizer: TSyntetizer); override; 35 constructor Create; 36 destructor Destroy; override; 37 end; 70 CompilerDirectives: TList; // TList<TCompilerDirective> 71 Variables: TVariableSection; 72 procedure Syntetize(Syntetizer: TSyntetizer); 73 constructor Create; 74 destructor Destroy; override; 75 end; 76 77 { TSyntetizer } 38 78 39 79 TSyntetizer = class 40 BaseElement: TElement; 80 private 81 procedure AddLine(Line: string); 82 public 83 BaseElement: TProgram; 41 84 Output: TStringList; 85 Indent: Integer; 42 86 procedure Syntetize; 43 87 constructor Create; … … 48 92 49 93 { TSyntetizer } 94 95 procedure TSyntetizer.AddLine(Line: string); 96 var 97 I: Integer; 98 begin 99 for I := 1 to Indent * 2 do 100 Line := ' ' + Line; 101 Output.Add(Line); 102 end; 50 103 51 104 procedure TSyntetizer.Syntetize; … … 66 119 end; 67 120 68 { TElement }69 70 procedure TElement.Syntetize(Syntetizer: TSyntetizer);71 begin72 73 end;74 75 121 { TProgram } 76 122 77 123 procedure TProgram.Syntetize(Syntetizer: TSyntetizer); 78 begin 79 Syntetizer.Output.Add('program ' + Name + ';'); 124 var 125 I: Integer; 126 begin 127 Syntetizer.AddLine('program ' + Name + ';'); 128 for I := 0 to CompilerDirectives.Count - 1 do 129 TCompilerDirective(CompilerDirectives[I]).Syntetize(Syntetizer); 130 Variables.Syntetize(Syntetizer); 80 131 Code.Syntetize(Syntetizer); 81 132 inherited; … … 84 135 constructor TProgram.Create; 85 136 begin 137 CompilerDirectives := TList.Create; 138 Variables := TVariableSection.Create; 86 139 Code := TBeginEnd.Create; 140 Code.LastEnd := True; 87 141 end; 88 142 89 143 destructor TProgram.Destroy; 90 begin 144 var 145 I: Integer; 146 begin 147 Variables.Destroy; 148 for I := 0 to CompilerDirectives.Count - 1 do 149 TCompilerDirective(CompilerDirectives[I]).Destroy; 91 150 Code.Destroy; 92 151 inherited Destroy; … … 99 158 I: Integer; 100 159 begin 101 with Syntetizer.Output do begin 102 Add('begin'); 160 with Syntetizer do begin 161 AddLine('begin'); 162 Inc(Syntetizer.Indent); 103 163 for I := 0 to Commands.Count - 1 do 104 164 TCommand(Commands[I]).Syntetize(Syntetizer); 105 Add('end;'); 165 Dec(Syntetizer.Indent); 166 if LastEnd then AddLine('end.') else AddLine('end;'); 106 167 end; 107 168 inherited Syntetize(Syntetizer); … … 123 184 end; 124 185 186 { TProcedureCall } 187 188 constructor TProcedureCall.Create; 189 begin 190 Parameters := TList.Create; 191 end; 192 193 destructor TProcedureCall.Destroy; 194 var 195 I: Integer; 196 begin 197 for I := 0 to Parameters.Count - 1 do 198 TProcedureParameter(Parameters[I]).Destroy; 199 Parameters.Destroy; 200 inherited Destroy; 201 end; 202 203 procedure TProcedureCall.Syntetize(Syntetizer: TSyntetizer); 204 var 205 ParameterString: string; 206 I: Integer; 207 begin 208 for I := 0 to Parameters.Count - 1 do 209 ParameterString := ParameterString + TProcedureParameter(Parameters[I]).Value; 210 if Parameters.Count > 0 then 211 ParameterString := '(' + ParameterString + ')'; 212 Syntetizer.AddLine(Name + ParameterString + ';'); 213 inherited Syntetize(Syntetizer); 214 end; 215 216 { TCompilerDirective } 217 218 procedure TCompilerDirective.Syntetize(Syntetizer: TSyntetizer); 219 begin 220 Syntetizer.AddLine('{$' + Variable + ' ' + Value + '}'); 221 end; 222 223 { TVariableDeclaration } 224 225 procedure TVariableDeclaration.Syntetize(Syntetizer: TSyntetizer); 226 begin 227 Syntetizer.AddLine(Name + ': ' + VarType + ';'); 228 inherited; 229 end; 230 231 { TCommand } 232 233 procedure TCommand.Syntetize(Syntetizer: TSyntetizer); 234 begin 235 end; 236 237 { TVariableSection } 238 239 procedure TVariableSection.Syntetize(Syntetizer: TSyntetizer); 240 var 241 I: Integer; 242 SectionString: string; 243 begin 244 Syntetizer.AddLine('var'); 245 Inc(Syntetizer.Indent); 246 for I := 0 to Variables.Count - 1 do 247 TVariableDeclaration(Variables[I]).Syntetize(Syntetizer); 248 Dec(Syntetizer.Indent); 249 end; 250 251 constructor TVariableSection.Create; 252 begin 253 Variables := TList.Create; 254 end; 255 256 destructor TVariableSection.Destroy; 257 var 258 I: Integer; 259 begin 260 for I := 0 to Variables.Count - 1 do 261 TVariableDeclaration(Variables[I]).Destroy; 262 Variables.Destroy; 263 inherited Destroy; 264 end; 265 125 266 end. 126 267
Note:
See TracChangeset
for help on using the changeset viewer.