parser.c (20781B)
1 #include "lexer.h" 2 #include "parser.h" 3 4 Operator_entry_list preops = {0}; 5 Operator_entry_list inops = {0}; 6 Operator_entry_list postops = {0}; 7 8 Ast_node * 9 new_ast_node(Parser *p, Ast_kind kind, Token loc) 10 { 11 Ast_node *result = p->ast_alloc(); 12 result->kind = kind; 13 result->start_tok = loc; 14 return result; 15 } 16 17 Operator 18 collect_operator(Parser *p, Operator_entry_list *ops) 19 { 20 int i; 21 int latest_match = -1; 22 int exact; 23 Lexer saved_lexer = p->lexer; 24 skip_whitespace(&p->lexer); 25 Token tok; 26 Token start_tok = peek_token(&p->lexer); 27 Sb sb = {0}; 28 ssize_t old_len = 0; 29 bool match; 30 do { 31 exact = -1; 32 match = false; 33 tok = next_token(&p->lexer); 34 old_len = sb.count; 35 sb_append_sv(&sb, tok.sv); 36 for (i = 0; i < ops->count; i++) { 37 if (sv_starts_with(ops->items[i].sv, sv_from_sb(sb))) { 38 if (exact < 0 && sv_equal(ops->items[i].sv, sv_from_sb(sb))) exact = i; 39 match = true; 40 } 41 } 42 if (exact >= 0) { 43 latest_match = exact; 44 saved_lexer = p->lexer; 45 } 46 } while (match); 47 p->lexer = saved_lexer; 48 if (latest_match < 0) { 49 return (Operator) { .entry = -1 }; 50 } 51 start_tok.sv.count = old_len; 52 return (Operator) { .entry = latest_match, .tok = start_tok}; 53 } 54 55 Ast_node * 56 parse_postfix(Parser *p, Ast_node *child) 57 { 58 (void) p; 59 return child; 60 } 61 62 Ast_node * 63 parse_primitive(Parser *p) 64 { 65 Ast_node *result; 66 Token t = next_token(&p->lexer); 67 if (t.kind == T_NUMBER) { 68 result = new_ast_node(p, AST_NUMBER, t); 69 } else if (t.kind == T_IDENT) { 70 result = new_ast_node(p, AST_IDENT, t); 71 } else if (t.kind == '(') { 72 result = parse_expression(p); 73 if (result->kind == AST_ERROR || !expect_token_kind(&p->lexer, ')')) { 74 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 75 } 76 } else { 77 Sb sb = {0}; 78 sb_append_loc(&sb, t.loc); 79 sb_append_cstr(&sb, "Error: invalid primitive "); 80 sb_append_token_kind(&sb, t.kind); 81 sb_append_char(&sb, '\n'); 82 sb_write(2, sb); 83 sb_release(&sb); 84 p->error = true; 85 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 86 } 87 if (result->kind == AST_ERROR) { 88 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 89 } 90 return parse_postfix(p, result); 91 } 92 93 Ast_node * 94 parse_prefix(Parser *p) 95 { 96 Ast_node *result; 97 Operator prefix = collect_operator(p, &preops); 98 if (prefix.entry == -1) { 99 return parse_primitive(p); 100 } 101 result = new_ast_node(p, AST_PREFIX, prefix.tok); 102 result->preop.op = prefix; 103 // TODO: Maybe we should return null if this call fails? 104 result->preop.right = parse_prefix(p); 105 return result; 106 } 107 108 Ast_node * 109 parse_binop(Parser *p, int precedence, Ast_node *left_child) 110 { 111 Lexer saved_lexer; 112 Ast_node *left; 113 Ast_node *right = NULL; 114 Ast_node *result; 115 Operator binop; 116 int next_prec; 117 if (!left_child) { 118 left = parse_prefix(p); 119 if (left->kind == AST_ERROR) { 120 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 121 } 122 } else { 123 left = left_child; 124 } 125 do { 126 saved_lexer = p->lexer; 127 Operator binop_peek = collect_operator(p, &inops); 128 if (binop_peek.entry == -1) { 129 break; 130 } 131 132 next_prec = inops.items[binop_peek.entry].prec; 133 if (next_prec > precedence) { 134 p->lexer = saved_lexer; 135 break; 136 } 137 if (next_prec < precedence) { 138 p->lexer = saved_lexer; 139 /* skip_whitespace(&p->lexer); */ 140 if (right) { 141 right = parse_binop(p, next_prec, right); 142 if (right->kind == AST_ERROR) { 143 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 144 } 145 } else { 146 left = parse_binop(p, next_prec, left); 147 if (left->kind == AST_ERROR) { 148 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 149 } 150 } 151 continue; 152 } 153 skip_whitespace(&p->lexer); 154 if (right) { 155 result = new_ast_node(p, AST_INFIX, binop.tok); 156 result->inop.op = binop; 157 result->inop.left = left; 158 result->inop.right = right; 159 left = result; 160 } 161 binop = binop_peek; 162 if (inops.items[binop.entry].right_assoc) { 163 right = parse_binop(p, next_prec, NULL); 164 if (right->kind == AST_ERROR) { 165 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 166 } 167 } else { 168 right = parse_prefix(p); 169 if (right->kind == AST_ERROR) { 170 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 171 } 172 } 173 } while (1); 174 if (right) { 175 result = new_ast_node(p, AST_INFIX, binop.tok); 176 result->inop.op = binop; 177 result->inop.left = left; 178 result->inop.right = right; 179 left = result; 180 } 181 return left; 182 } 183 184 Ast_node * 185 parse_expression(Parser *p) 186 { 187 Ast_node *result = parse_binop(p, INT_MAX, NULL); 188 return result; 189 } 190 191 Ast_node * 192 parse_type(Parser *p) 193 { 194 if (!expect_token_kind(&p->lexer, T_IDENT)) { 195 p->error = true; 196 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 197 }; 198 Token base = p->lexer.last_tok; 199 return new_ast_node(p, AST_TYPE, base); 200 } 201 202 Ast_node * 203 parse_var_declaration(Parser *p) 204 { 205 Token var_name; 206 Ast_node *var_type = NULL; 207 Ast_node *var_value = NULL; 208 Ast_node *result; 209 210 if (!expect_token_kind(&p->lexer, T_IDENT)) { 211 p->error = true; 212 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 213 } 214 var_name = p->lexer.last_tok; 215 skip_whitespace(&p->lexer); 216 expect_token_kind(&p->lexer, ':'); 217 skip_whitespace(&p->lexer); 218 if (peek_token(&p->lexer).kind != '=') { 219 var_type = parse_type(p); 220 if (var_type->kind == AST_ERROR) { 221 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 222 } 223 skip_whitespace(&p->lexer); 224 } 225 if (peek_token(&p->lexer).kind == '=') { 226 next_token(&p->lexer); 227 skip_whitespace(&p->lexer); 228 var_value = parse_expression(p); 229 if (var_value->kind == AST_ERROR) { 230 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 231 } 232 } 233 result = new_ast_node(p, AST_VAR_DECL, var_name); 234 result->var.name = var_name; 235 result->var.type = var_type; 236 result->var.value = var_value; 237 238 return result; 239 } 240 241 Ast_node * 242 parse_var_declaration_statement(Parser *p) { 243 Token start_tok; 244 if (!expect_token_kind(&p->lexer, T_VAR)) { 245 p->error = true; 246 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 247 } 248 249 start_tok = p->lexer.last_tok; 250 251 skip_whitespace(&p->lexer); 252 Ast_node *result = parse_var_declaration(p); 253 if (result->kind == AST_ERROR) { 254 return NULL; 255 } 256 257 if (!expect_end_statement(&p->lexer)) { 258 p->error = true; 259 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 260 } 261 262 result->start_tok = start_tok; 263 return result; 264 } 265 266 Ast_node * 267 parse_block(Parser *p) 268 { 269 Token end_tok; 270 if (!expect_token_kind(&p->lexer, '{')) { 271 p->error = true; 272 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 273 } 274 Token start_tok = p->lexer.last_tok; 275 276 skip_whitespace(&p->lexer); 277 Ast_node *node, *result; 278 Ast_node_list list = {0}; 279 Token peek; 280 skip_whitespace(&p->lexer); 281 while ((peek = peek_token(&p->lexer)).kind != '}') { 282 if (peek.kind == T_VAR) { 283 node = parse_var_declaration_statement(p); 284 } else if (peek.kind == ';') { 285 next_token(&p->lexer); 286 skip_whitespace(&p->lexer); 287 continue; 288 } else if (peek.kind == '{') { 289 node = parse_block(p); 290 } else if (peek.kind == T_EOF) { 291 p->error = true; 292 293 // Leaking nodes 294 free(list.items); 295 296 print_loc(start_tok.loc); 297 fprintf(stderr, "Error: unterminated block\n"); 298 return new_ast_node(p, AST_ERROR, peek); 299 } else if (peek.kind == T_ERROR_) { 300 // Leaking nodes 301 free(list.items); 302 return new_ast_node(p, AST_ERROR, peek); 303 } else { 304 node = parse_expression(p); 305 /* expect_token_kind(&p->lexer, ';'); */ 306 if (expect_end_statement(&p->lexer)) { 307 p->error = true; 308 return new_ast_node(p, AST_ERROR, peek); 309 } 310 } 311 da_append(&list, node); 312 skip_whitespace(&p->lexer); 313 } 314 315 if (!expect_token_kind(&p->lexer, '}')) { 316 p->error = true; 317 return new_ast_node(p, AST_ERROR, peek); 318 } 319 end_tok = p->lexer.last_tok; 320 321 result = new_ast_node(p, AST_BLOCK, start_tok); 322 result->block.list = list; 323 result->block.end_tok = end_tok; 324 return result; 325 } 326 327 Ast_node_list 328 parse_func_arg_declaraction_list(Parser *p) 329 { 330 Token peek; 331 Ast_node *node; 332 Ast_node_list list = {0}; 333 while ((peek = peek_token(&p->lexer)).kind != ')') { 334 if (list.count && !expect_token_kind(&p->lexer, ',')) { 335 p->error = true; 336 break; 337 } 338 node = parse_var_declaration(p); 339 if (node->kind == AST_ERROR) { 340 break; 341 } 342 da_append(&list, node); 343 skip_whitespace(&p->lexer); 344 } 345 return list; 346 } 347 348 Ast_node * 349 parse_func_declaration_statement(Parser *p) 350 { 351 Ast_node *ret_type = NULL; 352 Ast_node *result; 353 if (!expect_token_kind(&p->lexer, T_FUNC)) { 354 p->error = true; 355 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 356 } 357 Token start_tok = p->lexer.last_tok; 358 skip_whitespace(&p->lexer); 359 if (!expect_token_kind(&p->lexer, T_IDENT)) { 360 p->error = true; 361 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 362 } 363 Token name = p->lexer.last_tok; 364 365 skip_whitespace(&p->lexer); 366 if (!expect_token_kind(&p->lexer, '(')) { 367 p->error = true; 368 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 369 } 370 skip_whitespace(&p->lexer); 371 Ast_node_list arg_list = parse_func_arg_declaraction_list(p); 372 if (p->error || !expect_token_kind(&p->lexer, ')')) { 373 p->error = true; 374 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 375 } 376 377 skip_whitespace(&p->lexer); 378 if (peek_token(&p->lexer).kind == '-') { 379 if (!expect_token_kind(&p->lexer, '-')) { 380 p->error = true; 381 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 382 } 383 if (!expect_token_kind(&p->lexer, '>')) { 384 p->error = true; 385 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 386 } 387 skip_whitespace(&p->lexer); 388 ret_type = parse_type(p); 389 if (ret_type->kind == AST_ERROR) { 390 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 391 } 392 skip_whitespace(&p->lexer); 393 } 394 395 Ast_node *body = parse_block(p); 396 if (body->kind == AST_ERROR) { 397 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 398 } 399 400 result = new_ast_node(p, AST_FUNC_DECL, start_tok); 401 result->func.body = body; 402 result->func.ret_type = ret_type; 403 result->func.name = name; 404 result->func.arg_list = arg_list; 405 return result; 406 } 407 408 Ast_node * 409 parse_operator_declaration(Parser *p) 410 { 411 int precedence = -1; 412 int op_kind = -1; 413 int associativity = -1; 414 Token tok; 415 Token error_tok; 416 if (!expect_token_kind(&p->lexer, T_OPERATOR)) { 417 p->error = true; 418 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 419 } 420 Token start_tok = p->lexer.last_tok; 421 skip_whitespace(&p->lexer); 422 if (!expect_token_kind(&p->lexer, '<')) { 423 p->error = true; 424 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 425 } 426 error_tok = peek_token(&p->lexer); 427 skip_whitespace(&p->lexer); 428 while ((tok = next_token(&p->lexer)).kind != '>') { 429 if (tok.kind == T_IDENT) { 430 if (sv_equal(tok.sv, sv_literal("precedence"))) { 431 skip_whitespace(&p->lexer); 432 if (!expect_token_kind(&p->lexer, '=')) { 433 p->error = true; 434 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 435 } 436 skip_whitespace(&p->lexer); 437 if (!expect_token_kind(&p->lexer, T_NUMBER)) { 438 p->error = true; 439 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 440 } 441 tok = p->lexer.last_tok; 442 precedence = strtol(tok.sv.items, NULL, 10); 443 } else if (sv_equal(tok.sv, sv_literal("prefix"))) { 444 if (op_kind >= 0) { 445 todo("error handling"); 446 } 447 op_kind = 0; 448 } else if (sv_equal(tok.sv, sv_literal("infix"))) { 449 if (op_kind >= 0) { 450 todo("error handling"); 451 } 452 op_kind = 1; 453 } else if (sv_equal(tok.sv, sv_literal("postfix"))) { 454 if (op_kind >= 0) { 455 todo("error handling"); 456 } 457 op_kind = 2; 458 } else if (sv_equal(tok.sv, sv_literal("left"))) { 459 associativity = 0; 460 } else if (sv_equal(tok.sv, sv_literal("right"))) { 461 associativity = 1; 462 } 463 error_tok = peek_token(&p->lexer); 464 skip_whitespace(&p->lexer); 465 if (peek_token(&p->lexer).kind == ',') { 466 next_token(&p->lexer); 467 error_tok = peek_token(&p->lexer); 468 skip_whitespace(&p->lexer); 469 } 470 continue; 471 } 472 print_loc(error_tok.loc); 473 fprintf(stderr, "expected valid operator modifier, or '>', but got "); 474 debug_print_token(tok); 475 fprintf(stderr, "\n"); 476 p->error = true; 477 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 478 } 479 if (precedence < 0 && op_kind == 1) { 480 print_loc(start_tok.loc); 481 fprintf(stderr, "infix operator must have a precedence\n"); 482 p->error = true; 483 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 484 } 485 if (precedence >= 0 && op_kind != 1) { 486 print_loc(start_tok.loc); 487 fprintf(stderr, "non-infix operator must not have a precedence\n"); 488 p->error = true; 489 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 490 } 491 if (op_kind < 0) { 492 print_loc(start_tok.loc); 493 fprintf(stderr, "operator must have a op_kind (prefix, infix, or postfix)\n"); 494 p->error = true; 495 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 496 } 497 if (associativity < 0 && op_kind == 1) { 498 print_loc(start_tok.loc); 499 fprintf(stderr, "infix operator must have an associativity (left or right)\n"); 500 p->error = true; 501 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 502 } 503 if (associativity >= 0 && op_kind != 1) { 504 print_loc(start_tok.loc); 505 fprintf(stderr, "non-infix operator must not have a designated associativity\n"); 506 p->error = true; 507 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 508 } 509 skip_whitespace(&p->lexer); 510 Token op_start = peek_token(&p->lexer); 511 if (!is_operator_token(op_start.kind)) { 512 print_loc(op_start.loc); 513 fprintf(stderr, "unexpected token in operator name "); 514 debug_print_token(op_start); 515 fprintf(stderr, "\n"); 516 p->error = true; 517 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 518 } 519 Token op_tmp = {0}; 520 do { 521 next_token(&p->lexer); 522 op_start.sv.count += op_tmp.sv.count; 523 op_tmp = peek_token(&p->lexer); 524 } while (is_operator_token(op_tmp.kind)); 525 if (op_tmp.kind != T_WHITESPACE && op_tmp.kind != '(') { 526 print_loc(op_tmp.loc); 527 fprintf(stderr, "unexpected token in operator name "); 528 debug_print_token(op_tmp); 529 fprintf(stderr, "\n"); 530 p->error = true; 531 return new_ast_node(p, AST_ERROR, p->lexer.last_tok); 532 } 533 skip_whitespace(&p->lexer); 534 Ast_node *body = parse_block(p); 535 Ast_node *result = new_ast_node(p, AST_OPERATOR_DECL, start_tok); 536 if (op_kind == OP_PREFIX) { 537 result->operator.entry = preops.count; 538 da_append(&preops, ((Operator_entry) { .sv = op_start.sv })); 539 } else if (op_kind == OP_INFIX) { 540 result->operator.entry = inops.count; 541 da_append(&inops, ((Operator_entry) { .sv = op_start.sv, .prec = precedence, .right_assoc = associativity })); 542 } else if (op_kind == OP_POSTFIX) { 543 result->operator.entry = postops.count; 544 da_append(&postops, ((Operator_entry) { .sv = op_start.sv })); 545 } else { 546 unreachable("invalid operator op_kind"); 547 } 548 result->operator.kind = op_kind; 549 result->operator.body = body; 550 return result; 551 } 552 553 Ast_node_list 554 parse(Parser *p) 555 { 556 Ast_node *node; 557 Ast_node_list list = {0}; 558 Token peek; 559 skip_whitespace(&p->lexer); 560 while ((peek = peek_token(&p->lexer)).kind != T_EOF) { 561 if (peek.kind == T_VAR) { 562 node = parse_var_declaration_statement(p); 563 } else if (peek.kind == T_OPERATOR) { 564 node = parse_operator_declaration(p); 565 } else if (peek.kind == T_FUNC) { 566 node = parse_func_declaration_statement(p); 567 } else if (peek.kind == T_ERROR_) { 568 p->error = true; 569 node = new_ast_node(p, AST_ERROR, p->lexer.last_tok); 570 } else if (peek.kind == '{') { 571 node = parse_block(p); 572 } else { 573 Sb sb = {0}; 574 sb_append_loc(&sb, peek.loc); 575 sb_append_cstr(&sb, "Error: invalid top level declaration at token "); 576 sb_append_token_kind(&sb, peek.kind); 577 sb_append_char(&sb, '\n'); 578 sb_write(2, sb); 579 sb_release(&sb); 580 p->error = true; 581 node = new_ast_node(p, AST_ERROR, p->lexer.last_tok); 582 } 583 da_append(&list, node); 584 skip_whitespace(&p->lexer); 585 if (p->error) { 586 break; 587 } 588 } 589 return list; 590 } 591 592 void 593 debug_print_ast_node(Ast_node *node, int indent) 594 { 595 if (!node) { 596 fprintf(stderr, "%*s", indent * 2, ""); 597 fprintf(stderr, "(null)\n"); 598 return; 599 } 600 int min_width = node->start_tok.loc.filename.count + 11; 601 print_loc_pad(node->start_tok.loc, min_width); 602 fprintf(stderr, "%*s", indent * 2, ""); 603 604 switch (node->kind) { 605 case AST_TYPE: 606 fprintf(stderr, "Type(%.*s)\n", (int) node->start_tok.sv.count, node->start_tok.sv.items); 607 break; 608 case AST_VAR_DECL: 609 fprintf(stderr, "VarDecl(%.*s)\n", (int) node->var.name.sv.count, node->var.name.sv.items); 610 if (node->var.type) { 611 debug_print_ast_node(node->var.type, indent + 1); 612 } 613 if (node->var.value) { 614 debug_print_ast_node(node->var.value, indent + 1); 615 } 616 break; 617 case AST_FUNC_DECL: 618 fprintf(stderr, "FuncDecl(%.*s)\n", (int) node->func.name.sv.count, node->func.name.sv.items); 619 if (node->func.ret_type) { 620 debug_print_ast_node(node->func.ret_type, indent + 1); 621 } 622 for (int i = 0; i < node->func.arg_list.count; ++i) { 623 debug_print_ast_node(node->func.arg_list.items[i], indent + 1); 624 } 625 debug_print_ast_node(node->func.body, indent + 1); 626 break; 627 case AST_TYPE_DECL: 628 fprintf(stderr, "TypeDecl\n"); 629 break; 630 case AST_BLOCK: 631 fprintf(stderr, "Block {\n"); 632 for (int i = 0; i < node->block.list.count; ++i) { 633 debug_print_ast_node(node->block.list.items[i], indent + 1); 634 } 635 print_loc_pad(node->block.end_tok.loc, min_width); 636 fprintf(stderr, "%*s\n", indent * 2, "}"); 637 break; 638 case AST_PREFIX: 639 fprintf(stderr, "Prefix(%.*s)\n", (int) node->preop.op.tok.sv.count, node->preop.op.tok.sv.items); 640 debug_print_ast_node(node->preop.right, indent + 1); 641 break; 642 case AST_INFIX: 643 fprintf(stderr, "Infix(%.*s)\n", (int) node->inop.op.tok.sv.count, node->inop.op.tok.sv.items); 644 debug_print_ast_node(node->inop.left, indent + 1); 645 debug_print_ast_node(node->inop.right, indent + 1); 646 break; 647 case AST_POSTFIX: 648 fprintf(stderr, "Postfix(%.*s)\n", (int) node->postop.op.tok.sv.count, node->postop.op.tok.sv.items); 649 debug_print_ast_node(node->postop.left, indent + 1); 650 break; 651 case AST_NUMBER: 652 fprintf(stderr, "Number(%.*s)\n", (int) node->start_tok.sv.count, node->start_tok.sv.items); 653 break; 654 case AST_IDENT: 655 fprintf(stderr, "Ident(%.*s)\n", (int) node->start_tok.sv.count, node->start_tok.sv.items); 656 break; 657 case AST_OPERATOR_DECL: 658 fprintf(stderr, "Operator("); 659 if (node->operator.kind == OP_PREFIX) { 660 fprintf(stderr, "prefix, op: %.*s)\n", 661 (int) preops.items[node->operator.entry].sv.count, 662 preops.items[node->operator.entry].sv.items); 663 break; 664 } 665 if (node->operator.kind == OP_INFIX) { 666 fprintf(stderr, "infix, op: %.*s, prec: %i, assoc: %s)\n", 667 (int) inops.items[node->operator.entry].sv.count, 668 inops.items[node->operator.entry].sv.items, 669 inops.items[node->operator.entry].prec, 670 inops.items[node->operator.entry].right_assoc ? "right" : "left"); 671 break; 672 } 673 if (node->operator.kind == OP_POSTFIX) { 674 fprintf(stderr, "prefix, op: %.*s)\n", 675 (int) postops.items[node->operator.entry].sv.count, 676 postops.items[node->operator.entry].sv.items); 677 break; 678 } 679 unreachable("unknown AST_OPERATOR_DECL kind"); 680 case AST_ERROR: 681 fprintf(stderr, "Error\n"); 682 break; 683 default: 684 unreachable("debug_print_ast_node"); 685 } 686 } 687 688 void 689 dump_preops() 690 { 691 for (int i = 0; i < preops.count; i++) { 692 fprintf(stderr, "Prefix { op: %.*s }\n", 693 (int) preops.items[i].sv.count, 694 preops.items[i].sv.items); 695 } 696 } 697 698 void 699 dump_inops() 700 { 701 for (int i = 0; i < inops.count; i++) { 702 fprintf(stderr, "Infix { op: %.*s prec: %i assoc: %s }\n", 703 (int) inops.items[i].sv.count, 704 inops.items[i].sv.items, 705 inops.items[i].prec, 706 inops.items[i].right_assoc ? "right" : "left"); 707 } 708 } 709 710 void 711 dump_postops() 712 { 713 for (int i = 0; i < postops.count; i++) { 714 fprintf(stderr, "Postfix { op: %.*s }\n", 715 (int) postops.items[i].sv.count, 716 postops.items[i].sv.items); 717 } 718 }