diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lexer.c | 19 | ||||
-rw-r--r-- | src/main.c | 16 |
2 files changed, 31 insertions, 4 deletions
diff --git a/src/lexer.c b/src/lexer.c index 2d2b6fc..2feba2a 100644 --- a/src/lexer.c +++ b/src/lexer.c | |||
@@ -1,3 +1,6 @@ | |||
1 | #ifndef LEXER_C | ||
2 | #define LEXER_C | ||
3 | |||
1 | #include "badlib.h" | 4 | #include "badlib.h" |
2 | 5 | ||
3 | #define LEXER_MEM GB(2) | 6 | #define LEXER_MEM GB(2) |
@@ -610,3 +613,19 @@ scan_token(Scanner *scanner) { | |||
610 | } | 613 | } |
611 | return emit_token(current, scanner, TOK_SYMBOL); | 614 | return emit_token(current, scanner, TOK_SYMBOL); |
612 | } | 615 | } |
616 | |||
617 | void | ||
618 | print_token(Token tok) { | ||
619 | println("%d:%d\t%s %s", tok.line, tok.col, token_str[tok.kind], tok.val); | ||
620 | } | ||
621 | |||
622 | void | ||
623 | print_tokens(Str path, Token *tokens) { | ||
624 | for (sz i = 0; i < array_size(tokens); i++) { | ||
625 | Token tok = tokens[i]; | ||
626 | print("%s:", path); | ||
627 | print_token(tok); | ||
628 | } | ||
629 | } | ||
630 | |||
631 | #endif // LEXER_C | ||
@@ -584,8 +584,12 @@ parse_keyword(Parser *parser) { | |||
584 | case TOK_IF: { | 584 | case TOK_IF: { |
585 | node = node_alloc(parser, NODE_IF, prev); | 585 | node = node_alloc(parser, NODE_IF, prev); |
586 | if (!node) return; | 586 | if (!node) return; |
587 | parse_consume(parser, TOK_LPAREN, | ||
588 | cstr("expected '(' on if expression")); | ||
587 | parse_expr(parser, PREC_LOW); | 589 | parse_expr(parser, PREC_LOW); |
588 | node->cond_if = array_pop(parser->nodes); | 590 | node->cond_if = array_pop(parser->nodes); |
591 | parse_consume(parser, TOK_RPAREN, | ||
592 | cstr("expected ')' on if expression")); | ||
589 | parse_expr(parser, PREC_LOW); | 593 | parse_expr(parser, PREC_LOW); |
590 | node->cond_expr = array_pop(parser->nodes); | 594 | node->cond_expr = array_pop(parser->nodes); |
591 | if (parse_match(parser, TOK_ELSE)) { | 595 | if (parse_match(parser, TOK_ELSE)) { |
@@ -596,10 +600,12 @@ parse_keyword(Parser *parser) { | |||
596 | case TOK_MATCH: { | 600 | case TOK_MATCH: { |
597 | node = node_alloc(parser, NODE_MATCH, prev); | 601 | node = node_alloc(parser, NODE_MATCH, prev); |
598 | if (!node) return; | 602 | if (!node) return; |
603 | parse_consume(parser, TOK_LPAREN, | ||
604 | cstr("expected '(' on if expression")); | ||
599 | parse_expr(parser, PREC_LOW); | 605 | parse_expr(parser, PREC_LOW); |
600 | node->match_expr = array_pop(parser->nodes); | 606 | node->match_expr = array_pop(parser->nodes); |
601 | parse_consume(parser, TOK_ASSIGN, | 607 | parse_consume(parser, TOK_RPAREN, |
602 | cstr("malformed match statement")); | 608 | cstr("expected ')' on if expression")); |
603 | parse_consume(parser, TOK_LCURLY, | 609 | parse_consume(parser, TOK_LCURLY, |
604 | cstr("expected block of match cases")); | 610 | cstr("expected block of match cases")); |
605 | while (!parse_match(parser, TOK_RCURLY) && !parser->panic) { | 611 | while (!parse_match(parser, TOK_RCURLY) && !parser->panic) { |
@@ -686,10 +692,12 @@ parse_keyword(Parser *parser) { | |||
686 | case TOK_WHILE: { | 692 | case TOK_WHILE: { |
687 | node = node_alloc(parser, NODE_WHILE, prev); | 693 | node = node_alloc(parser, NODE_WHILE, prev); |
688 | if (!node) return; | 694 | if (!node) return; |
695 | parse_consume(parser, TOK_LPAREN, | ||
696 | cstr("expected '(' on while expression")); | ||
689 | parse_expr(parser, PREC_LOW); | 697 | parse_expr(parser, PREC_LOW); |
690 | node->while_cond = array_pop(parser->nodes); | 698 | node->while_cond = array_pop(parser->nodes); |
691 | parse_consume(parser, TOK_ASSIGN, | 699 | parse_consume(parser, TOK_RPAREN, |
692 | cstr("malformed while expression")); | 700 | cstr("expected ')' on while expression")); |
693 | parse_expr(parser, PREC_LOW); | 701 | parse_expr(parser, PREC_LOW); |
694 | node->while_expr = array_pop(parser->nodes); | 702 | node->while_expr = array_pop(parser->nodes); |
695 | } break; | 703 | } break; |