Changeset 75 for branches/Transpascal/Compiler
- Timestamp:
- Oct 21, 2010, 7:56:25 AM (15 years ago)
- Location:
- branches/Transpascal/Compiler
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Transpascal/Compiler/Analyze/UPascalParser.pas
r74 r75 26 26 procedure ParseUnitImplementation(SourceCode: TModuleUnit); 27 27 procedure ParseProgram(SourceCode: TModuleProgram); 28 procedure ParseCommonBlock(SourceCode: TCommonBlock; EndSymbol: char = ';'); 28 procedure ParseCommonBlock(SourceCode: TCommonBlock; EndSymbol: char = ';'; 29 WithBody: Boolean = True); 29 30 procedure ParseCommonBlockInterface(SourceCode: TCommonBlock); 30 31 function ParseCommand(SourceCode: TCommonBlock): TCommand; … … 368 369 369 370 ParseCommonBlock(Body, '.'); 371 SourceCode.ParentProgram.Modules.Add(SourceCode); 370 372 end; 371 373 end; … … 384 386 if NextToken = 'implementation' then 385 387 ParseUnitImplementation(SourceCode); 388 389 SourceCode.ParentProgram.Modules.Add(SourceCode); 386 390 387 391 if NextToken = 'initialization' then begin … … 411 415 ParseUses(SourceCode.UsedModules, False); 412 416 413 ParseCommonBlock(SourceCode.Body, '.' );417 ParseCommonBlock(SourceCode.Body, '.', False); 414 418 end; 415 419 … … 417 421 418 422 procedure TPascalParser.ParseCommonBlock(SourceCode: TCommonBlock; 419 EndSymbol: char = ';' );423 EndSymbol: char = ';'; WithBody: Boolean = True); 420 424 begin 421 425 with SourceCode do begin 422 426 while (NextToken <> EndSymbol) do begin 423 if NextToken = 'var' then 427 if NextToken = 'var' then begin 428 Expect('var'); 424 429 ParseVariableList(Variables) 425 else if NextToken = 'const' then 430 end else 431 if NextToken = 'const' then begin 432 Expect('const'); 426 433 ParseConstantList(Constants) 427 else if NextToken = 'type' then 428 ParseTypeList(Types) 429 else if NextToken = 'procedure' then 434 end else 435 if NextToken = 'type' then begin 436 Expect('type'); 437 ParseTypeList(Types); 438 end else 439 if NextToken = 'procedure' then 430 440 ParseFunctionList(Functions) 431 441 else if NextToken = 'function' then 432 442 ParseFunctionList(Functions) 433 443 else begin 434 ParseBeginEnd(Code); 444 if WithBody then 445 ParseBeginEnd(Code); 435 446 Break; 436 447 end; 437 448 end; 438 Expect(EndSymbol);449 if WithBody then Expect(EndSymbol); 439 450 end; 440 451 end; … … 444 455 with SourceCode do begin 445 456 while (NextToken <> 'implementation') and (NextTokenType <> ttEndOfFile) do begin 446 if NextToken = 'var' then 447 ParseVariableList(Variables) 448 else if NextToken = 'const' then 449 ParseConstantList(Constants, True) 450 else if NextToken = 'type' then 451 ParseTypeList(Types, True) 452 else if NextToken = 'procedure' then 457 if NextToken = 'var' then begin 458 Expect('var'); 459 ParseVariableList(Variables); 460 end else 461 if NextToken = 'const' then begin 462 Expect('const'); 463 ParseConstantList(Constants, True); 464 end else 465 if NextToken = 'type' then begin 466 Expect('type'); 467 ParseTypeList(Types, True); 468 end else 469 if NextToken = 'procedure' then 453 470 ParseFunctionList(Functions, True) 454 471 else if NextToken = 'function' then … … 496 513 I: integer; 497 514 begin 515 try 498 516 Identifiers := TStringList.Create; 499 517 with SourceCode do begin 500 518 with TFunction(Items[Add(TFunction.Create)]) do begin 501 519 Parent := SourceCode.Parent; 502 if NextToken = 'procedure' then 503 begin 520 if NextToken = 'procedure' then begin 504 521 Expect('procedure'); 505 522 HaveResult := False; 506 end 507 else 508 begin 523 end else begin 509 524 Expect('function'); 510 525 HaveResult := True; … … 545 560 end; 546 561 end; 562 if NextToken = ';' then Expect(';'); 547 563 end; 548 564 Expect(')'); … … 580 596 if not Exported then ParseCommonBlock(TFunction(Last)); 581 597 end; 582 Identifiers.Destroy; 598 finally 599 Identifiers.Free; 600 end; 583 601 end; 584 602 … … 638 656 Identifiers := TStringList.Create; 639 657 with SourceCode do begin 640 Expect('var');641 658 while IsIdentificator(NextToken) and (NextTokenType <> ttEndOfFile) do begin 642 659 Identifiers.Clear; … … 694 711 begin 695 712 Identifiers := TStringList.Create; 696 with SourceCode do 697 begin 698 Expect('const'); 699 while IsIdentificator(NextToken) do 700 begin 713 with SourceCode do begin 714 while IsIdentificator(NextToken) do begin 701 715 ConstantName := ReadCode; 702 716 Constant := Search(ConstantName); 703 if not Assigned(Constant) then 704 begin 717 if not Assigned(Constant) then begin 705 718 Identifiers.Add(ConstantName); 706 while NextToken = ',' do 707 begin 719 while NextToken = ',' do begin 708 720 Expect(','); 709 721 Identifiers.Add(ReadCode); 710 722 end; 711 end 712 else 723 end else 713 724 ErrorMessage(SRedefineIdentifier, [ConstantName], -1); 714 725 Expect(':'); … … 742 753 with SourceCode do 743 754 begin 744 Expect('type');745 755 while IsIdentificator(NextToken) do begin 746 756 NewType := ParseType(SourceCode); … … 872 882 function TPascalParser.ParseTypeRecord(TypeList: TTypeList; Name: string 873 883 ): TType; 884 type 885 TSectionType = (stVar, stType, stConst); 874 886 var 875 887 Visibility: TTypeVisibility; 876 begin 888 SectionType: TSectionType; 889 begin 890 SectionType := stVar; 877 891 Visibility := tvPublic; 878 892 Expect('record'); 879 893 Result := TTypeRecord.Create; 880 894 TTypeRecord(Result).Parent := TypeList; 895 TTypeRecord(Result).CommonBlock.Parent := TypeList.Parent; 881 896 TType(Result).Name := Name; 882 897 while (NextToken <> 'end') and (NextTokenType <> ttEndOfFile) do … … 898 913 Visibility := tvProtected; 899 914 end else 900 if NextToken = 'var' then 901 ParseVariableList(TTypeRecord(Result).CommonBlock.Variables) 902 else if NextToken = 'const' then 915 if NextToken = 'var' then begin 916 Expect('var'); 917 SectionType := stVar 918 end else 919 if NextToken = 'const' then begin 920 Expect('const'); 921 SectionType := stConst 922 end else 923 if NextToken = 'type' then begin 924 Expect('type'); 925 SectionType := stType; 926 end; 927 928 if SectionType = stVar then begin 929 if NextToken = 'procedure' then 930 ParseFunctionList(TTypeRecord(Result).CommonBlock.Functions, True) 931 else if NextToken = 'function' then 932 ParseFunctionList(TTypeRecord(Result).CommonBlock.Functions, True) 933 else begin 934 ParseVariableList(TTypeRecord(Result).CommonBlock.Variables, True) 935 //TTypeRecord(Result).CommonBlock.Types.Add(ParseType(TypeList, True, ':')); 936 //TType(TTypeRecord(Result).CommonBlock.Types.Last).Visibility := Visibility; 937 end; ParseVariableList(TTypeRecord(Result).CommonBlock.Variables) 938 end 939 else if SectionType = stConst then 903 940 ParseConstantList(TTypeRecord(Result).CommonBlock.Constants, True) 904 else if NextToken = 'type' then 905 ParseTypeList(TTypeRecord(Result).CommonBlock.Types, True) 906 else if NextToken = 'procedure' then 907 ParseFunctionList(TTypeRecord(Result).CommonBlock.Functions, True) 908 else if NextToken = 'function' then 909 ParseFunctionList(TTypeRecord(Result).CommonBlock.Functions, True) 910 else begin 911 TTypeRecord(Result).CommonBlock.Types.Add(ParseType(TypeList, True, ':')); 912 TType(TTypeRecord(Result).CommonBlock.Types.Last).Visibility := Visibility; 913 end; 914 Expect(';'); 941 else if SectionType = stType then 942 ParseTypeList(TTypeRecord(Result).CommonBlock.Types, True); 915 943 end; 916 944 Expect('end'); -
branches/Transpascal/Compiler/Produce/UProducerC.pas
r68 r75 29 29 procedure GenerateFunctions(Functions: TFunctionList); 30 30 procedure GenerateBeginEnd(BeginEnd: TBeginEnd); 31 procedure GenerateVariableList(VariableList: TVariableList); 32 procedure GenerateVariable(Variable: TVariable); 31 33 procedure GenerateCommand(Command: TCommand); 32 34 procedure GenerateWhileDo(WhileDo: TWhileDo); … … 123 125 GenerateUses(TModuleProgram(Module).UsedModules); 124 126 GenerateCommonBlock(TModuleProgram(Module).Body, ''); 127 end else 128 if Module is TModuleUnit then begin 129 GenerateUses(TModuleProgram(Module).UsedModules); 130 GenerateCommonBlock(TModuleUnit(Module).Body, ''); 125 131 end; 126 132 end; … … 176 182 Inc(Indetation); 177 183 178 179 184 // Variables 180 185 if BeginEnd.Parent is TCommonBlock then begin 181 for I := 0 to BeginEnd.CommonBlock.Variables.Count - 1 do 182 with TVariable(BeginEnd.CommonBlock.Variables[I]) do 183 Emit(TranslateType(ValueType.Name) + ' ' + Name + ';'); 184 Emit(''); 186 GenerateVariableList(BeginEnd.CommonBlock.Variables); 185 187 end; 186 188 … … 191 193 Dec(Indetation); 192 194 Emit('}'); 195 end; 196 197 procedure TProducerC.GenerateVariableList(VariableList: TVariableList); 198 var 199 I: Integer; 200 begin 201 for I := 0 to VariableList.Count - 1 do 202 GenerateVariable(TVariable(VariableList[I])); 203 // Emit(''); 204 end; 205 206 procedure TProducerC.GenerateVariable(Variable: TVariable); 207 begin 208 with Variable do 209 Emit(TranslateType(ValueType.Name) + ' ' + Name + ';'); 193 210 end; 194 211 … … 296 313 if Assigned(AType) then begin 297 314 if AType is TTypeRecord then begin 298 Emit(' typedefstruct');315 Emit('struct'); 299 316 Emit('{'); 300 317 Inc(Indetation); 301 for I := 0 to TTypeRecord(AType).CommonBlock.Types.Count - 1 do begin 302 GenerateType(TType(TTypeRecord(AType).CommonBlock.Types[I])); 303 Emit(';'); 304 end; 318 GenerateVariableList(TTypeRecord(AType).CommonBlock.Variables); 305 319 Dec(Indetation); 306 320 Emit('} ' + TranslateType(AType.Name), False); … … 321 335 end else begin 322 336 if Assigned(AType.UsedType) then begin 323 GenerateType(AType.UsedType); 337 //GenerateType(AType.UsedType); 338 Emit(AType.UsedType.Name, False); 324 339 Emit(' ', False); 325 340 end; … … 338 353 with TType(Types[I]) do 339 354 if (not System) then begin 355 Emit('typedef ', False); 340 356 GenerateType(TType(Types[I])); 341 357 Emit(';'); -
branches/Transpascal/Compiler/UCompiler.pas
r74 r75 67 67 Parser.Process; 68 68 //ShowMessage(IntToHex(Integer(Addr(Parser.OnGetSource)), 8)); 69 NewModule := Parser.ParseModule(ProgramCode); 70 ProgramCode.Modules.Add(NewModule); 69 Parser.ParseModule(ProgramCode); 71 70 for I := 0 to ProgramCode.Modules.Count - 1 do begin 72 71 Producer.Produce(TModule(ProgramCode.Modules[I]));
Note:
See TracChangeset
for help on using the changeset viewer.