diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-19 23:40:05 +0100 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-19 23:40:05 +0100 |
commit | fcead42285fe37990d1843c3d3da39f65fd426c0 (patch) | |
tree | 3d15851970de503613be095b25b7b4d2753b06fa | |
parent | Bug: Fix bug with accept_parse (diff) | |
download | interpreter-fcead42285fe37990d1843c3d3da39f65fd426c0.tar.gz interpreter-fcead42285fe37990d1843c3d3da39f65fd426c0.tar.bz2 interpreter-fcead42285fe37990d1843c3d3da39f65fd426c0.zip |
Start working on proper operator precedence
-rw-r--r-- | examples/7.src | 2 | ||||
-rw-r--r-- | grammar.ebnf | 2 | ||||
-rw-r--r-- | src/parser.zig | 9 |
3 files changed, 10 insertions, 3 deletions
diff --git a/examples/7.src b/examples/7.src index b96cba8..3bb1b91 100644 --- a/examples/7.src +++ b/examples/7.src @@ -1,7 +1,7 @@ let main = () => { let i = 4; - if i + 1 == 5 { + if i + 1 == 5 - (1 + 1) { print(i); return i; }; diff --git a/grammar.ebnf b/grammar.ebnf index aaa0e78..bde99cb 100644 --- a/grammar.ebnf +++ b/grammar.ebnf @@ -12,7 +12,7 @@ ReturnStatement ::= RETURN Expression FunctionArguments ::= Expression ("," Expression)* -Expression ::= EqualityExpression | AdditiveExpression | FunctionDefinition +Expression ::= EqualityExpression | AdditiveExpression | FunctionDefinition | LPAREN Expression RPAREN EqualityExpression ::= AdditiveExpression "==" AdditiveExpression diff --git a/src/parser.zig b/src/parser.zig index f87c406..76f2127 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -217,10 +217,17 @@ pub const Parser = struct { } }); } - // Expression ::= EqualityExpression | AdditiveExpression | FunctionDefinition + // Expression ::= EqualityExpression | AdditiveExpression | FunctionDefinition | LPAREN Expression RPAREN 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 |