Changeset 17 for trunk/Compiler/Produce/UProducerPascal.pas
- Timestamp:
- Nov 8, 2010, 2:14:13 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Compiler/Produce/UProducerPascal.pas
r12 r17 15 15 TProducerPascal = class(TProducer) 16 16 private 17 procedure Emit(AText: string; NewLine: Boolean = True);18 17 procedure GenerateUses(UsedModules: TUsedModuleList); 19 18 procedure GenerateModule(Module: TModule); 19 procedure GenerateUnit(Module: TModule); 20 procedure GenerateLibrary(Module: TModule); 21 procedure GeneratePackage(Module: TModule); 20 22 procedure GenerateType(AType: TType; AssignSymbol: Char = ':'); 21 23 procedure GenerateTypes(Types: TTypeList); 22 24 procedure GenerateCommonBlock(CommonBlock: TCommonBlock; 23 25 LabelPrefix: string); 24 procedure GenerateProgram(ProgramBlock: TProgram);25 26 procedure GenerateFunctions(Functions: TFunctionList); 26 27 procedure GenerateConstants(Constants: TConstantList); … … 35 36 function GenerateExpression(Expression: TExpression): string; 36 37 public 37 TextSource: TStringList;38 IndentationLength: Integer;39 Indetation: Integer;40 38 procedure AssignToStringList(Target: TStringList); override; 41 39 procedure Produce(Module: TModule); override; … … 50 48 constructor TProducerPascal.Create; 51 49 begin 52 IndentationLength := 2; 53 TextSource := TStringList.Create; 50 inherited; 54 51 Name := 'Delphi'; 55 52 end; … … 57 54 destructor TProducerPascal.Destroy; 58 55 begin 59 TextSource.Free;60 56 inherited; 61 57 end; 62 58 63 procedure TProducerPascal.Emit(AText: string; NewLine: Boolean = True);64 begin65 with TextSource do begin66 if Count = 0 then Add('');67 if Strings[Count - 1] = '' then68 Strings[Count - 1] := Strings[Count - 1] + DupeString(' ', IndentationLength * Indetation);69 Strings[Count - 1] := Strings[Count - 1] + AText;70 if NewLine then Add('');71 end;72 end;73 74 59 procedure TProducerPascal.GenerateUses(UsedModules: TUsedModuleList); 75 60 var 76 61 I: Integer; 77 Line: string; 78 begin 79 Line := 'uses '; 62 ModuleName: string; 63 begin 64 EmitLn('uses'); 65 Inc(Indetation); 80 66 for I := 0 to UsedModules.Count - 1 do begin 81 Line := Line + TUsedModule(UsedModules[I]).Name; 82 if I < UsedModules.Count - 1 then Line := Line + ', '; 83 end; 84 Emit(Line + ';'); 85 Emit(''); 67 if Assigned(TUsedModule(UsedModules[I]).Module) then 68 ModuleName := TUsedModule(UsedModules[I]).Module.Name 69 else ModuleName := '(' + TUsedModule(UsedModules[I]).Name + ')'; 70 if UsedModules.ParentModule is TModuleProgram then begin 71 Emit(ModuleName + ' in ''' + ModuleName + '.pas'''); 72 if I < UsedModules.Count - 1 then EmitLn(', '); 73 end else begin 74 Emit(ModuleName); 75 if I < UsedModules.Count - 1 then Emit(', '); 76 end; 77 end; 78 EmitLn(';'); 79 Dec(Indetation); 86 80 end; 87 81 … … 91 85 if Module is TModuleProgram then 92 86 with TModuleProgram(Module) do begin 93 Emit('program', False); 94 Emit(' ' + Name + ';'); 95 Emit(''); 87 Module.TargetFile := Module.Name + '.dpr'; 88 EmitLn('program ' + Name + ';'); 96 89 GenerateUses(UsedModules); 97 90 GenerateCommonBlock(Body, ''); 98 Emit ('.', False);91 EmitLn('.'); 99 92 end else 100 if Module is TModuleUnit then Emit('unit', False) 101 else if Module is TModuleLibrary then Emit('library', False) 102 else if Module is TModulePackage then Emit('package', False); 93 if Module is TModuleUnit then GenerateUnit(Module) 94 else if Module is TModuleLibrary then GenerateLibrary(Module) 95 else if Module is TModulePackage then GeneratePackage(Module); 96 end; 97 98 procedure TProducerPascal.GenerateUnit(Module: TModule); 99 begin 100 EmitLn('unit ' + TModuleUnit(Module).Name + ';'); 101 EmitLn; 102 EmitLn('interface'); 103 EmitLn; 104 GenerateCommonBlock(TModuleUnit(Module).Body, '.'); 105 EmitLn('implementation'); 106 EmitLn; 107 EmitLn('end.'); 108 end; 109 110 procedure TProducerPascal.GenerateLibrary(Module: TModule); 111 begin 112 113 end; 114 115 procedure TProducerPascal.GeneratePackage(Module: TModule); 116 begin 117 103 118 end; 104 119 … … 108 123 begin 109 124 if AType is TTypeRecord then begin 110 Emit (AType.Name + ' ' + AssignSymbol + ' record');125 EmitLn(AType.Name + ' ' + AssignSymbol + ' record'); 111 126 Inc(Indetation); 112 127 for I := 0 to TTypeRecord(AType).CommonBlock.Types.Count - 1 do begin 113 128 GenerateType(TType(TTypeRecord(AType).CommonBlock.Types[I])); 114 Emit (';');129 EmitLn(';'); 115 130 end; 116 131 Dec(Indetation); 117 Emit('end' , False);132 Emit('end'); 118 133 end else 119 134 if AType is TTypeArray then begin 120 Emit(AType.Name + ' ' + AssignSymbol + ' array ' , False);135 Emit(AType.Name + ' ' + AssignSymbol + ' array '); 121 136 if Assigned(TTypeArray(AType).IndexType) then begin 122 Emit('[' , False);137 Emit('['); 123 138 GenerateType(TTypeArray(AType).IndexType); 124 Emit(']' , False);125 end; 126 Emit(' of ' , False);139 Emit(']'); 140 end; 141 Emit(' of '); 127 142 if Assigned(TTypeArray(AType).ItemType) then 128 143 GenerateType(TTypeArray(AType).ItemType); 129 144 end else begin 130 Emit(AType.Name , False);145 Emit(AType.Name); 131 146 if Assigned(AType.UsedType) then begin 132 Emit(' ' + AssignSymbol + ' ' , False);147 Emit(' ' + AssignSymbol + ' '); 133 148 GenerateType(AType.UsedType); 134 149 end; … … 141 156 begin 142 157 if Types.Count > 0 then begin 143 Emit ('type');158 EmitLn('type'); 144 159 Inc(Indetation); 145 160 for I := 0 to Types.Count - 1 do … … 147 162 if (not Internal) then begin 148 163 GenerateType(TType(Types[I]), '='); 149 Emit (';');164 EmitLn(';'); 150 165 end; 151 166 Dec(Indetation); 152 Emit ('');167 EmitLn; 153 168 end; 154 169 end; 155 170 156 171 procedure TProducerPascal.Produce(Module: TModule); 172 var 173 I: Integer; 157 174 begin 158 175 inherited; 159 176 TextSource.Clear; 177 178 // Check unit names 179 with Module.ParentProgram do 180 for I := 0 to Modules.Count - 1 do 181 if TModule(Modules[I]).Name = 'System' then 182 TModule(Modules[I]).Name := 'System2'; 183 160 184 GenerateModule(Module); 161 end;162 163 procedure TProducerPascal.GenerateProgram(ProgramBlock: TProgram);164 var165 I: Integer;166 begin167 Indetation := 0;;168 with ProgramBlock do169 for I := 0 to Modules.Count - 1 do170 GenerateModule(TModule(Modules[I]));171 185 end; 172 186 … … 195 209 if (FunctionType = ftFunction) and Assigned(ResultType) then 196 210 Line := Line + ': ' + ResultType.Name; 197 Emit (Line + ';');211 EmitLn(Line + ';'); 198 212 GenerateBeginEnd(Code); 199 Emit (';');200 Emit ('');213 EmitLn(';'); 214 EmitLn; 201 215 end; 202 216 end; … … 207 221 begin 208 222 if Constants.Count > 0 then begin 209 Emit ('const');223 EmitLn('const'); 210 224 Inc(Indetation); 211 225 for I := 0 to Constants.Count - 1 do 212 226 with TConstant(Constants[I]) do 213 227 if not System then begin 214 Emit(Name + ': ' + ValueType.Name + ' = ' + Value + ';'); 228 //Emit(Name + ': '); 229 //if Assigned(ValueType) then Emit(ValueType.Name); 230 //Emit(' = ' + Value + ';'); 215 231 end; 216 232 Dec(Indetation); … … 223 239 I: Integer; 224 240 begin 225 Emit ('begin');241 EmitLn('begin'); 226 242 Inc(Indetation); 227 243 // Commands 228 244 for I := 0 to BeginEnd.Commands.Count - 1 do begin 229 245 GenerateCommand(TCommand(BeginEnd.Commands[I])); 230 Emit (';');246 EmitLn(';'); 231 247 end; 232 248 233 249 Dec(Indetation); 234 Emit('end' , False);250 Emit('end'); 235 251 end; 236 252 … … 239 255 I: Integer; 240 256 begin 241 Emit ('var');257 EmitLn('var'); 242 258 Inc(Indetation); 243 259 for I := 0 to Variables.Count - 1 do 244 260 with TVariable(Variables[I]) do 245 Emit (Name + ': ' + ValueType.Name + ';');261 EmitLn(Name + ': ' + ValueType.Name + ';'); 246 262 Dec(Indetation); 247 Emit ('');263 EmitLn; 248 264 end; 249 265 … … 286 302 procedure TProducerPascal.GenerateAssignment(Assignment: TAssignment); 287 303 begin 288 Emit(Assignment.Target.Name + ' := ' + GenerateExpression(Assignment.Source) , False);304 Emit(Assignment.Target.Name + ' := ' + GenerateExpression(Assignment.Source)); 289 305 end; 290 306 … … 305 321 end; 306 322 end; 307 Emit(Line , False);323 Emit(Line); 308 324 end; 309 325
Note:
See TracChangeset
for help on using the changeset viewer.