summary refs log tree commit diff
path: root/src/evaluator.zig
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-12 19:47:16 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-12 19:47:32 +0100
commita362ee47453e48b9b90eb225b555fd5e5cc5e465 (patch)
tree0f9355bd993c6863361bb643397c49140d4428a5 /src/evaluator.zig
parentEvaluator: Store variables with value instead of reference (diff)
downloadinterpreter-a362ee47453e48b9b90eb225b555fd5e5cc5e465.tar.gz
interpreter-a362ee47453e48b9b90eb225b555fd5e5cc5e465.tar.bz2
interpreter-a362ee47453e48b9b90eb225b555fd5e5cc5e465.zip
Implement "return"
Diffstat (limited to 'src/evaluator.zig')
-rw-r--r--src/evaluator.zig15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/evaluator.zig b/src/evaluator.zig
index 9e57dbd..fd0c8e5 100644
--- a/src/evaluator.zig
+++ b/src/evaluator.zig
@@ -8,6 +8,7 @@ const EvaluatorError = error{
 
 pub const Evaluator = struct {
     variables: std.StringHashMap(?i64),
+    return_value: i64,
 
     allocator: std.mem.Allocator,
 
@@ -16,6 +17,7 @@ pub const Evaluator = struct {
         evaluator.* = .{
             .variables = std.StringHashMap(?i64).init(allocator),
             .allocator = allocator,
+            .return_value = 0,
         };
         return evaluator;
     }
@@ -34,7 +36,7 @@ pub const Evaluator = struct {
             try self.evaluate_statement(statement);
         }
 
-        return 0;
+        return self.return_value;
     }
 
     fn evaluate_statement(self: *Evaluator, statement: *parser.Node) !void {
@@ -44,6 +46,7 @@ pub const Evaluator = struct {
         return switch (statement.STATEMENT.statement.*) {
             .VARIABLE_STATEMENT => |*variable_statement| try self.evaluate_variable_statement(@ptrCast(variable_statement)),
             .PRINT_STATEMENT => |*print_statement| try self.evaluate_print_statement(@ptrCast(print_statement)),
+            .RETURN_STATEMENT => |*return_statement| try self.evaluate_return_statement(@ptrCast(return_statement)),
             else => unreachable,
         };
     }
@@ -79,6 +82,16 @@ pub const Evaluator = struct {
         std.debug.print("PRINT: {d}\n", .{print_value});
     }
 
+    // TODO: This is a functionless implementation of return, we should not do this
+    fn evaluate_return_statement(self: *Evaluator, return_statement: *parser.Node) !void {
+        errdefer std.debug.print("Error evaluating return statement\n", .{});
+        std.debug.assert(return_statement.* == parser.Node.RETURN_STATEMENT);
+
+        const return_value = try self.get_expression_value(return_statement.RETURN_STATEMENT.expression);
+
+        self.return_value = return_value;
+    }
+
     fn get_expression_value(self: *Evaluator, node: *parser.Node) !i64 {
         errdefer std.debug.print("Error getting statement value\n", .{});
         std.debug.assert(node.* == parser.Node.EXPRESSION);