codegen: prepare for variable emitting

This commit is contained in:
Luna 2019-09-27 17:18:38 -03:00
parent 7def9abc5a
commit 3f48be3420
2 changed files with 32 additions and 23 deletions

View file

@ -3,7 +3,7 @@ const (
)
fn f() i32 {
var a = 3;
// var a = 3;
// return a;
return 2;
}

View file

@ -69,6 +69,30 @@ pub const Codegen = struct {
};
}
fn emitForVariableType(self: *@This(), vari: var, get: var, kv: var) !llvm.LLVMValueRef {
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;
},
}
}
fn emitExpr(
self: *Codegen,
builder: var,
@ -77,7 +101,6 @@ pub const Codegen = struct {
// TODO if expr is Variable, we should do a variable lookup
// in a symbol table, going up in scope, etc.
// TODO VarDecl add things to the symbol table
// TODO Assign modify symbol table
return switch (expr.*) {
@ -136,29 +159,10 @@ pub const Codegen = struct {
// 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;
},
}
return try self.emitForVariableType(vari, get, kv);
}
// if not, its likely a variable, we should do it accordingly
// if not, its likely a variable, we should handle it accordingly
// as well
@panic("TODO handle variables");
},
@ -214,6 +218,8 @@ pub const Codegen = struct {
return llvm.LLVMBuildStore(builder, null, assign_expr);
},
.Variable => @panic("TODO emit variables"),
else => {
std.debug.warn("Got unexpected expr {}\n", ast.ExprType(expr.*));
return CompileError.EmitError;
@ -307,6 +313,9 @@ pub const Codegen = struct {
//llvm.LLVMAddIncoming(phi, &else_bb_val, &else_bb, 1);
},
// TODO
.VarDecl => {},
else => {
std.debug.warn("Got unexpected stmt {}\n", stmt.*);
return CompileError.EmitError;