diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-12 18:35:39 +0100 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-12 18:35:39 +0100 |
commit | eb1e1fc1e70bdde6a453f82dea2348c42ccb198d (patch) | |
tree | be5a3941341976c0e972ec281371e54b58bb9abc | |
parent | Add support for sum operator (diff) | |
download | interpreter-eb1e1fc1e70bdde6a453f82dea2348c42ccb198d.tar.gz interpreter-eb1e1fc1e70bdde6a453f82dea2348c42ccb198d.tar.bz2 interpreter-eb1e1fc1e70bdde6a453f82dea2348c42ccb198d.zip |
Evaluator: Store variables with value instead of reference
-rw-r--r-- | examples/3.src | 6 | ||||
-rw-r--r-- | flake.nix | 1 | ||||
-rw-r--r-- | src/evaluator.zig | 12 | ||||
-rw-r--r-- | todo/0.src | 5 |
4 files changed, 11 insertions, 13 deletions
diff --git a/examples/3.src b/examples/3.src index 042edea..4b56092 100644 --- a/examples/3.src +++ b/examples/3.src @@ -1,3 +1,5 @@ -let sixteen = 10 + 2 + 4; +let seventeen = 10 + 2 + 4; -print(sixteen); +seventeen = seventeen + 1; + +print(seventeen); diff --git a/flake.nix b/flake.nix index d638325..9d08953 100644 --- a/flake.nix +++ b/flake.nix @@ -22,6 +22,7 @@ buildInputs = with pkgs; [ zig.packages."${system}".master zls + gdb ]; }; in { 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 diff --git a/todo/0.src b/todo/0.src deleted file mode 100644 index 2784a86..0000000 --- a/todo/0.src +++ /dev/null @@ -1,5 +0,0 @@ -let x = 1; - -x = x; - -print(x); |