diff options
| author | Baitinq <[email protected]> | 2025-01-21 00:10:04 +0100 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2025-01-21 00:10:04 +0100 |
| commit | 630d3082c5b6632e9dfb5d3f518d638b6201c310 (patch) | |
| tree | 246a49681855671b9f57f85712e22fb0ad298d25 /src | |
| parent | Example: Add example for while loop (diff) | |
| download | interpreter-630d3082c5b6632e9dfb5d3f518d638b6201c310.tar.gz interpreter-630d3082c5b6632e9dfb5d3f518d638b6201c310.tar.bz2 interpreter-630d3082c5b6632e9dfb5d3f518d638b6201c310.zip | |
Parser: Make additive expression left associative
Diffstat (limited to 'src')
| -rw-r--r-- | src/parser.zig | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/src/parser.zig b/src/parser.zig index 76f2127..eead436 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -251,17 +251,21 @@ pub const Parser = struct { } }); } - // AdditiveExpression ::= PrimaryExpression (("+" | "-") AdditiveExpression)? + // AdditiveExpression ::= PrimaryExpression (("+" | "-") PrimaryExpression)* fn parse_additive_expression(self: *Parser) ParserError!*Node { errdefer if (!self.try_context) std.debug.print("Error parsing additive expression\n", .{}); - const lhs = try self.parse_primary_expression(); + var lhs = try self.parse_primary_expression(); - const plus = self.accept_token(tokenizer.TokenType.PLUS); - const minus = self.accept_token(tokenizer.TokenType.MINUS); - if (plus != null or minus != null) { - const rhs = try self.parse_additive_expression(); - return self.create_node(.{ .ADDITIVE_EXPRESSION = .{ + while (true) { + const plus = self.accept_token(tokenizer.TokenType.PLUS); + const minus = self.accept_token(tokenizer.TokenType.MINUS); + + if (plus == null and minus == null) break; + + const rhs = try self.parse_primary_expression(); + + lhs = try self.create_node(.{ .ADDITIVE_EXPRESSION = .{ .addition = plus != null, .lhs = lhs, .rhs = rhs, |