From 02ce35872c86d4ff056b8121121253fec40bafc0 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Mon, 24 Jun 2024 03:36:47 +0200 Subject: Properly handle decl without assignment --- src/codegen.c | 12 ++++++++++-- src/parser.c | 3 +-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/codegen.c b/src/codegen.c index 8218bc3..6fc7848 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -248,7 +248,11 @@ codegendecl(struct cgctx ctx, idx_t i) LLVMValueRef globl = LLVMAddGlobal(ctx.mod, t, svtocstr(name, sv)); LLVMValueRef v; - i = codegentypedexpr(ctx, p.rhs, ctx.types[i], &v); + if (p.rhs == AST_EMPTY) { + v = LLVMConstNull(t); + i = fwdnode(ctx.ast, i); + } else + i = codegentypedexpr(ctx, p.rhs, ctx.types[i], &v); LLVMSetInitializer(globl, v); LLVMSetLinkage(globl, LLVMInternalLinkage); return i; @@ -258,7 +262,11 @@ codegendecl(struct cgctx ctx, idx_t i) /* TODO: Namespace the name */ strview_t sv = ctx.toks.strs[ctx.ast.lexemes[i]]; var = symtab_insert(&ctx.scps[ctx.scpi].map, sv, NULL)->v; - i = codegentypedexpr(ctx, p.rhs, ctx.types[i], &val); + if (p.rhs == AST_EMPTY) { + val = LLVMConstNull(type2llvm(ctx, ctx.types[i])); + i = fwdnode(ctx.ast, i); + } else + i = codegentypedexpr(ctx, p.rhs, ctx.types[i], &val); LLVMBuildStore(ctx.bob, val, var); return i; } diff --git a/src/parser.c b/src/parser.c index 8705310..5b12e21 100644 --- a/src/parser.c +++ b/src/parser.c @@ -50,8 +50,7 @@ fwdnode(ast_t ast, idx_t i) i = ast.kids[i].lhs == AST_EMPTY ? i + 1 : ast.kids[i].rhs; break; case ASTDECL: - i = ast.kids[i].rhs == AST_EMPTY ? ast.kids[i].lhs - : ast.kids[i].rhs; + i = ast.kids[i].rhs == AST_EMPTY ? i + 1 : ast.kids[i].rhs; break; case ASTRET: if (ast.kids[i].rhs == AST_EMPTY) -- cgit v1.2.3