Changeset 34
- Timestamp:
- Aug 4, 2010, 3:10:20 PM (14 years ago)
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DelphiToC
- Property svn:ignore
-
old new 5 5 *.dcu 6 6 ProjectGroup1.bdsgroup 7 ParseLog.txt
-
- Property svn:ignore
-
branches/DelphiToC/Analyze/UPascalParser.pas
r24 r34 24 24 end; 25 25 26 26 TParserModule = class(TModule) 27 procedure Parse(Parser: TPascalParser); 28 procedure ParseUnit(Parser: TPascalParser); 29 procedure ParseProgram(Parser: TPascalParser); 30 end; 31 32 TParserProgram = class(TProgram) 33 procedure Parse(Parser: TPascalParser); 34 end; 35 36 TParserCommonBlock = class(TCommonBlock) 37 procedure Parse(Parser: TPascalParser; EndSymbol: Char = ';'); 38 end; 39 40 TParserBeginEnd = class(TBeginEnd) 41 procedure Parse(Parser: TPascalParser; Command: TBeginEnd); 42 end; 43 44 TParserParseFunction = class(TFunction) 45 procedure Parse(Parser: TPascalParser; Command: TBeginEnd); 46 end; 27 47 28 48 TPascalParser = class … … 43 63 function IsKeyword(Text: string): Boolean; 44 64 function IsOperator(Text: string): Boolean; 45 procedure ParseProgram(AProgram: TProgram);46 procedure ParseModule(Module: TModule);47 procedure ParseModuleUnit(Module: TModule);48 procedure ParseModuleProgram(Module: TModule);49 65 procedure ParseFunction(FunctionList: TFunctionList); 50 66 procedure ParseFunctionParameterList(ParameterList: TParameterList); … … 55 71 procedure ParseTypeList(TypeList: TTypeList); 56 72 procedure ParseType(AType: TType); 57 procedure ParseCommonBlockDefinitions(CommonBlock: TCommonBlock; EndSymbol: string = ';'); 58 function ParseCommonBlockExpression(CommonBlock: TCommonBlock): TExpression; 73 //function ParseCommonBlockExpression(CommonBlock: TCommonBlock): TExpression; 59 74 function ParseCommand(CommonBlock: TCommonBlock): TCommand; 60 75 procedure ParseBeginEnd(CommonBlock: TCommonBlock; Command: TBeginEnd); … … 204 219 end; 205 220 206 procedure TPascalParser.ParseModuleUnit(Module: TModule);207 begin208 with Module do begin209 Expect('unit');210 with TModule(ProgramCode.Modules[0]) do begin211 Name := ReadCode;212 ModuleType := mdUnit;213 end;214 Expect(';');215 //ParseInterface;216 //ParseImplementation;217 end;218 end;219 220 221 function TPascalParser.ReadCode: string; 221 222 begin … … 268 269 end; 269 270 Expect(';'); 270 ParseCommonBlockDefinitions(TFunction(Items[Count - 1]));271 TParserCommonBlock(TFunction(Items[Count - 1])).Parse(Parser); 271 272 end; 272 273 Identifiers.Destroy; … … 285 286 if NextCode = 'else' then begin 286 287 Expect('else'); 287 end;288 end;289 290 procedure TPascalParser.ParseProgram(AProgram: TProgram);291 var292 I: Integer;293 begin294 Log('==== Parse start ====');295 with AProgram do begin296 for I := 0 to Modules.Count - 1 do297 TModule(Modules[I]).Clear;298 Modules.Clear;299 with TModule(Modules[Modules.Add(TModule.Create)]) do begin300 Name := 'main';301 with TType(Types[Types.Add(TType.Create)]) do begin302 Name := 'void';303 Size := 0;304 UsedType := nil;305 end;306 with TType(Types[Types.Add(TType.Create)]) do begin307 Name := 'byte';308 Size := 1;309 UsedType := nil;310 end;311 with TFunction(Methods[Methods.Add(TFunction.Create)]) do begin312 Name := 'exit';313 ResultType := TModule(Modules[0]).Types[0];314 end;315 end;316 ParseModule(TModule(Modules[0]));317 288 end; 318 289 end; … … 365 336 end; 366 337 367 procedure TPascalParser.ParseModuleProgram(Module: TModule);368 var369 Identifier: string;370 begin371 with Module do begin372 if NextCode = 'program' then begin373 Expect('program');374 Name := ReadCode;375 ModuleType := mdProgram;376 Expect(';');377 end else Name := '';378 379 // Uses section380 if NextCode = 'uses' then begin381 Identifier := ReadCode;382 while NextCode = ',' do begin383 Identifier := ReadCode;384 385 end;386 end;387 ParseCommonBlockDefinitions(Module, '.');388 end;389 end;390 391 procedure TPascalParser.ParseModule(Module: TModule);392 begin393 with Module do begin394 if NextCode = 'program' then ParseModuleProgram(Module)395 else if NextCode = 'unit' then ParseModuleUnit(Module)396 else ParseModuleProgram(Module);397 end;398 end;399 400 procedure TPascalParser.ParseBeginEnd(CommonBlock: TCommonBlock; Command: TBeginEnd);401 var402 NewCommand: TCommand;403 begin404 with Command do begin405 Expect('begin');406 while NextCode <> 'end' do begin407 NewCommand := ParseCommand(CommonBlock);408 if Assigned(NewCommand) then Commands.Add(NewCommand);409 //ShowMessage(NextCode);410 if NextCode = ';' then ReadCode;411 end;412 Expect('end');413 end;414 end;415 416 procedure TPascalParser.ParseCommonBlockDefinitions(CommonBlock: TCommonBlock; EndSymbol: string = ';');417 begin418 with CommonBlock do begin419 while NextCode <> EndSymbol do begin420 if NextCode = 'var' then ParseVariableList(TVariableList(Variables))421 else if NextCode = 'const' then ParseConstantList(TConstantList(Constants))422 else if NextCode = 'type' then ParseTypeList(TTypeList(Types))423 else if NextCode = 'procedure' then ParseFunction(Methods)424 else begin425 ParseBeginEnd(CommonBlock, Code);426 Break;427 end;428 end;429 Expect(EndSymbol);430 end;431 end;432 433 338 function TPascalParser.ParseCommand(CommonBlock: TCommonBlock): TCommand; 434 339 var … … 442 347 IdentName: string; 443 348 begin 444 if NextCode = 'begin' then begin349 (* if NextCode = 'begin' then begin 445 350 Result := TBeginEnd.Create; 446 351 ParseBeginEnd(CommonBlock, TBeginEnd(Result)); … … 688 593 II: Integer; 689 594 begin 690 Expressions := TExpressionList.Create;595 (* Expressions := TExpressionList.Create; 691 596 Expressions.Add(TExpression.Create); 692 with CommonBlockdo begin597 with Parser do begin 693 598 while ((NextCode <> ';') and (NextCode <> ',') and (not IsKeyWord(NextCode))) and 694 599 not (((NextCode = ')') or (NextCode = ']'))) do begin … … 795 700 TExpression(Expressions[1]).SubItems[0] := nil; 796 701 Expressions.Destroy; 702 *) 703 end; 704 705 { TParserCommand } 706 707 procedure TParserCommand.Parse(Parser: TPascalParser); 708 begin 709 710 end; 711 712 { TParserModule } 713 714 procedure TParserModule.Parse(Parser: TPascalParser); 715 begin 716 with Parser do begin 717 if NextCode = 'program' then ParseProgram(Parser) 718 else if NextCode = 'unit' then ParseUnit(Parser) 719 else ParseProgram(Parser); 720 end; 721 end; 722 723 procedure TParserModule.ParseProgram(Parser: TPascalParser); 724 var 725 Identifier: string; 726 begin 727 with Parser do begin 728 if NextCode = 'program' then begin 729 Expect('program'); 730 Name := ReadCode; 731 ModuleType := mdProgram; 732 Expect(';'); 733 end else Name := ''; 734 735 // Uses section 736 if NextCode = 'uses' then begin 737 Identifier := ReadCode; 738 while NextCode = ',' do begin 739 Identifier := ReadCode; 740 741 end; 742 end; 743 TParserCommonBlock(Self).Parse(Parser, '.'); 744 end; 745 end; 746 747 procedure TParserModule.ParseUnit(Parser: TPascalParser); 748 begin 749 with Parser do begin 750 Expect('unit'); 751 with TModule(ProgramCode.Modules[0]) do begin 752 Name := ReadCode; 753 ModuleType := mdUnit; 754 end; 755 Expect(';'); 756 //ParseInterface; 757 //ParseImplementation; 758 end; 759 end; 760 761 { TParserProgram } 762 763 procedure TParserProgram.Parse(Parser: TPascalParser); 764 var 765 I: Integer; 766 begin 767 with Parser do begin 768 Log('==== Parse start ===='); 769 Modules.Clear; 770 with TModule(Modules[Modules.Add(TModule.Create)]) do begin 771 Name := 'main'; 772 with TType(Types[Types.Add(TType.Create)]) do begin 773 Name := 'void'; 774 Size := 0; 775 UsedType := nil; 776 end; 777 with TType(Types[Types.Add(TType.Create)]) do begin 778 Name := 'byte'; 779 Size := 1; 780 UsedType := nil; 781 end; 782 with TFunction(Methods[Methods.Add(TFunction.Create)]) do begin 783 Name := 'exit'; 784 ResultType := TType(TModule(Modules[0]).Types[0]); 785 end; 786 end; 787 TParserModule(TModule(Modules[0])).Parse(Parser); 788 end; 789 end; 790 791 { TParserCommonBlock } 792 793 procedure TParserCommonBlock.Parse(Parser: TPascalParser; EndSymbol: Char = ';'); 794 begin 795 with Parser do begin 796 while NextCode <> EndSymbol do begin 797 if NextCode = 'var' then ParseVariableList(TVariableList(Variables)) 798 else if NextCode = 'const' then ParseConstantList(TConstantList(Constants)) 799 else if NextCode = 'type' then ParseTypeList(TTypeList(Types)) 800 else if NextCode = 'procedure' then ParseFunction(Methods) 801 else begin 802 ParseBeginEnd(CommonBlock, Code); 803 Break; 804 end; 805 end; 806 Expect(EndSymbol); 807 end; 808 end; 809 810 { TParserBeginEnd } 811 812 procedure TParserBeginEnd.Parse(Parser: TPascalParser; Command: TBeginEnd); 813 var 814 NewCommand: TCommand; 815 begin 816 with Parser do begin 817 Expect('begin'); 818 while NextCode <> 'end' do begin 819 NewCommand := ParseCommand(CommonBlock); 820 if Assigned(NewCommand) then Commands.Add(NewCommand); 821 //ShowMessage(NextCode); 822 if NextCode = ';' then ReadCode; 823 end; 824 Expect('end'); 825 end; 797 826 end; 798 827 -
branches/DelphiToC/UPascalSource.pas
r20 r34 5 5 uses 6 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls ;7 Dialogs, StdCtrls, Contnrs; 8 8 9 9 type … … 102 102 TCaseOfEnd = class(TCommand) 103 103 Expression: TExpression; 104 Branches: T List; // TList<TCaseOfEndBranche>104 Branches: TObjectList; // TObjectList<TCaseOfEndBranche> 105 105 ElseCommand: TCommand; 106 constructor Create; 107 destructor Destroy; override; 106 108 end; 107 109 … … 116 118 end; 117 119 118 TCommandList = class(T List)120 TCommandList = class(TObjectList) 119 121 120 122 end; … … 146 148 147 149 TTypeRecord = class 148 Items: T List; // TList<TTypeRecordItem>150 Items: TObjectList; // TObjectList<TTypeRecordItem> 149 151 end; 150 152 … … 154 156 end; 155 157 156 TTypeList = class(T List)158 TTypeList = class(TObjectList) 157 159 Parent: TCommonBlock; 158 160 function Search(Name: string): TType; … … 166 168 end; 167 169 168 TConstantList = class(T List)170 TConstantList = class(TObjectList) 169 171 Parent: TCommonBlock; 170 172 function Search(Name: string): TConstant; … … 178 180 end; 179 181 180 TVariableList = class(T List)182 TVariableList = class(TObjectList) 181 183 Parent: TCommonBlock; 182 184 function Search(Name: string): TVariable; … … 190 192 end; 191 193 192 TParameterList = class(T List)194 TParameterList = class(TObjectList) 193 195 Parent: TFunction; 194 196 function Search(Name: string): TParameter; … … 208 210 end; 209 211 210 TExpressionList = class(T List)212 TExpressionList = class(TObjectList) 211 213 destructor Destroy; override; 212 214 end; … … 221 223 end; 222 224 223 TOperationList = class(T List)225 TOperationList = class(TObjectList) 224 226 destructor Destroy; override; 225 227 end; … … 227 229 TFunction = class(TCommonBlock) 228 230 public 229 Parameters: T List; // TList<TVariable>231 Parameters: TObjectList; // TObjectList<TVariable> 230 232 ResultType: TType; 231 233 constructor Create; override; … … 233 235 end; 234 236 235 TFunctionList = class(T List)237 TFunctionList = class(TObjectList) 236 238 Parent: TCommonBlock; 237 239 function Search(Name: string): TFunction; … … 242 244 public 243 245 ModuleType: TModuleType; 244 UsedModules: T List; // TList<TModule>246 UsedModules: TObjectList; // TObjectList<TModule> 245 247 constructor Create; override; 246 248 procedure Clear; … … 250 252 TProgram = class 251 253 Device: TDevice; 252 Modules: T List; // TList<TModule>254 Modules: TObjectList; // TObjectList<TModule> 253 255 constructor Create; 254 256 destructor Destroy; override; … … 290 292 begin 291 293 Device := TDevice.Create; 292 Modules := T List.Create;294 Modules := TObjectList.Create; 293 295 end; 294 296 295 297 destructor TProgram.Destroy; 296 var 297 I: Integer; 298 begin 299 for I := 0 to Modules.Count - 1 do 300 TModule(Modules[I]).Free; 298 begin 301 299 Modules.Free; 302 300 Device.Free; … … 323 321 I := 0; 324 322 while (I < Count) and (TConstant(Items[I]).Name <> Name) do Inc(I); 325 if I < Count then Result := Items[I]else begin323 if I < Count then Result := TConstant(Items[I]) else begin 326 324 if Assigned(Parent.Parent) then Result := Parent.Parent.Constants.Search(Name) 327 325 else begin … … 344 342 begin 345 343 inherited; 346 UsedModules := T List.Create;344 UsedModules := TObjectList.Create; 347 345 end; 348 346 … … 406 404 I := 0; 407 405 while (I < Count) and (TType(Items[I]).Name <> Name) do Inc(I); 408 if I < Count then Result := Items[I]else begin406 if I < Count then Result := TType(Items[I]) else begin 409 407 if Assigned(Parent.Parent) then Result := Parent.Parent.Types.Search(Name) 410 408 else begin … … 417 415 418 416 destructor TVariableList.Destroy; 419 var 420 I: Integer; 421 begin 422 for I := 0 to Count - 1 do 423 TVariable(Items[I]).Free; 417 begin 424 418 inherited; 425 419 end; … … 431 425 I := 0; 432 426 while (I < Count) and (TVariable(Items[I]).Name <> Name) do Inc(I); 433 if I < Count then Result := Items[I]else begin427 if I < Count then Result := TVariable(Items[I]) else begin 434 428 if Assigned(Parent.Parent) then Result := Parent.Parent.Variables.Search(Name) 435 429 else begin … … 461 455 I := 0; 462 456 while (I < Count) and (TFunction(Items[I]).Name <> Name) do Inc(I); 463 if I < Count then Result := Items[I]else begin457 if I < Count then Result := TFunction(Items[I]) else begin 464 458 if Assigned(Parent.Parent) then Result := Parent.Parent.Methods.Search(Name) 465 459 else begin … … 530 524 I := 0; 531 525 while (I < Count) and (TParameter(Items[I]).Name <> Name) do Inc(I); 532 if I < Count then Result := Items[I] else Result := nil; 526 if I < Count then Result := TParameter(Items[I]) 527 else Result := nil; 533 528 end; 534 529 … … 579 574 end; 580 575 576 { TCaseOfEnd } 577 578 constructor TCaseOfEnd.Create; 579 begin 580 Branches := TObjectList.Create 581 end; 582 583 destructor TCaseOfEnd.Destroy; 584 begin 585 Branches.Destroy; 586 inherited; 587 end; 588 581 589 end. 582 590
Note:
See TracChangeset
for help on using the changeset viewer.