Changeset 17 for trunk/Compiler
- Timestamp:
- Nov 8, 2010, 2:14:13 PM (14 years ago)
- Location:
- trunk/Compiler
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Compiler/Analyze/UAnalyzerPascal.pas
r12 r17 27 27 Expressions: TListExpression; var Func: TFunctionCall): Boolean; 28 28 function ParseUses(SourceCode: TUsedModuleList; AExported: Boolean): Boolean; 29 function ParseUsesItem(SourceCode: TUsedModuleList; AExported: Boolean): Boolean; 29 30 function ParseModule(ProgramCode: TProgram): TModule; override; 30 31 function ParseUnit(var SourceCode: TModuleUnit; ProgramCode: TProgram): Boolean; … … 1256 1257 if NextToken = 'uses' then begin 1257 1258 Expect('uses'); 1259 ParseUsesItem(SourceCode, AExported); 1260 while NextToken = ',' do begin 1261 Expect(','); 1262 ParseUsesItem(SourceCode, AExported); 1263 end; 1264 Expect(';'); 1265 Result := True; 1266 end else Result := False; 1267 end; 1268 1269 function TAnalyzerPascal.ParseUsesItem(SourceCode: TUsedModuleList; 1270 AExported: Boolean): Boolean; 1271 begin 1258 1272 with TUsedModule(SourceCode.Items[SourceCode.Add(TUsedModule.Create)]) do begin 1259 1273 Name := ReadToken; … … 1273 1287 end; 1274 1288 end; 1275 while NextToken = ',' do begin1276 Expect(',');1277 with TUsedModule(SourceCode.Items[SourceCode.Add(TUsedModule.Create)]) do1278 begin1279 Name := ReadToken;1280 if NextToken = 'in' then begin1281 Expect('in');1282 Location := ReadToken;1283 end else Location := Name + '.pas';1284 Module := SourceCode.ParentModule.ParentProgram.Modules.Search(Name);1285 if not Assigned(Module) then begin1286 if not ParseFile(Name) then begin1287 ErrorMessage(SUnitNotFound, [Name], -2);1288 SourceCode.Delete(SourceCode.Count - 1);1289 end;1290 end;1291 end;1292 end;1293 Expect(';');1294 Result := True;1295 end else Result := False;1296 1289 end; 1297 1290 -
trunk/Compiler/Produce/UProducer.pas
r12 r17 7 7 8 8 uses 9 USourceCode, Classes, SysUtils ;9 USourceCode, Classes, SysUtils, StrUtils; 10 10 11 11 type 12 13 { TProducer } 14 12 15 TProducer = class 13 16 Name: string; 17 TextSource: TStringList; 18 IndentationLength: Integer; 19 Indetation: Integer; 20 procedure Emit(AText: string); 21 procedure EmitLn(AText: string = ''); 14 22 procedure AssignToStringList(Target: TStringList); virtual; abstract; 15 23 procedure Produce(Module: TModule); virtual; abstract; 24 constructor Create; 25 destructor Destroy; override; 16 26 end; 17 27 … … 41 51 {$I 'GenericObjectList.inc'} 42 52 53 { TProducer } 54 55 procedure TProducer.EmitLn(AText: string = ''); 56 begin 57 Emit(AText); 58 TextSource.Add(''); 59 end; 60 61 constructor TProducer.Create; 62 begin 63 TextSource := TStringList.Create; 64 IndentationLength := 2; 65 end; 66 67 destructor TProducer.Destroy; 68 begin 69 TextSource.Free; 70 inherited Destroy; 71 end; 72 73 procedure TProducer.Emit(AText: string); 74 begin 75 with TextSource do begin 76 if Count = 0 then Add(''); 77 if Strings[Count - 1] = '' then 78 Strings[Count - 1] := Strings[Count - 1] + DupeString(' ', IndentationLength * Indetation); 79 Strings[Count - 1] := Strings[Count - 1] + AText; 80 end; 81 end; 82 43 83 end. -
trunk/Compiler/Produce/UProducerDynamicC.pas
r12 r17 17 17 function TranslateType(Name: string): string; 18 18 function TranslateOperator(Name: string): string; 19 procedure Emit(AText: string);20 procedure EmitLn(AText: string = '');21 19 procedure GenerateUses(UsedModules: TUsedModuleList); 22 20 procedure GenerateModule(Module: TModule); … … 39 37 function GenerateExpression(Expression: TExpression): string; 40 38 public 41 TextSource: TStringList;42 IndentationLength: Integer;43 Indetation: Integer;44 39 procedure AssignToStringList(Target: TStringList); override; 45 40 procedure Produce(Module: TModule); override; … … 54 49 constructor TProducerDynamicC.Create; 55 50 begin 56 TextSource := TStringList.Create;57 IndentationLength := 2;58 51 Name := 'Dynamic C'; 59 52 end; … … 90 83 else if Name = 'xor' then Result := '^' 91 84 else Result := Name; 92 end;93 94 procedure TProducerDynamicC.EmitLn(AText: string = '');95 begin96 Emit(AText);97 TextSource.Add('');98 end;99 100 procedure TProducerDynamicC.Emit(AText: string);101 begin102 with TextSource do begin103 if Count = 0 then Add('');104 if Strings[Count - 1] = '' then105 Strings[Count - 1] := Strings[Count - 1] + DupeString(' ', IndentationLength * Indetation);106 Strings[Count - 1] := Strings[Count - 1] + AText;107 end;108 85 end; 109 86 -
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 -
trunk/Compiler/TranspascalCompiler.lpk
r14 r17 12 12 </SearchPaths> 13 13 <Other> 14 <CustomOptions Value="-dSINGLE_PRODUCER "/>14 <CustomOptions Value="-dSINGLE_PRODUCER_"/> 15 15 <CompilerPath Value="$(CompPath)"/> 16 16 </Other>
Note:
See TracChangeset
for help on using the changeset viewer.