Changeset 77 for branches/Transpascal/Compiler/Analyze/UPascalParser.pas
- Timestamp:
- Oct 22, 2010, 9:22:55 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Transpascal/Compiler/Analyze/UPascalParser.pas
r76 r77 32 32 procedure ParseBeginEnd(SourceCode: TBeginEnd); 33 33 procedure ParseFunctionList(SourceCode: TFunctionList; Exported: Boolean = False); 34 procedure ParseFunctionParameters(SourceCode: TFunction); 34 35 procedure ParseIfThenElse(SourceCode: TIfThenElse); 35 36 procedure ParseForToDo(SourceCode: TForToDo); … … 58 59 SUndefinedConstant = 'Undefined constant "%s".'; 59 60 SUnitNotFound = 'Unit "%s" not found.'; 61 SFunctionNotDeclared = 'Function "%s" not declared.'; 62 SUnknownProcName = 'Unknown proc name "%s".'; 60 63 61 64 … … 507 510 { TParserParseFunctionList } 508 511 509 procedure TPascalParser.ParseFunctionList(SourceCode: TFunctionList; Exported: Boolean = False);510 var 511 Identifiers: TStringList; 512 procedure TPascalParser.ParseFunctionList(SourceCode: TFunctionList; 513 Exported: Boolean = False); 514 var 512 515 NewValueType: TType; 513 516 TypeName: string; 517 UseName: string; 518 I: Integer; 519 UseType: TType; 520 UseFunction: TFunction; 521 FunctionType: TFunctionType; 522 begin 523 with SourceCode do begin 524 if NextToken = 'procedure' then begin 525 Expect('procedure'); 526 FunctionType := ftProcedure; 527 end else 528 if NextToken = 'function' then begin 529 Expect('function'); 530 FunctionType := ftFunction; 531 end else 532 if NextToken = 'constructor' then begin 533 Expect('constructor'); 534 FunctionType := ftConstructor; 535 end else 536 if NextToken = 'destructor' then begin 537 Expect('destructor'); 538 FunctionType := ftDestructor; 539 end else ErrorMessage(SUnknownProcName, [NextToken]); 540 541 // Read function name 542 UseName := ReadCode; 543 UseType := SourceCode.Parent.Types.Search(UseName); 544 if Assigned(UseType) and ((UseType is TTypeRecord) or 545 (UseType is TTypeClass)) then begin 546 Expect('.'); 547 UseName := ReadCode; 548 if UseType is TTypeRecord then begin 549 UseFunction := TTypeRecord(UseType).CommonBlock.Functions.Search(UseName); 550 if not Assigned(UseFunction) then begin 551 ErrorMessage(SFunctionNotDeclared, [UseName]); 552 Exit; 553 end; 554 end; 555 end else begin 556 // Create new function 557 UseFunction := TFunction.Create; 558 UseFunction.Parent := SourceCode.Parent; 559 UseFunction.Name := UseName; 560 UseFunction.FunctionType := FunctionType; 561 Add(UseFunction); 562 end; 563 with UseFunction do begin 564 // Parse parameters 565 if NextToken = '(' then 566 ParseFunctionParameters(UseFunction); 567 568 // Parse function result type 569 if FunctionType = ftFunction then begin 570 Expect(':'); 571 TypeName := ReadCode; 572 NewValueType := Parent.Types.Search(TypeName); 573 if not Assigned(NewValueType) then 574 ErrorMessage(SUndefinedType, [TypeName], -1); 575 (* else 576 begin 577 ResultType := NewValueType; 578 with TVariable(Parent.Variables.Items[Parent.Variables.Add( 579 TVariable.Create)]) do 580 begin 581 Name := 'Result'; 582 ValueType := NewValueType; 583 end; 584 end; *) 585 end; 586 Expect(';'); 587 588 // Check directives 589 if NextToken = 'internal' then begin 590 Expect('internal'); 591 Expect(';'); 592 Internal := True; 593 end; 594 end; 595 596 if not Exported then ParseCommonBlock(UseFunction); 597 // if UseFunction then UseFunction.Code ; 598 end; 599 end; 600 601 procedure TPascalParser.ParseFunctionParameters(SourceCode: TFunction); 602 var 603 Identifiers: TStringList; 514 604 VariableName: string; 515 Variable: TParameter; 516 I: integer; 517 begin 605 UseVariable: TParameter; 606 TypeName: string; 607 UseType: TType; 608 I: Integer; 609 begin 610 with SourceCode do 518 611 try 519 Identifiers := TStringList.Create; 520 with SourceCode do begin 521 with TFunction(Items[Add(TFunction.Create)]) do begin 522 Parent := SourceCode.Parent; 523 if NextToken = 'procedure' then begin 524 Expect('procedure'); 525 HaveResult := False; 526 end else begin 527 Expect('function'); 528 HaveResult := True; 529 end; 530 Name := ReadCode; 531 532 if NextToken = '(' then begin 612 Identifiers := TStringList.Create; 533 613 Expect('('); 534 614 while NextToken <> ')' do begin … … 540 620 if VariableName = 'const' then begin 541 621 end else begin 542 Variable := Search(VariableName);543 if not Assigned( Variable) then begin622 UseVariable := Search(VariableName); 623 if not Assigned(UseVariable) then begin 544 624 Identifiers.Add(VariableName); 545 625 while NextToken = ',' do begin … … 551 631 Expect(':'); 552 632 TypeName := ReadCode; 553 NewValueType := Parent.Types.Search(TypeName);554 if not Assigned( NewValueType) then633 UseType := Parent.Types.Search(TypeName); 634 if not Assigned(UseType) then 555 635 ErrorMessage(SUndefinedType, [TypeName], -1) 556 636 else … … 559 639 begin 560 640 Name := Identifiers[I]; 561 ValueType := NewValueType;641 ValueType := UseType; 562 642 end; 563 643 end; … … 566 646 end; 567 647 Expect(')'); 568 569 // Parse function result type570 if HaveResult then begin571 Expect(':');572 TypeName := ReadCode;573 NewValueType := Parent.Types.Search(TypeName);574 if not Assigned(NewValueType) then575 ErrorMessage(SUndefinedType, [TypeName], -1);576 (* else577 begin578 ResultType := NewValueType;579 with TVariable(Parent.Variables.Items[Parent.Variables.Add(580 TVariable.Create)]) do581 begin582 Name := 'Result';583 ValueType := NewValueType;584 end;585 end; *)586 end;587 end;588 Expect(';');589 590 // Check directives591 if NextToken = 'internal' then begin592 Expect('internal');593 Expect(';');594 System := True;595 end;596 end;597 598 if not Exported then ParseCommonBlock(TFunction(Last));599 end;600 648 finally 601 649 Identifiers.Free; … … 924 972 if NextToken = 'var' then begin 925 973 Expect('var'); 926 SectionType := stVar 974 SectionType := stVar; 927 975 end else 928 976 if NextToken = 'const' then begin 929 977 Expect('const'); 930 SectionType := stConst 978 SectionType := stConst; 931 979 end else 932 980 if NextToken = 'type' then begin
Note:
See TracChangeset
for help on using the changeset viewer.