From 13923af3ab57dbd826ce6f36b38228eea595590f Mon Sep 17 00:00:00 2001 From: Quinn Stephens Date: Mon, 9 Jun 2025 22:02:58 -0400 Subject: parser: Parse uninitialized variable declarations Signed-off-by: Quinn Stephens --- src/parser/parser.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/parser') diff --git a/src/parser/parser.c b/src/parser/parser.c index 4f8543c..ccdc037 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -78,6 +78,20 @@ parse_func_decl(struct parser *ctx, struct ast_node *func) return true; } +static bool +parse_var_decl(struct parser *ctx, struct ast_node *var) +{ + var->kind = NOK_VARIABLE; + + /* + * TODO: Parse initial value. + */ + + hashmap_add(ctx->syms, &var->hashmap_entry); + parser_advance(ctx); + return true; +} + static bool parse_decl(struct parser *ctx) { @@ -118,12 +132,11 @@ parse_decl(struct parser *ctx) parser_advance(ctx); if (ctx->tok.kind == TOK_LPAREN) { return parse_func_decl(ctx, node); + } else if (ctx->tok.kind == TOK_SEMICOLON) { + return parse_var_decl(ctx, node); } - /* - * TODO: Parse variable declarations. - */ - tok_error(&ctx->tok, "expected \"(\" after identifier\n"); + tok_error(&ctx->tok, "expected \"(\" or \";\" after identifier\n"); free(node); return false; } -- cgit v1.2.3