summary refs log tree commit diff
path: root/src/evaluator.zig
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-23 22:55:57 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-23 22:55:57 +0100
commitae01bb4fe9c216552e7165f9e25c82d046aa121f (patch)
treec72b8cb9a0906d037343a05ce6a1a944c670d33e /src/evaluator.zig
parentSync grammar (diff)
downloadinterpreter-ae01bb4fe9c216552e7165f9e25c82d046aa121f.tar.gz
interpreter-ae01bb4fe9c216552e7165f9e25c82d046aa121f.tar.bz2
interpreter-ae01bb4fe9c216552e7165f9e25c82d046aa121f.zip
Feature: Support calling funtion definitions
Diffstat (limited to 'src/evaluator.zig')
-rw-r--r--src/evaluator.zig27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/evaluator.zig b/src/evaluator.zig
index ec6a698..3a145f8 100644
--- a/src/evaluator.zig
+++ b/src/evaluator.zig
@@ -97,16 +97,25 @@ pub const Evaluator = struct {
 
         const function_call_statement = node.FUNCTION_CALL_STATEMENT;
 
-        // Print function implementation
-        if (std.mem.eql(u8, function_call_statement.name, "print")) {
-            std.debug.assert(function_call_statement.arguments.len == 1);
-            std.debug.print("PRINT: {any}\n", .{try self.get_expression_value(function_call_statement.arguments[0])});
-            return null;
-        }
-
-        const function_definition = self.environment.get_variable(function_call_statement.name) orelse return EvaluatorError.EvaluationError;
+        switch (function_call_statement.expression.*) {
+            .FUNCTION_DEFINITION => |*function_definition| {
+                return try self.evaluate_function_definition(@ptrCast(function_definition), function_call_statement.arguments);
+            },
+            .PRIMARY_EXPRESSION => |*primary_expression| {
+                std.debug.assert(primary_expression.* == .IDENTIFIER);
+
+                // Print function implementation
+                if (std.mem.eql(u8, function_call_statement.expression.PRIMARY_EXPRESSION.IDENTIFIER.name, "print")) {
+                    std.debug.assert(function_call_statement.arguments.len == 1);
+                    std.debug.print("PRINT: {any}\n", .{try self.get_expression_value(function_call_statement.arguments[0])});
+                    return null;
+                }
 
-        return self.evaluate_function_definition(function_definition.FUNCTION_DEFINITION, function_call_statement.arguments);
+                const function_definition = self.environment.get_variable(primary_expression.IDENTIFIER.name) orelse return EvaluatorError.EvaluationError;
+                return self.evaluate_function_definition(function_definition.FUNCTION_DEFINITION, function_call_statement.arguments);
+            },
+            else => unreachable,
+        }
     }
 
     fn evaluate_if_statement(self: *Evaluator, node: *parser.Node) !?*Variable {