diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-06-24 03:36:47 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-06-24 03:36:47 +0200 |
commit | 02ce35872c86d4ff056b8121121253fec40bafc0 (patch) | |
tree | c93360fab30cff47963dbce2b051107cebe64ee4 /src | |
parent | c4f84a90242b7c507b2636fc99f027345f56d42b (diff) |
Properly handle decl without assignment
Diffstat (limited to 'src')
-rw-r--r-- | src/codegen.c | 12 | ||||
-rw-r--r-- | 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) |