codegen: add emitting for enum get expressions

This commit is contained in:
Luna 2019-09-25 12:28:22 -03:00
parent 5188dac3c0
commit 1040eef79f

View file

@ -110,9 +110,52 @@ pub const Codegen = struct {
};
},
// TODO codegen errors
.Get => |get| {
// TODO rename get.struc to get.target
var struc = get.struc.*;
switch (struc) {
.Variable => |vari| {
// first, we must check if the target is a type
// and emit accordingly
var kv_sym_opt = self.ctx.symbol_table.get(vari.lexeme);
if (kv_sym_opt) |kv| {
var sym = kv.value;
switch (sym) {
.Enum => |map| {
var val = map.get(get.name.lexeme);
if (val == null) {
std.debug.warn(
"enum {} does not have field {}\n",
vari.lexeme,
get.name.lexeme,
);
}
return llvm.LLVMConstInt(llvm.LLVMInt32Type(), val.?.value, 1);
},
.Struct => @panic("TODO handle struct"),
else => {
std.debug.warn("Invalid get target: {}\n", comp.SymbolType(sym));
return CompileError.EmitError;
},
}
}
// if not, its likely a variable, we should do it accordingly
// as well
@panic("TODO handle variables");
},
else => {
std.debug.warn("Invalid get target: {}\n", ast.ExprType(struc));
return CompileError.EmitError;
},
}
},
else => {
std.debug.warn("Got unexpected expr {}\n", expr.*);
std.debug.warn("Got unexpected expr {}\n", ast.ExprType(expr.*));
return CompileError.EmitError;
},
};