codegen: prepare for variable emitting
This commit is contained in:
parent
7def9abc5a
commit
3f48be3420
2 changed files with 32 additions and 23 deletions
|
@ -3,7 +3,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
fn f() i32 {
|
fn f() i32 {
|
||||||
var a = 3;
|
// var a = 3;
|
||||||
// return a;
|
// return a;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(
|
fn emitExpr(
|
||||||
self: *Codegen,
|
self: *Codegen,
|
||||||
builder: var,
|
builder: var,
|
||||||
|
@ -77,7 +101,6 @@ pub const Codegen = struct {
|
||||||
// TODO if expr is Variable, we should do a variable lookup
|
// TODO if expr is Variable, we should do a variable lookup
|
||||||
// in a symbol table, going up in scope, etc.
|
// in a symbol table, going up in scope, etc.
|
||||||
|
|
||||||
// TODO VarDecl add things to the symbol table
|
|
||||||
// TODO Assign modify symbol table
|
// TODO Assign modify symbol table
|
||||||
return switch (expr.*) {
|
return switch (expr.*) {
|
||||||
|
|
||||||
|
@ -136,29 +159,10 @@ pub const Codegen = struct {
|
||||||
// and emit accordingly
|
// and emit accordingly
|
||||||
var kv_sym_opt = self.ctx.symbol_table.get(vari.lexeme);
|
var kv_sym_opt = self.ctx.symbol_table.get(vari.lexeme);
|
||||||
if (kv_sym_opt) |kv| {
|
if (kv_sym_opt) |kv| {
|
||||||
var sym = kv.value;
|
return try self.emitForVariableType(vari, get, kv);
|
||||||
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
|
// if not, its likely a variable, we should handle it accordingly
|
||||||
// as well
|
// as well
|
||||||
@panic("TODO handle variables");
|
@panic("TODO handle variables");
|
||||||
},
|
},
|
||||||
|
@ -214,6 +218,8 @@ pub const Codegen = struct {
|
||||||
return llvm.LLVMBuildStore(builder, null, assign_expr);
|
return llvm.LLVMBuildStore(builder, null, assign_expr);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
.Variable => @panic("TODO emit variables"),
|
||||||
|
|
||||||
else => {
|
else => {
|
||||||
std.debug.warn("Got unexpected expr {}\n", ast.ExprType(expr.*));
|
std.debug.warn("Got unexpected expr {}\n", ast.ExprType(expr.*));
|
||||||
return CompileError.EmitError;
|
return CompileError.EmitError;
|
||||||
|
@ -307,6 +313,9 @@ pub const Codegen = struct {
|
||||||
//llvm.LLVMAddIncoming(phi, &else_bb_val, &else_bb, 1);
|
//llvm.LLVMAddIncoming(phi, &else_bb_val, &else_bb, 1);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
.VarDecl => {},
|
||||||
|
|
||||||
else => {
|
else => {
|
||||||
std.debug.warn("Got unexpected stmt {}\n", stmt.*);
|
std.debug.warn("Got unexpected stmt {}\n", stmt.*);
|
||||||
return CompileError.EmitError;
|
return CompileError.EmitError;
|
||||||
|
|
Loading…
Reference in a new issue