diff options
| author | Baitinq <[email protected]> | 2025-01-18 19:30:40 +0100 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2025-01-18 19:30:40 +0100 |
| commit | 70fe80f1cb9e4331a7341f6eaf50c0386a106e53 (patch) | |
| tree | 9a742dc21be20c347ab6af47218de99728f7bc37 | |
| parent | Evaluator: Create environment abstraction for handling scopes (diff) | |
| download | interpreter-70fe80f1cb9e4331a7341f6eaf50c0386a106e53.tar.gz interpreter-70fe80f1cb9e4331a7341f6eaf50c0386a106e53.tar.bz2 interpreter-70fe80f1cb9e4331a7341f6eaf50c0386a106e53.zip | |
Evaluator: create and drop scopes when entering/leaving functions
| -rw-r--r-- | src/evaluator.zig | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/evaluator.zig b/src/evaluator.zig index fbc2f8e..95aecd1 100644 --- a/src/evaluator.zig +++ b/src/evaluator.zig @@ -152,6 +152,9 @@ pub const Evaluator = struct { const function_definition = node.*.FUNCTION_DEFINITION; + try self.environment.create_scope(); + defer self.environment.drop_scope(); + var i: usize = 0; while (i < function_definition.statements.len - 1) { const stmt = function_definition.statements[i]; @@ -196,14 +199,16 @@ const Environment = struct { } fn create_scope(self: *Environment) !void { - const global_scope = try self.allocator.create(Scope); - global_scope.* = .{ + const scope = try self.allocator.create(Scope); + scope.* = .{ .variables = std.StringHashMap(?*Variable).init(self.allocator), }; - try self.scope_stack.append(global_scope); + try self.scope_stack.append(scope); } - fn drop_scope() !void {} + fn drop_scope(self: *Environment) void { + _ = self.scope_stack.pop(); + } fn add_variable(self: *Environment, name: []const u8, variable: ?*Variable) !void { try self.scope_stack.getLast().variables.put(name, variable); |