diff options
| author | Baitinq <[email protected]> | 2025-02-12 21:04:54 +0100 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2025-02-12 21:04:54 +0100 |
| commit | 137e6ba510d752b21ddb61440bfbc266416cfdbe (patch) | |
| tree | 3cb8fedb339bb3e68823d142b8b84d8f30f65ccb /src/codegen.zig | |
| parent | Codegen: Fix bug with function as variables (diff) | |
| download | pry-lang-137e6ba510d752b21ddb61440bfbc266416cfdbe.tar.gz pry-lang-137e6ba510d752b21ddb61440bfbc266416cfdbe.tar.bz2 pry-lang-137e6ba510d752b21ddb61440bfbc266416cfdbe.zip | |
Codegen: Properly support global vars
Diffstat (limited to '')
| -rw-r--r-- | src/codegen.zig | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/codegen.zig b/src/codegen.zig index 6783f96..a23ff32 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -128,8 +128,6 @@ pub const CodeGen = struct { const assignment_statement = statement.ASSIGNMENT_STATEMENT; - std.debug.assert(self.environment.contains_variable(assignment_statement.name) != assignment_statement.is_declaration); - if (assignment_statement.is_declaration and self.environment.scope_stack.items.len > 1 and assignment_statement.expression.* != .FUNCTION_DEFINITION) { const x = try std.fmt.allocPrintZ(self.arena, "{s}", .{assignment_statement.name}); const alloca = core.LLVMBuildAlloca(self.builder, core.LLVMInt64Type(), x); @@ -312,6 +310,15 @@ pub const CodeGen = struct { .NUMBER => |n| { var variable: types.LLVMValueRef = undefined; if (name != null) { + const x = try std.fmt.allocPrintZ(self.arena, "{s}", .{name.?}); + if (self.environment.scope_stack.items.len == 1) { + const ptr = try self.create_variable(.{ + .value = core.LLVMAddGlobal(self.llvm_module, core.LLVMInt64Type(), x), + .type = core.LLVMInt64Type(), + }); + core.LLVMSetInitializer(ptr.value, core.LLVMConstInt(core.LLVMInt64Type(), @intCast(n.value), 0)); + return ptr; + } const ptr = self.environment.get_variable(name.?) orelse unreachable; const val = core.LLVMConstInt(core.LLVMInt64Type(), @intCast(n.value), 0); @@ -335,6 +342,15 @@ pub const CodeGen = struct { var variable: types.LLVMValueRef = undefined; if (name != null) { + const x = try std.fmt.allocPrintZ(self.arena, "{s}", .{name.?}); + if (self.environment.scope_stack.items.len == 1) { + const ptr = try self.create_variable(.{ + .value = core.LLVMAddGlobal(self.llvm_module, core.LLVMInt1Type(), x), + .type = core.LLVMInt1Type(), + }); + core.LLVMSetInitializer(ptr.value, core.LLVMConstInt(core.LLVMInt1Type(), @intCast(int_value), 0)); + return ptr; + } const ptr = self.environment.get_variable(name.?) orelse unreachable; const val = core.LLVMConstInt(core.LLVMInt1Type(), @intCast(int_value), 0); |