diff options
| author | Baitinq <[email protected]> | 2025-01-22 00:25:22 +0100 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2025-01-22 00:25:22 +0100 |
| commit | 6d1d1355379feb3273c3634adfc50a1876c40314 (patch) | |
| tree | d5302638c8569a4d0905ef1fd4ef1b4e39e6a2de /src/parser.zig | |
| parent | Feature: Add support for while statements (diff) | |
| download | pry-lang-6d1d1355379feb3273c3634adfc50a1876c40314.tar.gz pry-lang-6d1d1355379feb3273c3634adfc50a1876c40314.tar.bz2 pry-lang-6d1d1355379feb3273c3634adfc50a1876c40314.zip | |
Feature: Add support for division and multiplication
Diffstat (limited to 'src/parser.zig')
| -rw-r--r-- | src/parser.zig | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/src/parser.zig b/src/parser.zig index 13509ea..966cad3 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -15,6 +15,7 @@ const NodeType = enum { WHILE_STATEMENT, EQUALITY_EXPRESSION, ADDITIVE_EXPRESSION, + MULTIPLICATIVE_EXPRESSION, UNARY_EXPRESSION, PRIMARY_EXPRESSION, FUNCTION_DEFINITION, @@ -54,6 +55,11 @@ pub const Node = union(NodeType) { lhs: *Node, rhs: *Node, }, + MULTIPLICATIVE_EXPRESSION: struct { + multiplication: bool, + lhs: *Node, + rhs: *Node, + }, UNARY_EXPRESSION: struct { negation: bool, expression: *Node, @@ -268,11 +274,11 @@ pub const Parser = struct { } }); } - // AdditiveExpression ::= UnaryExpression (("+" | "-") UnaryExpression)* + // AdditiveExpression ::= MultiplicativeExpression (("+" | "-") MultiplicativeExpression)* fn parse_additive_expression(self: *Parser) ParserError!*Node { errdefer if (!self.try_context) std.debug.print("Error parsing additive expression\n", .{}); - var lhs = try self.parse_unary_expression(); + var lhs = try self.parse_multiplicative_expression(); while (true) { const plus = self.accept_token(tokenizer.TokenType.PLUS); @@ -280,7 +286,7 @@ pub const Parser = struct { if (plus == null and minus == null) break; - const rhs = try self.parse_unary_expression(); + const rhs = try self.parse_multiplicative_expression(); lhs = try self.create_node(.{ .ADDITIVE_EXPRESSION = .{ .addition = plus != null, @@ -292,6 +298,30 @@ pub const Parser = struct { return lhs; } + // MultiplicativeExpression ::= UnaryExpression (("*" | "/") UnaryExpression)* + fn parse_multiplicative_expression(self: *Parser) ParserError!*Node { + errdefer if (!self.try_context) std.debug.print("Error parsing additive expression\n", .{}); + + var lhs = try self.parse_unary_expression(); + + while (true) { + const mul = self.accept_token(tokenizer.TokenType.MUL); + const div = self.accept_token(tokenizer.TokenType.DIV); + + if (mul == null and div == null) break; + + const rhs = try self.parse_unary_expression(); + + lhs = try self.create_node(.{ .MULTIPLICATIVE_EXPRESSION = .{ + .multiplication = mul != null, + .lhs = lhs, + .rhs = rhs, + } }); + } + + return lhs; + } + // UnaryExpression ::= "!" UnaryExpression | PrimaryExpression fn parse_unary_expression(self: *Parser) ParserError!*Node { errdefer if (!self.try_context) std.debug.print("Error parsing unary expression\n", .{}); |