about summary refs log tree commit diff
path: root/src/parser.zig
diff options
context:
space:
mode:
authorBaitinq <[email protected]>2025-01-23 19:08:30 +0100
committerBaitinq <[email protected]>2025-01-23 19:08:30 +0100
commit7a6a2ffbb02f7cb404ea124a99a7b0c93a27da43 (patch)
tree8dd67aefa96929b8f7518c59b4a5a3088d144e61 /src/parser.zig
parentExample: Add functions as values example (diff)
downloadinterpreter-7a6a2ffbb02f7cb404ea124a99a7b0c93a27da43.tar.gz
interpreter-7a6a2ffbb02f7cb404ea124a99a7b0c93a27da43.tar.bz2
interpreter-7a6a2ffbb02f7cb404ea124a99a7b0c93a27da43.zip
Parser: Better functions as values
Diffstat (limited to 'src/parser.zig')
-rw-r--r--src/parser.zig11
1 files changed, 6 insertions, 5 deletions
diff --git a/src/parser.zig b/src/parser.zig
index a13f1cc..9b86f4c 100644
--- a/src/parser.zig
+++ b/src/parser.zig
@@ -231,12 +231,11 @@ pub const Parser = struct {
         } });
     }
 
-    // Expression   ::= EqualityExpression | AdditiveExpression | FunctionDefinition
+    // Expression   ::= EqualityExpression | AdditiveExpression
     fn parse_expression(self: *Parser) ParserError!*Node {
         errdefer if (!self.try_context) std.debug.print("Error parsing expression\n", .{});
 
         return self.accept_parse(parse_equality_expression) orelse
-            self.accept_parse(parse_function_definition) orelse
             self.accept_parse(parse_additive_expression) orelse
             return ParserError.ParsingError;
     }
@@ -323,18 +322,20 @@ pub const Parser = struct {
         } });
     }
 
-    // PrimaryExpression ::= NUMBER | BOOLEAN | IDENTIFIER | FunctionCallStatement | LPAREN Expression RPAREN
+    // PrimaryExpression ::= NUMBER | BOOLEAN | IDENTIFIER | FunctionCallStatement | FunctionDefinition | LPAREN Expression RPAREN
     fn parse_primary_expression(self: *Parser) ParserError!*Node {
         errdefer if (!self.try_context) std.debug.print("Error parsing primary expression\n", .{});
 
+        if (self.accept_parse(parse_function_definition)) |stmt| return stmt;
+        if (self.accept_parse(parse_function_call_statement)) |stmt| return stmt;
+
+        // LPAREN (Expression) RPAREN
         if (self.accept_token(tokenizer.TokenType.LPAREN)) |_| {
             const expr = try self.parse_expression();
             _ = try self.parse_token(tokenizer.TokenType.RPAREN);
             return expr;
         }
 
-        if (self.accept_parse(parse_function_call_statement)) |stmt| return stmt;
-
         const token = self.consume_token() orelse return ParserError.ParsingError;
 
         return switch (token.type) {