Changeset 20
- Timestamp:
- Sep 8, 2009, 2:01:55 PM (15 years ago)
- Files:
-
- 10 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DelphiToC/Example.pas
r19 r20 1 1 program Test; 2 2 3 procedure Pokus ;3 procedure Pokus(A: Byte); 4 4 begin 5 5 end; 6 6 7 7 const 8 Verze: Byte = 1 0;8 Verze: Byte = 11; 9 9 var 10 a: B pyte;10 a: Byte; 11 11 B: Byte; 12 12 sS: Byte; 13 13 begin 14 14 A := 1; 15 While A < 1 do A := A + 1; 15 16 end. -
branches/DelphiToC/UAssemblerSource.pas
r14 r20 28 28 procedure AddInstruction(LabelName, Instruction, Operand1, 29 29 Operand2: string); 30 procedure GenerateCommonBlock(CommonBlock: TCommonBlock; LabelPrefix: string);30 // procedure GenerateCommonBlock(CommonBlock: TCommonBlock; LabelPrefix: string); 31 31 procedure GenerateExpression(Expression: TExpression; LabelPrefix: string); 32 32 procedure GenerateProgram(ProgramBlock: TProgram); … … 114 114 end; 115 115 116 procedure TAssemblerProducer.GenerateCommonBlock(CommonBlock: TCommonBlock; LabelPrefix: string); 117 var 118 I: Integer; 119 LabelName: string; 120 begin 121 with CommonBlock do 122 for I := 0 to Operations.Count - 1 do 123 with TOperation(Operations[I]) do begin 124 if Referenced then LabelName := Name + '_L' + IntToStr(I) 125 else LabelName := ''; 126 case Instruction of 127 inJump: begin 128 AddInstruction(LabelName, 'JMP', Name + '_L' + IntToStr(GotoAddress), ''); 116 (* 117 procedure TAssemblerProducer.GenerateCommonBlock(CommonBlock: TCommonBlock; LabelPrefix: string); 118 var 119 I: Integer; 120 LabelName: string; 121 begin 122 with CommonBlock do 123 for I := 0 to Operations.Count - 1 do 124 with TOperation(Operations[I]) do begin 125 if Referenced then LabelName := Name + '_L' + IntToStr(I) 126 else LabelName := ''; 127 case Instruction of 128 inJump: begin 129 AddInstruction(LabelName, 'JMP', Name + '_L' + IntToStr(GotoAddress), ''); 130 end; 131 inConditionalJump: begin 132 GenerateExpression(ExpressionTree, LabelPrefix + '_L' + IntToStr(GotoAddress)); 133 AddInstruction(LabelName, 'BRCS', Name + '_L' + IntToStr(GotoAddress), ''); 134 end; 135 inExpressionEvaluation: begin 136 if LabelName <> '' then AddInstruction(LabelName, '', '', ''); 137 GenerateExpression(ExpressionTree, Name + '_L' + IntToStr(GotoAddress)); 138 end; 139 inReturn: 140 AddInstruction(LabelName, 'RET', '', ''); 129 141 end; 130 inConditionalJump: begin131 GenerateExpression(ExpressionTree, LabelPrefix + '_L' + IntToStr(GotoAddress));132 AddInstruction(LabelName, 'BRCS', Name + '_L' + IntToStr(GotoAddress), '');133 end;134 inExpressionEvaluation: begin135 if LabelName <> '' then AddInstruction(LabelName, '', '', '');136 GenerateExpression(ExpressionTree, Name + '_L' + IntToStr(GotoAddress));137 end;138 inReturn:139 AddInstruction(LabelName, 'RET', '', '');140 142 end; 141 143 end; 142 end; 144 *) 143 145 144 146 procedure TAssemblerProducer.GenerateExpression(Expression: TExpression; LabelPrefix: string); … … 180 182 I: Integer; 181 183 begin 182 with ProgramBlock do183 for I := 0 to Modules.Count - 1 do184 GenerateCommonBlock(TModule(Modules[I]), '');184 // with ProgramBlock do 185 // for I := 0 to Modules.Count - 1 do 186 // GenerateCommonBlock(TModule(Modules[I]), ''); 185 187 end; 186 188 -
branches/DelphiToC/UCSource.pas
r19 r20 37 37 begin 38 38 inherited; 39 TextSource.Clear; 39 40 GenerateProgram(ProgramCode); 40 41 end; -
branches/DelphiToC/UMainForm.pas
r19 r20 65 65 NewNode: TTreeNode; 66 66 NewNode2: TTreeNode; 67 NewNode3: TTreeNode; 67 68 ModuleNode: TTreeNode; 68 69 I: Integer; 70 II: Integer; 69 71 M: Integer; 70 72 begin … … 82 84 NewNode := AddChild(ModuleNode, 'Funkce'); 83 85 for I := 0 to Methods.Count - 1 do 84 with TFunction(Methods[I]) do 86 with TFunction(Methods[I]) do begin 85 87 NewNode2 := AddChild(NewNode, Name); 88 with NewNode2 do begin 89 NewNode3 := AddChild(NewNode2, 'Parametery'); 90 for II := 0 to Parameters.Count - 1 do 91 AddChild(NewNode3, TParameter(Parameters[II]).Name + ':' + TParameter(Parameters[II]).ValueType.Name); 92 end; 93 end; 86 94 NewNode := AddChild(ModuleNode, 'Promìnné'); 87 95 for I := 0 to Variables.Count - 1 do 88 96 with TVariable(Variables[I]) do 89 NewNode2 := AddChild(NewNode, Name );97 NewNode2 := AddChild(NewNode, Name + ':' + ValueType.Name); 90 98 NewNode := AddChild(ModuleNode, 'Konstanty'); 91 99 for I := 0 to Constants.Count - 1 do 92 100 with TConstant(Constants[I]) do 93 NewNode2 := AddChild(NewNode, Name );101 NewNode2 := AddChild(NewNode, Name + ':' + ValueType.Name + '=' + Value); 94 102 NewNode := AddChild(ModuleNode, 'Program'); 103 for I := 0 to Code.Commands.Count - 1 do begin 104 if TObject(Code.Commands[I]) is TBeginEnd then begin 105 NewNode2 := AddChild(NewNode, 'Begin-End'); 106 107 end else 108 if TObject(Code.Commands[I]) is TWhileDo then begin 109 NewNode2 := AddChild(NewNode, 'While-Do'); 110 end else 111 if TObject(Code.Commands[I]) is TAssignment then begin 112 NewNode2 := AddChild(NewNode, TAssignment(Code.Commands[I]).Target.Name + ' := exp'); 113 114 end; 115 end; 116 95 117 end; 96 118 TopItem.Expand(True); -
branches/DelphiToC/UPascalParser.pas
r19 r20 8 8 9 9 type 10 TPascalParser = class; 11 10 12 TOnErrorMessage = procedure (Text: string) of object; 13 14 TParserCommand = class(TCommand) 15 procedure Parse(Parser: TPascalParser); 16 end; 17 18 TParserWhileDo = class(TWhileDo) 19 procedure Parse(Parser: TPascalParser); 20 end; 21 22 TParserExpression = class(TExpression) 23 procedure Parse(Parser: TPascalParser); 24 end; 25 26 11 27 12 28 TPascalParser = class … … 32 48 procedure ParseModuleProgram(Module: TModule); 33 49 procedure ParseFunction(FunctionList: TFunctionList); 50 procedure ParseFunctionParameterList(ParameterList: TParameterList); 34 51 procedure ParseVariableList(VariableList: TVariableList); 35 52 procedure ParseVariable(Variable: TVariable); … … 40 57 procedure ParseCommonBlockDefinitions(CommonBlock: TCommonBlock; EndSymbol: string = ';'); 41 58 function ParseCommonBlockExpression(CommonBlock: TCommonBlock): TExpression; 42 procedure ParseCommonBlockProgramCode(CommonBlock: TCommonBlock); 43 procedure ParseCommonBlockOperation(CommonBlock: TCommonBlock); 59 function ParseCommand(CommonBlock: TCommonBlock): TCommand; 60 procedure ParseBeginEnd(CommonBlock: TCommonBlock; Command: TBeginEnd); 61 procedure ParseIfThenElse(CommonBlock: TCommonBlock; Command: TIfThenElse); 62 procedure ParseWhileDo(CommonBlock: TCommonBlock; Command: TWhileDo); 44 63 procedure Log(Text: string); 45 64 property OnErrorMessage: TOnErrorMessage read FOnErrorMessage write FOnErrorMessage; … … 206 225 207 226 procedure TPascalParser.ParseFunction(FunctionList: TFunctionList); 208 begin 227 var 228 Identifiers: TStringList; 229 NewValueType: TType; 230 TypeName: string; 231 VariableName: string; 232 Variable: TParameter; 233 I: Integer; 234 begin 235 Identifiers := TStringList.Create; 209 236 with FunctionList do begin 210 237 with TFunction(Items[Add(TFunction.Create)]) do begin 238 Parent := FunctionList.Parent; 211 239 Expect('procedure'); 212 240 Name := ReadCode; 213 Expect(';'); 214 ParseCommonBlockDefinitions(Items[Count - 1]); 215 end; 241 if NextCode = '(' then begin 242 Expect('('); 243 while NextCode <> ')' do begin 244 // while IsIdentificator(NextCode) do begin 245 with TParameterList(Parameters) do begin 246 VariableName := ReadCode; 247 Variable := Search(VariableName); 248 if not Assigned(Variable) then begin 249 Identifiers.Add(VariableName); 250 while NextCode = ',' do begin 251 Expect(','); 252 Identifiers.Add(ReadCode); 253 end; 254 end else ErrorMessage('Pøedefinování existující promìnné.'); 255 Expect(':'); 256 TypeName := ReadCode; 257 NewValueType := Parent.Types.Search(TypeName); 258 if not Assigned(NewValueType) then ErrorMessage('Typ ' + TypeName + ' nebyl definován.') 259 else for I := 0 to Identifiers.Count - 1 do 260 with TParameter(Items[Add(TParameter.Create)]) do begin 261 Name := Identifiers[I]; 262 ValueType := NewValueType; 263 end; 264 end; 265 end; 266 Expect(')'); 267 end; 268 end; 269 Expect(';'); 270 ParseCommonBlockDefinitions(TFunction(Items[Count - 1])); 271 end; 272 Identifiers.Destroy; 273 end; 274 275 procedure TPascalParser.ParseFunctionParameterList( 276 ParameterList: TParameterList); 277 begin 278 279 end; 280 281 procedure TPascalParser.ParseIfThenElse(CommonBlock: TCommonBlock; Command: TIfThenElse); 282 begin 283 Expect('if'); 284 Expect('than'); 285 if NextCode = 'else' then begin 286 Expect('else'); 216 287 end; 217 288 end; … … 229 300 Name := 'main'; 230 301 with TType(Types[Types.Add(TType.Create)]) do begin 302 Name := 'void'; 303 Size := 0; 304 UsedType := nil; 305 end; 306 with TType(Types[Types.Add(TType.Create)]) do begin 231 307 Name := 'byte'; 232 308 Size := 1; 233 309 UsedType := nil; 310 end; 311 with TFunction(Methods[Methods.Add(TFunction.Create)]) do begin 312 Name := 'exit'; 313 ResultType := TModule(Modules[0]).Types[0]; 234 314 end; 235 315 end; … … 318 398 end; 319 399 400 procedure TPascalParser.ParseBeginEnd(CommonBlock: TCommonBlock; Command: TBeginEnd); 401 var 402 NewCommand: TCommand; 403 begin 404 with Command do begin 405 Expect('begin'); 406 while NextCode <> 'end' do begin 407 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 320 416 procedure TPascalParser.ParseCommonBlockDefinitions(CommonBlock: TCommonBlock; EndSymbol: string = ';'); 321 417 begin … … 327 423 else if NextCode = 'procedure' then ParseFunction(Methods) 328 424 else begin 329 Parse CommonBlockProgramCode(CommonBlock);425 ParseBeginEnd(CommonBlock, Code); 330 426 Break; 331 427 end; … … 335 431 end; 336 432 337 function TPascalParser.ParseCommonBlockExpression(CommonBlock: TCommonBlock): TExpression; 433 function TPascalParser.ParseCommand(CommonBlock: TCommonBlock): TCommand; 434 var 435 Identifier: string; 436 Variable: TVariable; 437 Method: TFunction; 438 First: TOperation; 439 Second: TOperation; 440 StartIndex: Integer; 441 LoopVariable: TVariable; 442 IdentName: string; 443 begin 444 if NextCode = 'begin' then begin 445 Result := TBeginEnd.Create; 446 ParseBeginEnd(CommonBlock, TBeginEnd(Result)); 447 end else 448 if NextCode = 'if' then begin 449 Result := TIfThenElse.Create; 450 ParseIfThenElse(CommonBlock, TIfThenElse(Result)); 451 end else 452 if NextCode = 'while' then begin 453 Result := TWhileDo.Create; 454 ParseWhileDo(CommonBlock, TWhileDo(Result)); 455 end else 456 if IsIdentificator(NextCode) then begin 457 if Assigned(CommonBlock.Variables.Search(NextCode)) then begin 458 Result := TAssignment.Create; 459 IdentName := ReadCode; 460 TAssignment(Result).Target := CommonBlock.Variables.Search(IdentName); 461 Expect(':='); 462 TAssignment(Result).Source := ParseCommonBlockExpression(CommonBlock); 463 end else 464 if Assigned(CommonBlock.Methods.Search(NextCode)) then begin 465 Result := TMethodCall.Create; 466 // ParseMetVariable(TMethodCall(Result).Target); 467 end; 468 end; 469 470 (* begin 471 Expect('if'); 472 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin 473 Instruction := inConditionalJump; 474 ExpressionTree := ParseCommonBlockExpression(CommonBlock); 475 Negative := True; 476 end; 477 First := Operations[Operations.Count - 1]; 478 Expect('then'); 479 ParseCommonBlockOperation(CommonBlock); 480 if NextCode = 'else' then begin 481 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin 482 Instruction := inJump; 483 end; 484 Second := Operations[Operations.Count - 1]; 485 First.GotoAddress := Operations.Count; 486 Expect('else'); 487 ParseCommonBlockOperation(CommonBlock); 488 Second.GotoAddress := Operations.Count; 489 end else First.GotoAddress := Operations.Count; 490 end 491 else if NextCode = 'repeat' then begin 492 Expect('repeat'); 493 StartIndex := Operations.Count; 494 ParseCommonBlockOperation(CommonBlock); 495 Expect('until'); 496 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin 497 Instruction := inConditionalJump; 498 ExpressionTree := ParseCommonBlockExpression(CommonBlock); 499 GotoAddress := StartIndex; 500 end; 501 end 502 else if NextCode = 'while' then begin 503 Expect('while'); 504 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin 505 Instruction := inConditionalJump; 506 ExpressionTree := ParseCommonBlockExpression(CommonBlock); 507 end; 508 First := Operations[Operations.Count - 1]; 509 StartIndex := Operations.Count - 1; 510 Expect('do'); 511 ParseCommonBlockOperation(CommonBlock); 512 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin 513 Instruction := inJump; 514 GotoAddress := StartIndex; 515 end; 516 First.GotoAddress := Operations.Count; 517 end 518 else if NextCode = 'for' then begin 519 Expect('for'); 520 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin 521 Instruction := inExpressionEvaluation; 522 ExpressionTree := ParseCommonBlockExpression(CommonBlock); 523 if (ExpressionTree.NodeType <> ntOperator) and 524 (ExpressionTree.OperatorName <> ':=') then ErrorMessage('Expected assigment in for loop'); 525 if TExpression(TExpression(ExpressionTree).SubItems[0]).NodeType <> ntVariable then 526 ErrorMessage('Index in FOR loop have to be variable'); 527 LoopVaraible := TExpression(TExpression(ExpressionTree).SubItems[0]).Variable; 528 end; 529 Expect('to'); 530 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin 531 Instruction := inExpressionEvaluation; 532 ExpressionTree := TExpression.Create; 533 with ExpressionTree do begin 534 NodeType := ntOperator; 535 OperatorName := '='; 536 SubItems[0] := TExpression.Create; 537 with TExpression(SubItems[0]) do begin 538 NodeType := ntVariable; 539 Variable := LoopVaraible; 540 end; 541 SubItems[1] := ParseCommonBlockExpression(CommonBlock); 542 end; 543 Negative := True; 544 end; 545 First := Operations[Operations.Count - 1]; 546 StartIndex := Operations.Count - 1; 547 Expect('do'); 548 ParseCommonBlockOperation(CommonBlock); 549 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin 550 Instruction := inExpressionEvaluation; 551 ExpressionTree := TExpression.Create; 552 with ExpressionTree do begin 553 NodeType := ntOperator; 554 OperatorName := ':='; 555 SubItems[0] := TExpression.Create; 556 with TExpression(SubItems[0]) do begin 557 NodeType := ntVariable; 558 Variable := LoopVaraible; 559 end; 560 SubItems[1] := TExpression.Create; 561 with TExpression(SubItems[1]) do begin 562 NodeType := ntOperator; 563 OperatorName := '+'; 564 SubItems[0] := TExpression.Create; 565 with TExpression(SubItems[0]) do begin 566 NodeType := ntVariable; 567 Variable := LoopVaraible; 568 end; 569 SubItems[1] := TExpression.Create; 570 with TExpression(SubItems[1]) do begin 571 NodeType := ntConstant; 572 //SetLength(Value, 1); 573 //Value[0] := 1; 574 Value := 1; 575 end; 576 end; 577 end; 578 end; 579 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin 580 Instruction := inJump; 581 GotoAddress := StartIndex; 582 end; 583 First.GotoAddress := Operations.Count; 584 end 585 else begin 586 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin 587 Instruction := inExpressionEvaluation; 588 ExpressionTree := ParseCommonBlockExpression(CommonBlock); 589 end; 590 end; 591 *) 592 end; 593 594 procedure TPascalParser.ParseTypeList(TypeList: TTypeList); 595 begin 596 with TypeList do begin 597 Expect('type'); 598 while IsIdentificator(NextCode) do 599 with TType(Items[Add(TType.Create)]) do begin 600 Parent := TypeList; 601 ParseType(TType(Items[Count - 1])); 602 end; 603 end; 604 end; 605 606 procedure TPascalParser.ParseVariableList(VariableList: TVariableList); 607 var 608 Identifiers: TStringList; 609 NewValueType: TType; 610 TypeName: string; 611 VariableName: string; 612 Variable: TVariable; 613 I: Integer; 614 begin 615 Identifiers := TStringList.Create; 616 with VariableList do begin 617 Expect('var'); 618 while IsIdentificator(NextCode) do begin 619 VariableName := ReadCode; 620 Variable := Search(VariableName); 621 if not Assigned(Variable) then begin 622 Identifiers.Add(VariableName); 623 while NextCode = ',' do begin 624 Expect(','); 625 Identifiers.Add(ReadCode); 626 end; 627 end else ErrorMessage('Pøedefinování existující promìnné.'); 628 Expect(':'); 629 TypeName := ReadCode; 630 NewValueType := Parent.Types.Search(TypeName); 631 if NewValueType = nil then ErrorMessage('Typ ' + TypeName + ' nebyl definován.') 632 else for I := 0 to Identifiers.Count - 1 do 633 with TVariable(Items[Add(TVariable.Create)]) do begin 634 Name := Identifiers[I]; 635 ValueType := NewValueType; 636 end; 637 Expect(';'); 638 end; 639 end; 640 Identifiers.Destroy; 641 end; 642 643 procedure TPascalParser.ParseWhileDo(CommonBlock: TCommonBlock; Command: TWhileDo); 644 begin 645 end; 646 647 procedure TPascalParser.ParseVariable(Variable: TVariable); 648 begin 649 with Variable do begin 650 Name := NextCode; 651 Expect(':='); 652 653 end; 654 end; 655 656 procedure TPascalParser.ParseType(AType: TType); 657 begin 658 with AType do begin 659 Name := NextCode; 660 Expect('='); 661 UsedType := Parent.Search(NextCode); 662 end; 663 end; 664 665 { TParserWhileDo } 666 667 procedure TParserWhileDo.Parse(Parser: TPascalParser); 668 begin 669 with Parser do begin 670 Expect('while'); 671 TParserExpression(Condition).Parse(Parser); 672 Expect('do'); 673 TParserCommand(Command).Parse(Parser); 674 end; 675 end; 676 677 { TExpression } 678 679 procedure TParserExpression.Parse(Parser: TPascalParser); 338 680 var 339 681 Identifier: string; … … 455 797 end; 456 798 457 procedure TPascalParser.ParseCommonBlockOperation(CommonBlock: TCommonBlock);458 var459 Identifier: string;460 Variable: TVariable;461 Method: TFunction;462 First: TOperation;463 Second: TOperation;464 StartIndex: Integer;465 LoopVaraible: TVariable;466 begin467 with CommonBlock do begin468 if NextCode = 'begin' then ParseCommonBlockProgramCode(CommonBlock)469 else if NextCode = 'if' then begin470 Expect('if');471 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin472 Instruction := inConditionalJump;473 ExpressionTree := ParseCommonBlockExpression(CommonBlock);474 Negative := True;475 end;476 First := Operations[Operations.Count - 1];477 Expect('then');478 ParseCommonBlockOperation(CommonBlock);479 if NextCode = 'else' then begin480 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin481 Instruction := inJump;482 end;483 Second := Operations[Operations.Count - 1];484 First.GotoAddress := Operations.Count;485 Expect('else');486 ParseCommonBlockOperation(CommonBlock);487 Second.GotoAddress := Operations.Count;488 end else First.GotoAddress := Operations.Count;489 end490 else if NextCode = 'repeat' then begin491 Expect('repeat');492 StartIndex := Operations.Count;493 ParseCommonBlockOperation(CommonBlock);494 Expect('until');495 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin496 Instruction := inConditionalJump;497 ExpressionTree := ParseCommonBlockExpression(CommonBlock);498 GotoAddress := StartIndex;499 end;500 end501 else if NextCode = 'while' then begin502 Expect('while');503 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin504 Instruction := inConditionalJump;505 ExpressionTree := ParseCommonBlockExpression(CommonBlock);506 end;507 First := Operations[Operations.Count - 1];508 StartIndex := Operations.Count - 1;509 Expect('do');510 ParseCommonBlockOperation(CommonBlock);511 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin512 Instruction := inJump;513 GotoAddress := StartIndex;514 end;515 First.GotoAddress := Operations.Count;516 end517 else if NextCode = 'for' then begin518 Expect('for');519 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin520 Instruction := inExpressionEvaluation;521 ExpressionTree := ParseCommonBlockExpression(CommonBlock);522 if (ExpressionTree.NodeType <> ntOperator) and523 (ExpressionTree.OperatorName <> ':=') then ErrorMessage('Expected assigment in for loop');524 if TExpression(TExpression(ExpressionTree).SubItems[0]).NodeType <> ntVariable then525 ErrorMessage('Index in FOR loop have to be variable');526 LoopVaraible := TExpression(TExpression(ExpressionTree).SubItems[0]).Variable;527 end;528 Expect('to');529 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin530 Instruction := inExpressionEvaluation;531 ExpressionTree := TExpression.Create;532 with ExpressionTree do begin533 NodeType := ntOperator;534 OperatorName := '=';535 SubItems[0] := TExpression.Create;536 with TExpression(SubItems[0]) do begin537 NodeType := ntVariable;538 Variable := LoopVaraible;539 end;540 SubItems[1] := ParseCommonBlockExpression(CommonBlock);541 end;542 Negative := True;543 end;544 First := Operations[Operations.Count - 1];545 StartIndex := Operations.Count - 1;546 Expect('do');547 ParseCommonBlockOperation(CommonBlock);548 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin549 Instruction := inExpressionEvaluation;550 ExpressionTree := TExpression.Create;551 with ExpressionTree do begin552 NodeType := ntOperator;553 OperatorName := ':=';554 SubItems[0] := TExpression.Create;555 with TExpression(SubItems[0]) do begin556 NodeType := ntVariable;557 Variable := LoopVaraible;558 end;559 SubItems[1] := TExpression.Create;560 with TExpression(SubItems[1]) do begin561 NodeType := ntOperator;562 OperatorName := '+';563 SubItems[0] := TExpression.Create;564 with TExpression(SubItems[0]) do begin565 NodeType := ntVariable;566 Variable := LoopVaraible;567 end;568 SubItems[1] := TExpression.Create;569 with TExpression(SubItems[1]) do begin570 NodeType := ntConstant;571 //SetLength(Value, 1);572 //Value[0] := 1;573 Value := 1;574 end;575 end;576 end;577 end;578 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin579 Instruction := inJump;580 GotoAddress := StartIndex;581 end;582 First.GotoAddress := Operations.Count;583 end584 else begin585 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin586 Instruction := inExpressionEvaluation;587 ExpressionTree := ParseCommonBlockExpression(CommonBlock);588 end;589 end;590 end;591 end;592 593 procedure TPascalParser.ParseCommonBlockProgramCode(CommonBlock: TCommonBlock);594 begin595 with CommonBlock do begin596 Expect('begin');597 while NextCode <> 'end' do begin598 ParseCommonBlockOperation(CommonBlock);599 Expect(';');600 end;601 Expect('end');602 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin603 Instruction := inReturn;604 end;605 CheckReferences;606 end;607 end;608 609 procedure TPascalParser.ParseTypeList(TypeList: TTypeList);610 begin611 with TypeList do begin612 Expect('type');613 while IsIdentificator(NextCode) do614 with TType(Items[Add(TType.Create)]) do begin615 Parent := TypeList;616 ParseType(TType(Items[Count - 1]));617 end;618 end;619 end;620 621 procedure TPascalParser.ParseVariableList(VariableList: TVariableList);622 var623 Identifiers: TStringList;624 NewValueType: TType;625 TypeName: string;626 VariableName: string;627 Variable: TVariable;628 I: Integer;629 begin630 Identifiers := TStringList.Create;631 with VariableList do begin632 Expect('var');633 while IsIdentificator(NextCode) do begin634 VariableName := ReadCode;635 Variable := Search(VariableName);636 if not Assigned(Variable) then begin637 Identifiers.Add(VariableName);638 while NextCode = ',' do begin639 Expect(',');640 Identifiers.Add(ReadCode);641 end;642 end else ErrorMessage('Pøedefinování existující promìnné.');643 Expect(':');644 TypeName := ReadCode;645 NewValueType := Parent.Types.Search(TypeName);646 if NewValueType = nil then ErrorMessage('Typ ' + TypeName + ' nebyl definován.')647 else for I := 0 to Identifiers.Count - 1 do648 with TVariable(Items[Add(TVariable.Create)]) do begin649 Name := Identifiers[I];650 ValueType := NewValueType;651 end;652 Expect(';');653 end;654 end;655 Identifiers.Destroy;656 end;657 658 procedure TPascalParser.ParseVariable(Variable: TVariable);659 begin660 end;661 662 procedure TPascalParser.ParseType(AType: TType);663 begin664 with AType do begin665 Name := NextCode;666 Expect('=');667 UsedType := Parent.Search(NextCode);668 end;669 end;670 671 799 end. -
branches/DelphiToC/UPascalSource.pas
r19 r20 43 43 44 44 TCommand = class 45 45 Parent: TObject; 46 47 end; 48 49 TAssignment = class(TCommand) 50 Target: TVariable; 51 Source: TExpression; 52 constructor Create; 53 destructor Destroy; override; 54 end; 55 56 TMethodCall = class(TCommand) 57 Method: TMethod; 46 58 end; 47 59 48 60 TBeginEnd = class(TCommand) 49 61 Commands: TCommandList; 62 procedure Clear; 63 constructor Create; 64 destructor Destroy; override; 50 65 end; 51 66 … … 53 68 Condition: TExpression; 54 69 Command: TCommand; 55 end; 56 57 WithDo = class(TCommand) 70 constructor Create; 71 destructor Destroy; override; 72 end; 73 74 TWithDo = class(TCommand) 58 75 Context: TContext; 59 76 Command: TCommand; 60 77 end; 61 78 62 RepeatUntil = class(TCommand)79 TRepeatUntil = class(TCommand) 63 80 Block: TCommandList; 64 81 Condition: TExpression; 65 82 end; 66 83 67 ForToDo = class(TCommand)84 TForToDo = class(TCommand) 68 85 ControlVariable: TVariable; 69 86 Start: TExpression; … … 72 89 end; 73 90 74 IfThenElse = class(TCommand)91 TIfThenElse = class(TCommand) 75 92 Condition: TExpression; 76 93 Command: TCommand; … … 83 100 end; 84 101 85 CaseOfEnd = class(TCommand)102 TCaseOfEnd = class(TCommand) 86 103 Expression: TExpression; 87 104 Branches: TList; // TList<TCaseOfEndBranche> … … 89 106 end; 90 107 91 T ryFinally = class(TCommand)108 TTryFinally = class(TCommand) 92 109 Block: TCommandList; 93 110 FinallyBlock: TCommandList; 94 111 end; 95 112 96 T ryExcept = class(TCommand)113 TTryExcept = class(TCommand) 97 114 Block: TCommandList; 98 115 ExceptBlock: TCommandList; 99 116 end; 100 101 102 117 103 118 TCommandList = class(TList) … … 112 127 Variables: TVariableList; 113 128 Methods: TFunctionList; 114 Operations: TOperationList;129 Code: TBeginEnd; 115 130 constructor Create; virtual; 116 131 destructor Destroy; override; 117 procedure CheckReferences;132 // procedure CheckReferences; 118 133 end; 119 134 … … 166 181 Parent: TCommonBlock; 167 182 function Search(Name: string): TVariable; 183 destructor Destroy; override; 184 end; 185 186 TParameter = class 187 Name: string; 188 ValueType: TType; 189 DafaultValue: TValue; 190 end; 191 192 TParameterList = class(TList) 193 Parent: TFunction; 194 function Search(Name: string): TParameter; 168 195 destructor Destroy; override; 169 196 end; … … 200 227 TFunction = class(TCommonBlock) 201 228 public 202 Parameters: TList; // TList<T Parameter>229 Parameters: TList; // TList<TVariable> 203 230 ResultType: TType; 204 231 constructor Create; override; … … 246 273 begin 247 274 inherited; 248 Parameters := TList.Create; 249 ResultType := TType.Create; 275 Parameters := TParameterList.Create; 276 TParameterList(Parameters).Parent := Self; 277 //ResultType := TType.Create; 250 278 end; 251 279 … … 253 281 begin 254 282 Parameters.Free; 255 ResultType.Free;283 // ResultType.Free; 256 284 inherited; 257 285 end; … … 310 338 Constants.Clear; 311 339 Methods.Clear; 312 Operations.Clear;340 Code.Clear; 313 341 end; 314 342 … … 325 353 end; 326 354 355 (* 327 356 procedure TCommonBlock.CheckReferences; 328 357 var … … 335 364 end; 336 365 end; 366 *) 337 367 338 368 constructor TCommonBlock.Create; … … 346 376 Methods := TFunctionList.Create; 347 377 Methods.Parent := Self; 348 Operations := TOperationList.Create;378 Code := TBeginEnd.Create; 349 379 end; 350 380 … … 355 385 Variables.Destroy; 356 386 Methods.Destroy; 357 Operations.Destroy;387 Code.Destroy; 358 388 inherited; 359 389 end; … … 483 513 end; 484 514 515 { TParameterList } 516 517 destructor TParameterList.Destroy; 518 var 519 I: Integer; 520 begin 521 for I := 0 to Count - 1 do 522 TParameter(Items[I]).Free; 523 inherited; 524 end; 525 526 function TParameterList.Search(Name: string): TParameter; 527 var 528 I: Integer; 529 begin 530 I := 0; 531 while (I < Count) and (TParameter(Items[I]).Name <> Name) do Inc(I); 532 if I < Count then Result := Items[I] else Result := nil; 533 end; 534 535 { TBeginEnd } 536 537 procedure TBeginEnd.Clear; 538 begin 539 540 end; 541 542 constructor TBeginEnd.Create; 543 begin 544 Commands := TCommandList.Create; 545 end; 546 547 destructor TBeginEnd.Destroy; 548 begin 549 Commands.Free; 550 inherited; 551 end; 552 553 { TAssignment } 554 555 constructor TAssignment.Create; 556 begin 557 // Source := TExpression.Create; 558 end; 559 560 destructor TAssignment.Destroy; 561 begin 562 Source.Free; 563 inherited; 564 end; 565 566 { TWhileDo } 567 568 constructor TWhileDo.Create; 569 begin 570 Condition := TExpression.Create; 571 Command := TCommand.Create; 572 end; 573 574 destructor TWhileDo.Destroy; 575 begin 576 Condition.Free; 577 Command.Free; 578 inherited; 579 end; 580 485 581 end. 486 582 -
branches/Pascal Compiler 2/Example.pas
r16 r20 15 15 var 16 16 StringList: TStringList; 17 Text : string;17 Texts: string; 18 18 begin 19 19 20 with TStringList.Create do try 20 21 Assign(RichEdit1.Lines); 21 22 SaveToFile('Example.pas'); 22 23 23 // Comment 24 24 // dasdComment 25 25 26 26 //Here we are 27 28 27 ddd 29 28 {dfsf} … … 59 58 60 59 end. 60
Note:
See TracChangeset
for help on using the changeset viewer.