From a44abe5e7300976e331c21bca8315e5134784be7 Mon Sep 17 00:00:00 2001 From: me Date: Sat, 10 Jan 2026 16:56:51 +0200 Subject: [PATCH] move parse open parens out of parse_function --- first-interpreter/src/parse.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/first-interpreter/src/parse.c b/first-interpreter/src/parse.c index b8f46d8..152a281 100644 --- a/first-interpreter/src/parse.c +++ b/first-interpreter/src/parse.c @@ -43,7 +43,6 @@ char* parse_identifier(TokenArray tokens, unsigned* tokens_index) { Expr parse_expr(TokenArray tokens, unsigned* tokens_index); Expr parse_function(TokenArray tokens, unsigned* tokens_index, char* name) { - parse_token(tokens, tokens_index, OPENPAREN); Expr* exprs = (Expr*)malloc((tokens.length - *tokens_index) * sizeof(Expr)); unsigned expr_index = 0; while (*tokens_index < tokens.length && tokens.tokens[*tokens_index].tag != CLOSEPAREN) { @@ -79,7 +78,7 @@ Expr parse_expr(TokenArray tokens, unsigned* tokens_index) { }; } case IDENTIFIER: { - if (*tokens_index < tokens.length && tokens.tokens[*tokens_index].tag == OPENPAREN) { + if (maybe_parse_token(tokens, tokens_index, OPENPAREN)) { return parse_function(tokens, tokens_index, token.data.identifier); } else { return (Expr) { @@ -132,8 +131,8 @@ Stmt parse_stmt(TokenArray tokens, unsigned* tokens_index) { }; } // Expression - else { - Expr expr = parse_function(tokens, tokens_index, identifier); + else if (maybe_parse_token(tokens, tokens_index, OPENPAREN)) { + Expr expr = parse_function(tokens, tokens_index, identifier); return (Stmt) { .tag = EXPR, @@ -142,6 +141,11 @@ Stmt parse_stmt(TokenArray tokens, unsigned* tokens_index) { }, }; } + // Error + else { + fprintf(stderr, "Parse error: Unexpected token. Expected '(' or '='"); + exit(1); + } } StmtArray parse_block(TokenArray tokens, unsigned* tokens_index) {