codegen: add incomplete generation of expressions
This commit is contained in:
parent
05c480c364
commit
4c1bdb5f91
2 changed files with 45 additions and 0 deletions
|
@ -16,6 +16,43 @@ pub const Codegen = struct {
|
|||
return Codegen{ .allocator = allocator };
|
||||
}
|
||||
|
||||
fn genExpr(self: *Codegen, builder: var, expr: *const ast.Expr) !llvm.LLVMValueRef {
|
||||
// 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
|
||||
|
||||
// TODO Calls fetch symbol table, check arity of it at codegen level
|
||||
switch (expr.*) {
|
||||
|
||||
// TODO handle all literals, construct llvm values for them
|
||||
.Literal => |literal| {},
|
||||
.Binary => |binary| {
|
||||
var left = try self.genExpr(builder, binary.left);
|
||||
var right = try self.genExpr(builder, binary.right);
|
||||
|
||||
return switch (binary.op.lexeme[0]) {
|
||||
// TODO other operators
|
||||
'+' => llvm.LLVMBuildAdd(builder, left, right, c"addtmp"),
|
||||
|
||||
// TODO codegen errors
|
||||
else => @panic("invalid binary operator"),
|
||||
};
|
||||
},
|
||||
|
||||
// TODO codegen errors
|
||||
else => @panic("invalid expr"),
|
||||
}
|
||||
}
|
||||
|
||||
fn genFuncStmt(self: *Codegen, builder: var, stmt: *const ast.Stmt) !void {
|
||||
switch (stmt.*) {
|
||||
.Expr => |expr| try self.genExpr(builder, expr),
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
|
||||
fn genNode(
|
||||
self: *Codegen,
|
||||
mod: llvm.LLVMModuleRef,
|
||||
|
@ -50,6 +87,11 @@ pub const Codegen = struct {
|
|||
var builder = llvm.LLVMCreateBuilder();
|
||||
llvm.LLVMPositionBuilderAtEnd(builder, entry);
|
||||
|
||||
for (decl.body.toSlice()) |stmt| {
|
||||
// TODO custom function context for us
|
||||
try self.genFuncStmt(builder, stmt);
|
||||
}
|
||||
|
||||
// TODO codegen decl.body
|
||||
var tmp = llvm.LLVMBuildAdd(
|
||||
builder,
|
||||
|
|
|
@ -46,6 +46,9 @@ pub fn run(allocator: *std.mem.Allocator, slice: []const u8) !Result {
|
|||
std.debug.warn("parse tree\n");
|
||||
printer.printNode(root.?, 0);
|
||||
|
||||
// TODO type pass
|
||||
// TODO variable pass
|
||||
|
||||
var cgen = codegen.Codegen.init(allocator);
|
||||
try cgen.gen(root.?);
|
||||
|
||||
|
|
Loading…
Reference in a new issue