From 3f48be3420facca86bb527ab2b4ae4126ccf1a5c Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 27 Sep 2019 17:18:38 -0300 Subject: [PATCH] codegen: prepare for variable emitting --- examples/hello.ry | 2 +- src/codegen.zig | 53 +++++++++++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/examples/hello.ry b/examples/hello.ry index 9be2bf2..e935ddc 100644 --- a/examples/hello.ry +++ b/examples/hello.ry @@ -3,7 +3,7 @@ const ( ) fn f() i32 { - var a = 3; + // var a = 3; // return a; return 2; } diff --git a/src/codegen.zig b/src/codegen.zig index 63af360..64c7e72 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -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;