summary refs log tree commit diff
path: root/src/codegen.zig
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-29 23:55:19 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-29 23:55:19 +0100
commit38f4543279469452eddc4f2dc6c118a52ca7597e (patch)
treef6d8d56644c30029505b44bd7ca8dec02a63f0df /src/codegen.zig
parentCodegen: Support variable reassignment (diff)
downloadinterpreter-38f4543279469452eddc4f2dc6c118a52ca7597e.tar.gz
interpreter-38f4543279469452eddc4f2dc6c118a52ca7597e.tar.bz2
interpreter-38f4543279469452eddc4f2dc6c118a52ca7597e.zip
Codegen: Support identifiers in return expressions
Diffstat (limited to '')
-rw-r--r--src/codegen.zig10
1 files changed, 7 insertions, 3 deletions
diff --git a/src/codegen.zig b/src/codegen.zig
index 4191ebc..d89f163 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -199,10 +199,14 @@ pub const CodeGen = struct {
 
         const expression = statement.RETURN_STATEMENT.expression;
         std.debug.assert(expression.* == parser.Node.PRIMARY_EXPRESSION);
-        const primary_expr = expression.PRIMARY_EXPRESSION.NUMBER.value;
-        // std.debug.assert(primary_expr == parser.Node.PRIMARY_EXPRESSION.NUMBER);
 
-        _ = core.LLVMBuildRet(self.builder, core.LLVMConstInt(core.LLVMInt64Type(), @intCast(primary_expr), 0));
+        const num_argument: types.LLVMValueRef = switch (expression.PRIMARY_EXPRESSION) {
+            .NUMBER => |n| core.LLVMConstInt(core.LLVMInt64Type(), @intCast(n.value), 0),
+            .IDENTIFIER => |i| core.LLVMBuildLoad2(self.builder, core.LLVMInt64Type(), self.symbol_table.get(i.name).?.value, "").?,
+            else => unreachable,
+        };
+
+        _ = core.LLVMBuildRet(self.builder, num_argument);
     }
 
     pub fn create_entrypoint(self: *CodeGen) CodeGenError!void {