diff options
author | Manuel Palenzuela Merino <manuel.palenzuela@datadoghq.com> | 2025-01-18 19:30:40 +0100 |
---|---|---|
committer | Manuel Palenzuela Merino <manuel.palenzuela@datadoghq.com> | 2025-01-18 19:30:40 +0100 |
commit | ac9aad101bef5ddddc7c6c3bd7480d56575589ef (patch) | |
tree | 9a742dc21be20c347ab6af47218de99728f7bc37 | |
parent | Evaluator: Create environment abstraction for handling scopes (diff) | |
download | interpreter-ac9aad101bef5ddddc7c6c3bd7480d56575589ef.tar.gz interpreter-ac9aad101bef5ddddc7c6c3bd7480d56575589ef.tar.bz2 interpreter-ac9aad101bef5ddddc7c6c3bd7480d56575589ef.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); |