summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-31 01:15:16 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-31 01:16:56 +0100
commit487f5d25fc2a75a870f76317af3ca92716c875c4 (patch)
tree44910812f45c4e79127c1e0483fb5c6f00255dbe /src
parentCodegen: Fix function as variables (diff)
downloadinterpreter-487f5d25fc2a75a870f76317af3ca92716c875c4.tar.gz
interpreter-487f5d25fc2a75a870f76317af3ca92716c875c4.tar.bz2
interpreter-487f5d25fc2a75a870f76317af3ca92716c875c4.zip
Codegen: Start handling global variables
Diffstat (limited to 'src')
-rw-r--r--src/codegen.zig13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/codegen.zig b/src/codegen.zig
index f5ef608..dc9c1da 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -208,9 +208,16 @@ pub const CodeGen = struct {
             },
             .PRIMARY_EXPRESSION => |primary_expression| switch (primary_expression) {
                 .NUMBER => |n| {
-                    const ptr = core.LLVMBuildAlloca(self.builder, core.LLVMInt64Type(), "") orelse return CodeGenError.CompilationError;
-                    _ = core.LLVMBuildStore(self.builder, core.LLVMConstInt(core.LLVMInt64Type(), @intCast(n.value), 0), ptr) orelse return CodeGenError.CompilationError;
-                    const variable = core.LLVMBuildLoad2(self.builder, core.LLVMInt64Type(), ptr, "") orelse return CodeGenError.CompilationError;
+                    // Global variables
+                    var variable: types.LLVMValueRef = undefined;
+                    if (self.environment.scope_stack.items.len == 1) {
+                        variable = core.LLVMConstInt(core.LLVMInt64Type(), @intCast(n.value), 0);
+                    } else {
+                        const ptr = core.LLVMBuildAlloca(self.builder, core.LLVMInt64Type(), "") orelse return CodeGenError.CompilationError;
+                        _ = core.LLVMBuildStore(self.builder, core.LLVMConstInt(core.LLVMInt64Type(), @intCast(n.value), 0), ptr) orelse return CodeGenError.CompilationError;
+                        variable = core.LLVMBuildLoad2(self.builder, core.LLVMInt64Type(), ptr, "") orelse return CodeGenError.CompilationError;
+                    }
+
                     return try self.create_variable(.{
                         .value = variable,
                         .type = core.LLVMInt64Type(),