From eed58fa9d2bccf9df9128e6eff08fcd08b6fa95e Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Wed, 19 Jun 2024 08:43:17 +0200 Subject: Change a few syntax constructs for consistency --- src/lexer.c | 19 +++++++++++++++++++ src/main.c | 16 ++++++++++++---- tests/conditionals.bad | 14 +++++++------- tests/loops.bad | 18 ++++++++++++------ 4 files changed, 50 insertions(+), 17 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 @@ +#ifndef LEXER_C +#define LEXER_C + #include "badlib.h" #define LEXER_MEM GB(2) @@ -610,3 +613,19 @@ scan_token(Scanner *scanner) { } return emit_token(current, scanner, TOK_SYMBOL); } + +void +print_token(Token tok) { + println("%d:%d\t%s %s", tok.line, tok.col, token_str[tok.kind], tok.val); +} + +void +print_tokens(Str path, Token *tokens) { + for (sz i = 0; i < array_size(tokens); i++) { + Token tok = tokens[i]; + print("%s:", path); + print_token(tok); + } +} + +#endif // LEXER_C diff --git a/src/main.c b/src/main.c index 666a664..d001d9c 100644 --- a/src/main.c +++ b/src/main.c @@ -584,8 +584,12 @@ parse_keyword(Parser *parser) { case TOK_IF: { node = node_alloc(parser, NODE_IF, prev); if (!node) return; + parse_consume(parser, TOK_LPAREN, + cstr("expected '(' on if expression")); parse_expr(parser, PREC_LOW); node->cond_if = array_pop(parser->nodes); + parse_consume(parser, TOK_RPAREN, + cstr("expected ')' on if expression")); parse_expr(parser, PREC_LOW); node->cond_expr = array_pop(parser->nodes); if (parse_match(parser, TOK_ELSE)) { @@ -596,10 +600,12 @@ parse_keyword(Parser *parser) { case TOK_MATCH: { node = node_alloc(parser, NODE_MATCH, prev); if (!node) return; + parse_consume(parser, TOK_LPAREN, + cstr("expected '(' on if expression")); parse_expr(parser, PREC_LOW); node->match_expr = array_pop(parser->nodes); - parse_consume(parser, TOK_ASSIGN, - cstr("malformed match statement")); + parse_consume(parser, TOK_RPAREN, + cstr("expected ')' on if expression")); parse_consume(parser, TOK_LCURLY, cstr("expected block of match cases")); while (!parse_match(parser, TOK_RCURLY) && !parser->panic) { @@ -686,10 +692,12 @@ parse_keyword(Parser *parser) { case TOK_WHILE: { node = node_alloc(parser, NODE_WHILE, prev); if (!node) return; + parse_consume(parser, TOK_LPAREN, + cstr("expected '(' on while expression")); parse_expr(parser, PREC_LOW); node->while_cond = array_pop(parser->nodes); - parse_consume(parser, TOK_ASSIGN, - cstr("malformed while expression")); + parse_consume(parser, TOK_RPAREN, + cstr("expected ')' on while expression")); parse_expr(parser, PREC_LOW); node->while_expr = array_pop(parser->nodes); } break; diff --git a/tests/conditionals.bad b/tests/conditionals.bad index 856ac8a..e1a456e 100644 --- a/tests/conditionals.bad +++ b/tests/conditionals.bad @@ -1,26 +1,26 @@ ; Basic if expressions. -if true "hello" +if (true) "hello" ; These can produce values and the result bound to a name. let a = if (2 + 2 >= 4) 42 ; We support a single if expression. -let b = if 0xff == 255 "hello" else "world" +let b = if (0xff == 255) "hello" else "world" ; ... but these can compound on each other -if 1 < 2 6 -else if 1 > 2 7 +if (1 < 2) 6 +else if (1 > 2) 7 else 8 ; A block is an expression, and if raise the scope level regardless if a block ; is used or not. -if true != false { +if (true != false) { let a = "yo" } ; Match cases should only apply to literal values, for example `case 1 + 2` ; isn't allowed. They should generally convert to a lookup table. -match a = { +match (a) { case 8 = "hello" case 9 = "world" else = "what" @@ -37,7 +37,7 @@ enum flags { let a:flags = flags.node_add ; No need to qualify enum fields inside match-case. -match a = { +match (a) { case node_add = "adding something..." case node_add = "subbing something..." else = "default case" diff --git a/tests/loops.bad b/tests/loops.bad index 5221844..45c9712 100644 --- a/tests/loops.bad +++ b/tests/loops.bad @@ -1,18 +1,24 @@ ; The most basic loop is the while loop. -while abc = "heelo" +while (abc) "heelo" -while 1 + 2 == 3 = { +while (1 + 2 == 3) { "hello" } -while symbol = { +while (symbol) { "hello" } - ; We could use some sugar for this. let i = 0 -while i < 10 = { +while (i < 10) { "hello" - set i = i + 1 + ; --i } +; TODO: add post/pre increment tokens +; TODO: add functions +; TODO: add function calls + +; for let i = 1, 1 < 10, i++ { + +; } -- cgit v1.2.1