diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-23 22:55:57 +0100 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-23 22:55:57 +0100 |
commit | ae01bb4fe9c216552e7165f9e25c82d046aa121f (patch) | |
tree | c72b8cb9a0906d037343a05ce6a1a944c670d33e /src/evaluator.zig | |
parent | Sync grammar (diff) | |
download | interpreter-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.zig | 27 |
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 { |