parser.c (22484B)
1 #include "arena.h" 2 #include "compiler.h" 3 #include "parser.h" 4 #include "tokenizer.h" 5 6 #include <string.h> 7 8 Location MakeLocation(const char* program, Tokens* token) 9 { 10 auto location = (Location) {1, 1}; 11 12 for (auto i = 0; program[i] != '\0'; i += 1) { 13 if (token != nullptr && token->this.lex.ptr == &program[i]) 14 break; 15 if (program[i] == '\n') { 16 location.line += 1; 17 location.column = 1; 18 } else { 19 location.column += 1; 20 } 21 } 22 23 return location; 24 } 25 26 Tokens* SkipComments(Tokens* tokens) 27 { 28 if (tokens->this.kind == TokenEOF) 29 return tokens; 30 if (tokens->this.kind == TokenComment) 31 return SkipComments(tokens->next); 32 tokens->next = SkipComments(tokens->next); 33 return tokens; 34 } 35 36 Tokens* SkipIODeclaration(Tokens* tokens, const char* program, Error* error) 37 { 38 if (tokens->this.kind != TokenOpenParen) 39 return tokens; 40 tokens = tokens->next; 41 if (tokens->this.kind != TokenIdent) { 42 *error = (Error) { 43 .location = MakeLocation(program, tokens), 44 .message = STR("expected IO declaration, got"), 45 .dynamic = tokens->this.lex, 46 }; 47 return nullptr; 48 } 49 tokens = tokens->next; 50 if (tokens->this.kind != TokenComma) { 51 *error = (Error) { 52 .location = MakeLocation(program, tokens), 53 .message = STR("expected comma, got"), 54 .dynamic = tokens->this.lex, 55 }; 56 return nullptr; 57 } 58 tokens = tokens->next; 59 if (tokens->this.kind != TokenIdent) { 60 *error = (Error) { 61 .location = MakeLocation(program, tokens), 62 .message = STR("expected IO declaration, got"), 63 .dynamic = tokens->this.lex, 64 }; 65 return nullptr; 66 } 67 tokens = tokens->next; 68 if (tokens->this.kind != TokenCloseParen) { 69 *error = (Error) { 70 .location = MakeLocation(program, tokens), 71 .message = STR("expected closing parenthesis, got"), 72 .dynamic = tokens->this.lex, 73 }; 74 return nullptr; 75 } 76 77 return tokens->next; 78 } 79 80 Names* ParseNameList( 81 Arena* arena, 82 const char* program, 83 Tokens* tokens, 84 Error* error, 85 Tokens** tokensAfter 86 ) 87 { 88 if (tokens->this.kind != TokenIdent) { 89 *error = (Error) { 90 .location = MakeLocation(program, tokens), 91 .message = STR("expected identifier, got"), 92 .dynamic = tokens->this.lex, 93 }; 94 return nullptr; 95 } 96 97 auto name = (Names*) Calloc(arena, sizeof(Names)); 98 name->this = tokens->this.lex; 99 if (tokens->next == nullptr || tokens->next->this.kind != TokenComma) { 100 *tokensAfter = tokens->next; 101 name->next = nullptr; 102 return name; 103 } 104 name->next = 105 ParseNameList(arena, program, tokens->next->next, error, tokensAfter); 106 return name; 107 } 108 109 Type ParseType( 110 Arena* arena, 111 const char* program, 112 Tokens* tokens, 113 Error* error, 114 Tokens** tokensAfter 115 ); 116 117 Array ParseArraySpecifier( 118 Arena* arena, 119 const char* program, 120 Tokens* tokens, 121 Error* error, 122 Tokens** tokensAfter 123 ) 124 { 125 auto array = (Array) {0}; 126 if (tokens->this.kind != TokenOpenSq) { 127 *error = (Error) { 128 .location = MakeLocation(program, tokens), 129 .message = STR("expected array size specifier, got"), 130 .dynamic = tokens->this.lex, 131 }; 132 return array; 133 } 134 tokens = tokens->next; 135 if (tokens->this.kind != TokenDigit || 136 String2Long(tokens->this.lex, &array.begin)) { 137 *error = (Error) { 138 .location = MakeLocation(program, tokens), 139 .message = STR("expected number for array size specifier, got"), 140 .dynamic = tokens->this.lex, 141 }; 142 return array; 143 } 144 tokens = tokens->next; 145 if (tokens->this.kind != TokenTwoPeriods) { 146 *error = (Error) { 147 .location = MakeLocation(program, tokens), 148 .message = 149 STR("expected two periods for array size specifier, got"), 150 .dynamic = tokens->this.lex, 151 }; 152 return array; 153 } 154 tokens = tokens->next; 155 if (tokens->this.kind != TokenDigit || 156 String2Long(tokens->this.lex, &array.end)) { 157 *error = (Error) { 158 .location = MakeLocation(program, tokens), 159 .message = STR("expected number for array size specifier, got"), 160 .dynamic = tokens->this.lex, 161 }; 162 return array; 163 } 164 tokens = tokens->next; 165 if (tokens->this.kind != TokenCloseSq) { 166 *error = (Error) { 167 .location = MakeLocation(program, tokens), 168 .message = STR( 169 "expected closing square bracket for array size specifier, got" 170 ), 171 .dynamic = tokens->this.lex, 172 }; 173 return array; 174 } 175 tokens = tokens->next; 176 if (tokens->this.kind != TokenOf) { 177 *error = (Error) { 178 .location = MakeLocation(program, tokens), 179 .message = STR("expected of, got"), 180 .dynamic = tokens->this.lex, 181 }; 182 return array; 183 } 184 185 *tokensAfter = tokens->next; 186 187 array.content = (struct Type*) Calloc(arena, sizeof(Type)); 188 auto type = ParseType(arena, program, *tokensAfter, error, tokensAfter); 189 memcpy(array.content, &type, sizeof(type)); 190 191 return array; 192 } 193 194 Type ParseType( 195 Arena* arena, 196 const char* program, 197 Tokens* tokens, 198 Error* error, 199 Tokens** tokensAfter 200 ) 201 { 202 *tokensAfter = tokens->next; 203 switch (tokens->this.kind) { 204 case TokenReal: 205 return (Type) { 206 .kind = TypeReal, 207 }; 208 case TokenInteger: 209 return (Type) { 210 .kind = TypeInteger, 211 }; 212 case TokenArray: 213 return (Type) { 214 .kind = TypeArray, 215 .array = ParseArraySpecifier( 216 arena, program, tokens->next, error, tokensAfter 217 ), 218 }; 219 break; 220 default: 221 *error = (Error) { 222 .location = MakeLocation(program, tokens), 223 .message = STR("expected type, got"), 224 .dynamic = tokens->this.lex, 225 }; 226 return (Type) {0}; 227 } 228 } 229 230 Type ParseTypeSpecifier( 231 Arena* arena, 232 const char* program, 233 Tokens* tokens, 234 Error* error, 235 Tokens** tokensAfter 236 ) 237 { 238 if (tokens->this.kind != TokenColon) { 239 *error = (Error) { 240 .location = MakeLocation(program, tokens), 241 .message = STR("expected colon after identifiers, got"), 242 .dynamic = tokens->this.lex, 243 }; 244 return (Type) {0}; 245 } 246 247 return ParseType(arena, program, tokens->next, error, tokensAfter); 248 } 249 250 Declaration ParseDeclaration( 251 Arena* arena, 252 const char* program, 253 Tokens* tokens, 254 Error* error, 255 Tokens** tokensAfter 256 ) 257 { 258 Declaration this; 259 this.names = ParseNameList(arena, program, tokens, error, tokensAfter); 260 if (!ErrorOk(*error)) 261 return this; 262 this.type = 263 ParseTypeSpecifier(arena, program, *tokensAfter, error, tokensAfter); 264 return this; 265 } 266 267 Declarations* ParseVariableDeclarations( 268 Arena* arena, 269 const char* program, 270 Tokens* tokens, 271 Error* error, 272 Tokens** tokensAfter 273 ) 274 { 275 if (tokens->this.kind != TokenVar) { 276 *tokensAfter = tokens; 277 return nullptr; 278 } 279 280 auto decls = (Declarations*) Calloc(arena, sizeof(Declarations)); 281 decls->this = 282 ParseDeclaration(arena, program, tokens->next, error, tokensAfter); 283 if (!ErrorOk(*error)) 284 return nullptr; 285 if ((*tokensAfter)->this.kind != TokenSemicolon) { 286 *error = (Error) { 287 .location = MakeLocation(program, *tokensAfter), 288 .message = STR("expected semicolon, got"), 289 .dynamic = (*tokensAfter)->this.lex, 290 }; 291 return nullptr; 292 } 293 decls->next = ParseVariableDeclarations( 294 arena, program, (*tokensAfter)->next, error, tokensAfter 295 ); 296 return decls; 297 } 298 299 Expression ParseIf( 300 Arena* arena, 301 const char* program, 302 Tokens* tokens, 303 Error* error, 304 Tokens** tokensAfter 305 ) 306 { 307 (void) arena; 308 (void) program; 309 (void) tokens; 310 (void) error; 311 (void) tokensAfter; 312 return (Expression) {0}; 313 } 314 315 Expression ParseWhile( 316 Arena* arena, 317 const char* program, 318 Tokens* tokens, 319 Error* error, 320 Tokens** tokensAfter 321 ) 322 { 323 (void) arena; 324 (void) program; 325 (void) tokens; 326 (void) error; 327 (void) tokensAfter; 328 return (Expression) {0}; 329 } 330 331 Expression ParseCompound( 332 Arena* arena, 333 const char* program, 334 Tokens* tokens, 335 Error* error, 336 Tokens** tokensAfter 337 ) 338 { 339 (void) arena; 340 (void) program; 341 (void) tokens; 342 (void) error; 343 (void) tokensAfter; 344 return (Expression) {0}; 345 } 346 347 Expression ParseIdentOrCallOrIndex( 348 Arena* arena, 349 const char* program, 350 Tokens* tokens, 351 Error* error, 352 Tokens** tokensAfter 353 ) 354 { 355 (void) arena; 356 (void) program; 357 (void) tokens; 358 (void) error; 359 (void) tokensAfter; 360 return (Expression) {0}; 361 } 362 363 Expression ParsePrimary( 364 Arena* arena, 365 const char* program, 366 Tokens* tokens, 367 Error* error, 368 Tokens** tokensAfter 369 ) 370 { 371 Expression expression; 372 373 switch (tokens->this.kind) { 374 case TokenDigit: 375 expression.kind = ExpressionLiteral; 376 expression.literal.type = TypeInteger; 377 expression.literal.content = tokens->this.lex; 378 return expression; 379 case TokenIdent: 380 return ParseIdentOrCallOrIndex( 381 arena, program, tokens, error, tokensAfter 382 ); 383 case TokenPlus: 384 expression.kind = ExpressionUnaryOperation; 385 expression.unary = (UnaryOperation) { 386 .operator= UnaryPlus, 387 .operand = (Expression*) Calloc(arena, sizeof(Expression)), 388 }; 389 *expression.unary.operand = 390 ParsePrimary(arena, program, tokens->next, error, tokensAfter); 391 return expression; 392 case TokenMinus: 393 expression.kind = ExpressionUnaryOperation; 394 expression.unary = (UnaryOperation) { 395 .operator= UnaryMinus, 396 .operand = (Expression*) Calloc(arena, sizeof(Expression)), 397 }; 398 *expression.unary.operand = 399 ParsePrimary(arena, program, tokens->next, error, tokensAfter); 400 return expression; 401 case TokenNot: 402 expression.kind = ExpressionUnaryOperation; 403 expression.unary = (UnaryOperation) { 404 .operator= UnaryNot, 405 .operand = (Expression*) Calloc(arena, sizeof(Expression)), 406 }; 407 *expression.unary.operand = 408 ParsePrimary(arena, program, tokens->next, error, tokensAfter); 409 return expression; 410 case TokenOpenParen: 411 expression = 412 ParseExpression(arena, program, tokens->next, error, tokensAfter); 413 if ((*tokensAfter)->this.kind != TokenCloseParen) { 414 *error = (Error) { 415 .message = STR("expected ), got "), 416 .dynamic = (*tokensAfter)->this.lex, 417 }; 418 } 419 *tokensAfter = (*tokensAfter)->next; 420 return expression; 421 default: 422 break; 423 } 424 } 425 426 int Token2BinaryOperator(TokenKind token, BinaryOperator* ret) 427 { 428 switch (token) { 429 case TokenPlus: 430 *ret = BinaryPlus; 431 return 0; 432 case TokenMinus: 433 *ret = BinaryMinus; 434 return 0; 435 case TokenMul: 436 *ret = BinaryMul; 437 return 0; 438 case TokenDiv: 439 *ret = BinaryDiv; 440 return 0; 441 case TokenMod: 442 *ret = BinaryMod; 443 return 0; 444 case TokenAnd: 445 *ret = BinaryAnd; 446 return 0; 447 case TokenOr: 448 *ret = BinaryOr; 449 return 0; 450 case TokenEq: 451 *ret = BinaryEq; 452 return 0; 453 case TokenGe: 454 *ret = BinaryGe; 455 return 0; 456 case TokenGt: 457 *ret = BinaryGt; 458 return 0; 459 case TokenLe: 460 *ret = BinaryLe; 461 return 0; 462 case TokenLt: 463 *ret = BinaryLt; 464 return 0; 465 case TokenAssign: 466 *ret = BinaryAssign; 467 return 0; 468 469 default: 470 return 1; 471 } 472 } 473 474 int Precedence(BinaryOperator operator) 475 { 476 switch (operator) { 477 case BinaryAssign: 478 return 0; 479 case BinaryEq: 480 case BinaryGe: 481 case BinaryGt: 482 case BinaryLe: 483 case BinaryLt: 484 return 1; 485 case BinaryPlus: 486 case BinaryMinus: 487 case BinaryOr: 488 return 2; 489 case BinaryMul: 490 case BinaryDiv: 491 case BinaryMod: 492 case BinaryAnd: 493 return 3; 494 default: 495 return -1; 496 } 497 } 498 499 Expression ParseBinary( 500 int minPrec, 501 Arena* arena, 502 const char* program, 503 Tokens* tokens, 504 Error* error, 505 Tokens** tokensAfter 506 ) 507 { 508 auto left = ParsePrimary(arena, program, tokens, error, tokensAfter); 509 510 // TODO. 511 } 512 513 Expression ParseExpression( 514 Arena* arena, 515 const char* program, 516 Tokens* tokens, 517 Error* error, 518 Tokens** tokensAfter 519 ) 520 { 521 switch (tokens->this.kind) { 522 case TokenIf: 523 return ParseIf(arena, program, tokens, error, tokensAfter); 524 case TokenWhile: 525 return ParseWhile(arena, program, tokens, error, tokensAfter); 526 case TokenBegin: 527 return ParseCompound(arena, program, tokens, error, tokensAfter); 528 default: 529 return ParseBinary(0, arena, program, tokens, error, tokensAfter); 530 } 531 } 532 533 Parameters* ParseParameterDeclarations( 534 Arena* arena, 535 const char* program, 536 Tokens* tokens, 537 Error* error, 538 Tokens** tokensAfter 539 ) 540 { 541 if (tokens->this.kind != TokenIdent && tokens->this.kind != TokenVar) { 542 *tokensAfter = tokens; 543 return nullptr; 544 } 545 546 auto parameters = (Parameters*) Calloc(arena, sizeof(Parameters)); 547 parameters->next = nullptr; 548 parameters->this.reference = tokens->this.kind == TokenVar; 549 550 if (tokens->this.kind == TokenVar) 551 tokens = tokens->next; 552 553 parameters->this.this = 554 ParseDeclaration(arena, program, tokens, error, tokensAfter); 555 if (!ErrorOk(*error)) 556 return parameters; 557 if ((*tokensAfter)->this.kind == TokenSemicolon) 558 parameters->next = ParseParameterDeclarations( 559 arena, program, (*tokensAfter)->next, error, tokensAfter 560 ); 561 return parameters; 562 } 563 564 Parameters* ParseParameterList( 565 Arena* arena, 566 const char* program, 567 Tokens* tokens, 568 Error* error, 569 Tokens** tokensAfter 570 ) 571 { 572 if (tokens->this.kind != TokenOpenParen) { 573 *tokensAfter = tokens; 574 return nullptr; 575 } 576 577 auto parameters = ParseParameterDeclarations( 578 arena, program, tokens->next, error, tokensAfter 579 ); 580 581 if ((*tokensAfter)->this.kind != TokenCloseParen) { 582 *error = (Error) { 583 .location = MakeLocation(program, *tokensAfter), 584 .message = STR("expected ), got"), 585 .dynamic = (*tokensAfter)->this.lex, 586 }; 587 return parameters; 588 } 589 *tokensAfter = (*tokensAfter)->next; 590 591 return parameters; 592 } 593 594 Subprogram ParseFunction( 595 Arena* arena, 596 const char* program, 597 Tokens* tokens, 598 Error* error, 599 Tokens** tokensAfter 600 ) 601 { 602 auto prg = (Subprogram) {0}; 603 if (tokens->this.kind != TokenIdent) { 604 *error = (Error) { 605 .location = MakeLocation(program, tokens), 606 .message = STR("expected function name, got"), 607 .dynamic = tokens->this.lex, 608 }; 609 return prg; 610 } 611 612 prg.name = tokens->this.lex; 613 prg.parameters = 614 ParseParameterList(arena, program, tokens->next, error, tokensAfter); 615 if (!ErrorOk(*error)) 616 return prg; 617 618 prg.returnType = 619 ParseTypeSpecifier(arena, program, *tokensAfter, error, tokensAfter); 620 if (!ErrorOk(*error)) 621 return prg; 622 623 if ((*tokensAfter)->this.kind != TokenSemicolon) { 624 *error = (Error) { 625 .location = MakeLocation(program, *tokensAfter), 626 .message = STR("expected semicolon, got"), 627 .dynamic = (*tokensAfter)->this.lex, 628 }; 629 return prg; 630 } 631 prg.locals = ParseVariableDeclarations( 632 arena, program, (*tokensAfter)->next, error, tokensAfter 633 ); 634 if (!ErrorOk(*error)) 635 return prg; 636 prg.body = 637 ParseExpression(arena, program, *tokensAfter, error, tokensAfter); 638 if (!ErrorOk(*error)) 639 return prg; 640 if ((*tokensAfter)->this.kind != TokenSemicolon) { 641 *error = (Error) { 642 .location = MakeLocation(program, *tokensAfter), 643 .message = STR("expected semicolon, got"), 644 .dynamic = (*tokensAfter)->this.lex, 645 }; 646 } 647 *tokensAfter = (*tokensAfter)->next; 648 return prg; 649 } 650 651 Subprogram ParseProcedure( 652 Arena* arena, 653 const char* program, 654 Tokens* tokens, 655 Error* error, 656 Tokens** tokensAfter 657 ) 658 { 659 auto prg = (Subprogram) { 660 .returnType = (Type) {.kind = TypeVoid}, 661 }; 662 if (tokens->this.kind != TokenIdent) { 663 *error = (Error) { 664 .location = MakeLocation(program, tokens), 665 .message = STR("expected procedure name, got"), 666 .dynamic = tokens->this.lex, 667 }; 668 return prg; 669 } 670 671 prg.name = tokens->this.lex; 672 prg.parameters = 673 ParseParameterList(arena, program, tokens->next, error, tokensAfter); 674 if (!ErrorOk(*error)) 675 return prg; 676 677 if ((*tokensAfter)->this.kind != TokenSemicolon) { 678 *error = (Error) { 679 .location = MakeLocation(program, *tokensAfter), 680 .message = STR("expected semicolon, got"), 681 .dynamic = (*tokensAfter)->this.lex, 682 }; 683 return prg; 684 } 685 prg.locals = ParseVariableDeclarations( 686 arena, program, (*tokensAfter)->next, error, tokensAfter 687 ); 688 if (!ErrorOk(*error)) 689 return prg; 690 prg.body = 691 ParseExpression(arena, program, *tokensAfter, error, tokensAfter); 692 if (!ErrorOk(*error)) 693 return prg; 694 if ((*tokensAfter)->this.kind != TokenSemicolon) { 695 *error = (Error) { 696 .location = MakeLocation(program, *tokensAfter), 697 .message = STR("expected semicolon, got"), 698 .dynamic = (*tokensAfter)->this.lex, 699 }; 700 } 701 *tokensAfter = (*tokensAfter)->next; 702 return prg; 703 } 704 705 Subprogram ParseSubprogram( 706 Arena* arena, 707 const char* program, 708 Tokens* tokens, 709 Error* error, 710 Tokens** tokensAfter 711 ) 712 { 713 if (tokens->this.kind == TokenFunction) 714 return ParseFunction(arena, program, tokens->next, error, tokensAfter); 715 return ParseProcedure(arena, program, tokens->next, error, tokensAfter); 716 } 717 718 Subprograms* ParseSubprograms( 719 Arena* arena, 720 const char* program, 721 Tokens* tokens, 722 Error* error, 723 Tokens** tokensAfter 724 ) 725 { 726 if (tokens->this.kind != TokenProcedure && 727 tokens->this.kind != TokenFunction) { 728 *tokensAfter = tokens; 729 return nullptr; 730 } 731 732 auto prgs = (Subprograms*) Calloc(arena, sizeof(Subprograms)); 733 prgs->this = (Subprogram*) Calloc(arena, sizeof(Subprogram)); 734 *prgs->this = ParseSubprogram(arena, program, tokens, error, tokensAfter); 735 if (!ErrorOk(*error)) 736 return nullptr; 737 prgs->next = 738 ParseSubprograms(arena, program, *tokensAfter, error, tokensAfter); 739 return prgs; 740 } 741 742 void ParseProgramBody( 743 Arena* arena, 744 Ast* ast, 745 const char* program, 746 Tokens* tokens, 747 Error* error 748 ) 749 { 750 Tokens* tokensAfterDecl = nullptr; 751 ast->declarations = ParseVariableDeclarations( 752 arena, program, tokens, error, &tokensAfterDecl 753 ); 754 if (!ErrorOk(*error)) 755 return; 756 757 Tokens* tokensAfterSubprgs = nullptr; 758 ast->subprograms = ParseSubprograms( 759 arena, program, tokensAfterDecl, error, &tokensAfterSubprgs 760 ); 761 if (!ErrorOk(*error)) 762 return; 763 764 Tokens* tokensAfterMainPrg = nullptr; 765 ast->main = ParseExpression( 766 arena, program, tokensAfterSubprgs, error, &tokensAfterMainPrg 767 ); 768 if (!ErrorOk(*error)) 769 return; 770 771 if (tokensAfterMainPrg->this.kind != TokenPeriod) { 772 *error = (Error) { 773 .location = MakeLocation(program, nullptr), 774 .message = STR("expected . after main program declaration"), 775 .dynamic = STR(""), 776 }; 777 return; 778 } 779 if (tokensAfterMainPrg->next->this.kind != TokenEOF) { 780 *error = (Error) { 781 .location = MakeLocation(program, tokensAfterMainPrg->next), 782 .message = STR("expected nothing, got"), 783 .dynamic = tokensAfterMainPrg->next->this.lex, 784 }; 785 return; 786 } 787 } 788 789 Ast* ParseProgram( 790 Arena* arena, 791 const char* program, 792 Tokens* tokens, 793 Error* error 794 ) 795 { 796 if (tokens->this.kind != TokenProgram) { 797 *error = (Error) { 798 .location = MakeLocation(program, tokens), 799 .message = STR("expected program declaration, got"), 800 .dynamic = tokens->this.lex, 801 }; 802 return nullptr; 803 } 804 tokens = tokens->next; 805 if (tokens->this.kind != TokenIdent) { 806 *error = (Error) { 807 .location = MakeLocation(program, tokens), 808 .message = STR("expected program name, got"), 809 .dynamic = tokens->this.lex, 810 }; 811 return nullptr; 812 } 813 814 auto ast = (Ast*) Calloc(arena, sizeof(Ast)); 815 ast->name = tokens->this.lex; 816 ast->declarations = nullptr; 817 ast->subprograms = nullptr; 818 819 tokens = SkipIODeclaration(tokens->next, program, error); 820 if (!ErrorOk(*error)) 821 return nullptr; 822 if (tokens->this.kind != TokenSemicolon) { 823 *error = (Error) { 824 .location = MakeLocation(program, tokens), 825 .message = STR("expected semicolon, got"), 826 .dynamic = tokens->this.lex, 827 }; 828 return nullptr; 829 } 830 831 ParseProgramBody(arena, ast, program, tokens->next, error); 832 return ast; 833 } 834 835 Ast* Parse(Arena* arena, const char* program, Tokens* tokens, Error* error) 836 { 837 tokens = SkipComments(tokens); 838 839 return ParseProgram(arena, program, tokens, error); 840 }