about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBaitinq <[email protected]>2025-01-29 23:55:19 +0100
committerBaitinq <[email protected]>2025-01-29 23:55:19 +0100
commitc488d1a21c925ff60ddc5706f2c0991259a75cc8 (patch)
treef6d8d56644c30029505b44bd7ca8dec02a63f0df
parentCodegen: Support variable reassignment (diff)
downloadinterpreter-c488d1a21c925ff60ddc5706f2c0991259a75cc8.tar.gz
interpreter-c488d1a21c925ff60ddc5706f2c0991259a75cc8.tar.bz2
interpreter-c488d1a21c925ff60ddc5706f2c0991259a75cc8.zip
Codegen: Support identifiers in return expressions
Diffstat (limited to '')
-rw-r--r--examples/2.src2
-rw-r--r--src/codegen.zig10
2 files changed, 8 insertions, 4 deletions
diff --git a/examples/2.src b/examples/2.src
index 561d626..96b0596 100644
--- a/examples/2.src
+++ b/examples/2.src
@@ -3,7 +3,7 @@ let main = () => {
 
 	test = 7;
 
-	print(test);
+	printf(test);
 
 	return test;
 };
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 {