diff options
| author | Baitinq <[email protected]> | 2025-01-12 18:35:39 +0100 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2025-01-12 18:35:39 +0100 |
| commit | 01a475bd9c9e2249816fc935e7045d78cafdc672 (patch) | |
| tree | be5a3941341976c0e972ec281371e54b58bb9abc /src | |
| parent | Add support for sum operator (diff) | |
| download | pry-lang-01a475bd9c9e2249816fc935e7045d78cafdc672.tar.gz pry-lang-01a475bd9c9e2249816fc935e7045d78cafdc672.tar.bz2 pry-lang-01a475bd9c9e2249816fc935e7045d78cafdc672.zip | |
Evaluator: Store variables with value instead of reference
Diffstat (limited to 'src')
| -rw-r--r-- | src/evaluator.zig | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/src/evaluator.zig b/src/evaluator.zig index ef83463..9e57dbd 100644 --- a/src/evaluator.zig +++ b/src/evaluator.zig @@ -7,14 +7,14 @@ const EvaluatorError = error{ }; pub const Evaluator = struct { - variables: std.StringHashMap(?*parser.Node), + variables: std.StringHashMap(?i64), allocator: std.mem.Allocator, pub fn init(allocator: std.mem.Allocator) !*Evaluator { const evaluator = try allocator.create(Evaluator); evaluator.* = .{ - .variables = std.StringHashMap(?*parser.Node).init(allocator), + .variables = std.StringHashMap(?i64).init(allocator), .allocator = allocator, }; return evaluator; @@ -60,14 +60,14 @@ pub const Evaluator = struct { } // Make sure identifier exists - _ = try self.get_expression_value(node.VARIABLE_STATEMENT.expression); + const val = try self.get_expression_value(node.VARIABLE_STATEMENT.expression); if (!self.variables.contains(variable_statement.name)) { std.debug.print("Variable not found: {s}\n", .{variable_statement.name}); return EvaluatorError.EvaluationError; } - try self.variables.put(variable_statement.name, node.VARIABLE_STATEMENT.expression); //TODO: We really should enforce this at the compiler level. + try self.variables.put(variable_statement.name, val); } fn evaluate_print_statement(self: *Evaluator, print_statement: *parser.Node) !void { @@ -86,12 +86,12 @@ pub const Evaluator = struct { return switch (node.EXPRESSION) { .NUMBER => |number| number.value, .IDENTIFIER => |identifier| { - const expression = self.variables.get(identifier.name) orelse { + const val = self.variables.get(identifier.name) orelse { std.debug.print("Identifier {any} not found\n", .{identifier.name}); return EvaluatorError.EvaluationError; }; - return try self.get_expression_value(expression orelse return EvaluatorError.EvaluationError); + return val.?; }, .BINARY => |operation| { //TODO: For now, this just represents sum |