Changeset 38 for branches/DelphiToC/Analyze/UPascalParser.pas
- Timestamp:
- Aug 5, 2010, 8:47:21 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DelphiToC/Analyze/UPascalParser.pas
r37 r38 14 14 TOnErrorMessage = procedure (Text: string) of object; 15 15 16 TParserCommand = class(TCommonBlock)17 function Parse(Parser: TPascalParser): TCommand;18 end;19 20 16 TParserWhileDo = class(TWhileDo) 21 17 procedure Parse(Parser: TPascalParser); … … 38 34 TParserCommonBlock = class(TCommonBlock) 39 35 procedure Parse(Parser: TPascalParser; EndSymbol: Char = ';'); 36 function ParseCommand(Parser: TPascalParser): TCommand; 40 37 end; 41 38 … … 76 73 ProgramCode: TProgram; 77 74 FOnErrorMessage: TOnErrorMessage; 78 procedure ErrorMessage( Text: string);75 procedure ErrorMessage(const Text: string; const Arguments: array of const); 79 76 public 80 77 CodePosition: Integer; … … 95 92 implementation 96 93 94 resourcestring 95 SUnknownIdentifier = 'Unknown identificator "%s".'; 96 SExpectedButFound = 'Expected "%s" but "%s" found.'; 97 SRedefineIdentifier = 'Identificator "%s" redefinition.'; 98 STypeNotDefined = 'Type "%s" not defined.'; 99 97 100 { TPascalParser } 98 101 99 procedure TPascalParser.ErrorMessage(Text: string); 100 begin 101 if Assigned(FOnErrorMessage) then FOnErrorMessage(Text); 102 procedure TPascalParser.ErrorMessage(const Text: string; const Arguments: array of const); 103 begin 104 if Assigned(FOnErrorMessage) then 105 FOnErrorMessage(Format(Text, Arguments)); 102 106 end; 103 107 … … 106 110 Log('Expected: ' + Code + ' Readed: ' + NextCode); 107 111 if NextCode <> Code then begin 108 ErrorMessage( 'Expected ' + Code + ' but ' + NextCode + ' found.');112 ErrorMessage(SExpectedButFound, [Code, NextCode]); 109 113 end; 110 114 ReadCode; … … 252 256 TParserExpression(Condition).Parse(Parser); 253 257 Expect('do'); 254 TParserCommand(Command).Parse(Parser);258 Command := TParserCommonBlock(CommonBlock).ParseCommand(Parser); 255 259 end; 256 260 end; … … 262 266 Identifier: string; 263 267 NewVariable: TVariable; 268 NewExpression: TExpression; 264 269 Method: TFunction; 265 270 Constant: TConstant; … … 269 274 II: Integer; 270 275 begin 271 (*Expressions := TExpressionList.Create;276 Expressions := TExpressionList.Create; 272 277 Expressions.Add(TExpression.Create); 273 278 with Parser do begin … … 276 281 Identifier := ReadCode; 277 282 if Identifier = '(' then begin 283 // Subexpression 278 284 with TExpression(Expressions[Expressions.Count - 1]) do begin 279 //SubItems[1] := TParserExpression(Self).Parse(Parser); 285 SubItems[1] := TExpression.Create; 286 TParserExpression(SubItems[1]).Parse(Parser); 280 287 end; 281 288 with TExpression(Expressions.Items[Expressions.Add(TExpression.Create)]) do begin … … 285 292 end else 286 293 if IsOperator(Identifier) then begin 294 // Operator 287 295 TExpression(Expressions[Expressions.Count - 1]).OperatorName := Identifier; 288 296 TExpression(Expressions[Expressions.Count - 1]).NodeType := ntOperator; 289 297 end else 290 298 if IsIdentificator(Identifier) then begin 291 NewVariable := Variables.Search(Identifier); 299 // Reference to identificator 300 NewVariable := CommonBlock.Variables.Search(Identifier); 292 301 if Assigned(NewVariable) then begin 302 // Referenced variable 293 303 with TExpression(Expressions[Expressions.Count - 1]) do begin 294 304 SubItems[1] := TExpression.Create; … … 300 310 end; 301 311 end else begin 302 Method := Methods.Search(Identifier);312 Method := CommonBlock.Methods.Search(Identifier); 303 313 if Assigned(Method) then begin 314 // Referenced method 304 315 with TExpression(Expressions[Expressions.Count - 1]) do begin 305 316 SubItems[1] := TExpression.Create; … … 307 318 with TExpression(SubItems[1]) do begin 308 319 Expect('('); 309 SubItems.Add(ParseCommonBlockExpression(CommonBlock)); 320 NewExpression := TExpression.Create; 321 NewExpression.CommonBlock := CommonBlock; 322 TParserExpression(NewExpression).Parse(Parser); 323 SubItems.Add(NewExpression); 310 324 while NextCode = ',' do begin 311 325 Expect(','); 312 SubItems.Add(ParseCommonBlockExpression(CommonBlock)); 326 NewExpression := TExpression.Create; 327 NewExpression.CommonBlock := CommonBlock; 328 TParserExpression(NewExpression).Parse(Parser); 329 SubItems.Add(NewExpression); 313 330 end; 314 331 Expect(')'); … … 321 338 end; 322 339 end else begin 323 Constant := Co nstants.Search(Identifier);340 Constant := CommonBlock.Constants.Search(Identifier); 324 341 if Assigned(Constant) then begin 342 // Referenced constant 325 343 with TExpression(Expressions[Expressions.Count - 1]) do begin 326 344 SubItems[1] := TExpression.Create; … … 332 350 end; 333 351 end else begin 334 ErrorMessage( 'Neznámý identifikátor: ' + Identifier);352 ErrorMessage(SUnknownIdentifier, [Identifier]); 335 353 end; 336 354 end; … … 338 356 end else 339 357 begin 358 // Constant value 340 359 with TExpression(Expressions[Expressions.Count - 1]) do begin 341 360 SubItems[1] := TExpression.Create; … … 372 391 end; 373 392 end; 374 Result := TExpression( Expressions[0]).SubItems[1];393 Result := TExpression(TExpression(Expressions[0]).SubItems[1]); 375 394 TExpression(Expressions[0]).SubItems[1] := nil; 376 395 TExpression(Expressions[1]).SubItems[0] := nil; 377 396 Expressions.Destroy; 378 *) 379 end; 380 381 { TParserCommand } 382 383 function TParserCommand.Parse(Parser: TPascalParser): TCommand; 397 end; 398 399 function TParserCommonBlock.ParseCommand(Parser: TPascalParser): TCommand; 384 400 var 385 401 Identifier: string; … … 395 411 if NextCode = 'begin' then begin 396 412 Result := TBeginEnd.Create; 413 Result.CommonBlock := Self; 397 414 TParserBeginEnd(Result).Parse(Parser); 398 415 end else 399 416 if NextCode = 'if' then begin 400 Result := TIfThenElse.Create; 417 Result := TIfThenElse.Create; 418 Result.CommonBlock := Self; 401 419 TParserIfThenElse(Result).Parse(Parser); 402 420 end else 403 421 if NextCode = 'while' then begin 404 422 Result := TWhileDo.Create; 423 Result.CommonBlock := Self; 405 424 TParserWhileDo(Result).Parse(Parser); 406 425 end else … … 408 427 if Assigned(Variables.Search(NextCode)) then begin 409 428 Result := TAssignment.Create; 429 Result.CommonBlock := Self; 410 430 IdentName := ReadCode; 411 431 TAssignment(Result).Target := Variables.Search(IdentName); 412 432 Expect(':='); 413 TAssignment(Result).Source := TParserExpression(Result).Parse(Parser); 433 TAssignment(Result).Source := TExpression.Create; 434 TAssignment(Result).Source.CommonBlock := Self; 435 TParserExpression(TAssignment(Result).Source).Parse(Parser); 414 436 end else 415 437 if Assigned(Methods.Search(NextCode)) then begin 416 438 Result := TMethodCall.Create; 439 Result.CommonBlock := Self; 417 440 // ParseMetVariable(TMethodCall(Result).Target); 418 441 end; … … 657 680 Expect('begin'); 658 681 while NextCode <> 'end' do begin 659 NewCommand := TParserComm and(Self).Parse(Parser);682 NewCommand := TParserCommonBlock(CommonBlock).ParseCommand(Parser); 660 683 if Assigned(NewCommand) then Commands.Add(NewCommand); 661 684 //ShowMessage(NextCode); … … 696 719 Identifiers.Add(ReadCode); 697 720 end; 698 end else ErrorMessage( 'Pøedefinování existující promìnné.');721 end else ErrorMessage(SRedefineIdentifier, [VariableName]); 699 722 Expect(':'); 700 723 TypeName := ReadCode; 701 724 NewValueType := Parent.Types.Search(TypeName); 702 if not Assigned(NewValueType) then ErrorMessage( 'Typ ' + TypeName + ' nebyl definován.')725 if not Assigned(NewValueType) then ErrorMessage(STypeNotDefined, [TypeName]) 703 726 else for I := 0 to Identifiers.Count - 1 do 704 727 with TParameter(Items[Add(TParameter.Create)]) do begin … … 753 776 Identifiers.Add(ReadCode); 754 777 end; 755 end else ErrorMessage( 'Pøedefinování existující promìnné.');778 end else ErrorMessage(SRedefineIdentifier, [VariableName]); 756 779 Expect(':'); 757 780 TypeName := ReadCode; 758 781 NewValueType := Parent.Types.Search(TypeName); 759 if NewValueType = nil then ErrorMessage( 'Typ ' + TypeName + ' nebyl definován.')782 if NewValueType = nil then ErrorMessage(STypeNotDefined, [TypeName]) 760 783 else for I := 0 to Identifiers.Count - 1 do 761 784 with TVariable(Items[Add(TVariable.Create)]) do begin … … 804 827 Identifiers.Add(ReadCode); 805 828 end; 806 end else ErrorMessage( 'Pøedefinování existující konstanty.');829 end else ErrorMessage(SRedefineIdentifier, [ConstantName]); 807 830 Expect(':'); 808 831 TypeName := ReadCode; … … 812 835 Expect(';'); 813 836 814 if NewValueType = nil then ErrorMessage( 'Typ ' + TypeName + ' nebyl definován.')837 if NewValueType = nil then ErrorMessage(STypeNotDefined, [TypeName]) 815 838 else for I := 0 to Identifiers.Count - 1 do 816 839 with TConstant(Items[Add(TConstant.Create)]) do begin
Note:
See TracChangeset
for help on using the changeset viewer.