summary refs log tree commit diff
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-21 00:18:29 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-21 00:18:29 +0100
commitace95945deccfbea0214e1387de8c2806cd9de82 (patch)
tree50f0813d95bcce41636130ab82cf3b6a501a48c7
parentParser: Make additive expression left associative (diff)
downloadinterpreter-ace95945deccfbea0214e1387de8c2806cd9de82.tar.gz
interpreter-ace95945deccfbea0214e1387de8c2806cd9de82.tar.bz2
interpreter-ace95945deccfbea0214e1387de8c2806cd9de82.zip
Parser: Add support for parenthesis grouping
-rw-r--r--examples/7.src2
-rw-r--r--grammar.ebnf4
-rw-r--r--src/parser.zig18
3 files changed, 12 insertions, 12 deletions
diff --git a/examples/7.src b/examples/7.src
index 8e3ad60..1245e45 100644
--- a/examples/7.src
+++ b/examples/7.src
@@ -1,7 +1,7 @@
 let main = () => {
 	let i = 4;
 	
-	if i + 1 == 5 - 1 + 1 {
+	if (i - 1) == 5 - (1 + 1) {
 		print(i);
 		return i;
 	};
diff --git a/grammar.ebnf b/grammar.ebnf
index b770797..a251f26 100644
--- a/grammar.ebnf
+++ b/grammar.ebnf
@@ -12,13 +12,13 @@ ReturnStatement ::= RETURN Expression
 
 FunctionArguments ::= Expression ("," Expression)*
 
-Expression   ::= EqualityExpression | AdditiveExpression | FunctionDefinition | LPAREN Expression RPAREN
+Expression   ::= EqualityExpression | AdditiveExpression | FunctionDefinition
 
 EqualityExpression ::= AdditiveExpression "==" AdditiveExpression
 
 AdditiveExpression ::= PrimaryExpression (("+" | "-") PrimaryExpression)*
 
-PrimaryExpression ::= NUMBER | BOOLEAN | IDENTIFIER | FunctionCallStatement
+PrimaryExpression ::= NUMBER | BOOLEAN | IDENTIFIER | FunctionCallStatement | LPAREN Expression RPAREN
 
 FunctionDefinition ::= LPAREN FunctionParamters? RPAREN ARROW LBRACE Statement* ReturnStatement SEMICOLON RBRACE
 
diff --git a/src/parser.zig b/src/parser.zig
index eead436..ce5522d 100644
--- a/src/parser.zig
+++ b/src/parser.zig
@@ -217,17 +217,10 @@ pub const Parser = struct {
         } });
     }
 
-    // Expression   ::= EqualityExpression | AdditiveExpression | FunctionDefinition | LPAREN Expression RPAREN
+    // Expression   ::= EqualityExpression | AdditiveExpression | FunctionDefinition
     fn parse_expression(self: *Parser) ParserError!*Node {
         errdefer if (!self.try_context) std.debug.print("Error parsing expression\n", .{});
 
-        if (self.accept_token(tokenizer.TokenType.LPAREN)) |_| {
-            const expr = try self.parse_expression();
-            _ = try self.parse_token(tokenizer.TokenType.RPAREN);
-            std.debug.print("HERE!\n", .{});
-            return expr;
-        }
-
         return self.accept_parse(parse_equality_expression) orelse
             self.accept_parse(parse_additive_expression) orelse
             self.accept_parse(parse_function_definition) orelse
@@ -275,10 +268,17 @@ pub const Parser = struct {
         return lhs;
     }
 
-    // PrimaryExpression ::= NUMBER | BOOLEAN | IDENTIFIER | FunctionCallStatement
+    // PrimaryExpression ::= NUMBER | BOOLEAN | IDENTIFIER | FunctionCallStatement | 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_token(tokenizer.TokenType.LPAREN)) |_| {
+            const expr = try self.parse_expression();
+            _ = try self.parse_token(tokenizer.TokenType.RPAREN);
+            std.debug.print("HERE!\n", .{});
+            return expr;
+        }
+
         if (self.accept_parse(parse_function_call_statement)) |stmt| return stmt;
 
         const token = self.consume_token() orelse return ParserError.ParsingError;