codegen: add emitting of const declarations

This commit is contained in:
Luna 2019-09-25 17:05:37 -03:00
parent cb8908dc80
commit 04c624e210

View file

@ -316,6 +316,25 @@ pub const Codegen = struct {
// NOTE: enums don't have specific llvm ir code generated for them
.Enum => {},
.ConstDecl => |constdecls| {
for (constdecls.toSlice()) |constdecl| {
const name = constdecl.name.lexeme;
var const_type = self.ctx.symbol_table.get(name).?.value;
var const_llvm_type = try self.typeToLLVM(const_type.Const);
const const_name = try std.cstr.addNullByte(self.allocator, name);
errdefer self.allocator.free(const_name);
var global = llvm.LLVMAddGlobal(mod, const_llvm_type, const_name.ptr);
var builder = llvm.LLVMCreateBuilder();
var expr_llvm_val = try self.emitExpr(builder, constdecl.expr);
llvm.LLVMSetInitializer(global, expr_llvm_val);
}
},
else => {
std.debug.warn("TODO handle node type {}\n", @tagName(node.*));
return;