about summary refs log tree commit diff
path: root/src/parser.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser.zig')
-rw-r--r--src/parser.zig26
1 files changed, 19 insertions, 7 deletions
diff --git a/src/parser.zig b/src/parser.zig
index ad2b8d0..b0a469c 100644
--- a/src/parser.zig
+++ b/src/parser.zig
@@ -37,9 +37,9 @@ pub const Node = union(enum) {
         rhs: *Node,
     },
     MULTIPLICATIVE_EXPRESSION: struct {
-        multiplication: bool,
         lhs: *Node,
         rhs: *Node,
+        typ: MultiplicativeExpressionType,
     },
     UNARY_EXPRESSION: struct {
         negation: bool,
@@ -82,6 +82,12 @@ pub const EqualityExpressionType = enum {
     GT,
 };
 
+pub const MultiplicativeExpressionType = enum {
+    MUL,
+    DIV,
+    MOD,
+};
+
 pub const Parser = struct {
     tokens: []tokenizer.Token,
     offset: u32,
@@ -328,24 +334,30 @@ pub const Parser = struct {
         return lhs;
     }
 
-    // MultiplicativeExpression ::= UnaryExpression (("*" | "/") UnaryExpression)*
+    // MultiplicativeExpression ::= UnaryExpression (("*" | "/" | "%") UnaryExpression)*
     fn parse_multiplicative_expression(self: *Parser) ParserError!*Node {
         errdefer if (!self.try_context) std.debug.print("Error parsing additive expression {any}\n", .{self.peek_token()});
 
         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;
+            var typ: MultiplicativeExpressionType = undefined;
+            if (self.accept_token(tokenizer.TokenType.MUL) != null) {
+                typ = .MUL;
+            } else if (self.accept_token(tokenizer.TokenType.DIV) != null) {
+                typ = .DIV;
+            } else if (self.accept_token(tokenizer.TokenType.MOD) != null) {
+                typ = .MOD;
+            } else {
+                break;
+            }
 
             const rhs = try self.parse_unary_expression();
 
             lhs = try self.create_node(.{ .MULTIPLICATIVE_EXPRESSION = .{
-                .multiplication = mul != null,
                 .lhs = lhs,
                 .rhs = rhs,
+                .typ = typ,
             } });
         }