summary refs log tree commit diff
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-08 00:04:17 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-08 00:04:17 +0100
commit230c0be4ffad3e0339b63b3a2363ea2d9ce3dc67 (patch)
tree6a15f39e44fb815d7a463ac36cbac73e1ee3695f
parentParser: Implement print statement parsing properly and cleanup (diff)
downloadinterpreter-230c0be4ffad3e0339b63b3a2363ea2d9ce3dc67.tar.gz
interpreter-230c0be4ffad3e0339b63b3a2363ea2d9ce3dc67.tar.bz2
interpreter-230c0be4ffad3e0339b63b3a2363ea2d9ce3dc67.zip
Parser: Prepare for variable statement parsing
-rw-r--r--src/parser.zig40
1 files changed, 29 insertions, 11 deletions
diff --git a/src/parser.zig b/src/parser.zig
index f55f5a2..7fb6372 100644
--- a/src/parser.zig
+++ b/src/parser.zig
@@ -93,20 +93,38 @@ pub const Parser = struct {
     }
 
     fn parse_statement(self: *Parser) ParserError!*Node {
-        //TODO: Add support for parsing variable declaration and assignment. Also here we shouldnt parse numbers/identifiers directly
-        const token = self.peek_token();
+        //TODO: Add support for parsing variable declaration and assignment.
+        const token = self.peek_token() orelse return ParserError.ParsingError;
         std.debug.print("PARSING: {any}\n", .{token});
 
-        const print_statement = try self.parse_print_statement();
-        _ = try self.accept_token(tokenizer.TokenType.SEMICOLON);
+        //TODO: Cleanup (avoid dupl)
+        if (token == .PRINT) {
+            const print_statement = try self.parse_print_statement();
+            _ = try self.accept_token(tokenizer.TokenType.SEMICOLON);
 
-        const node = try self.allocator.create(Node);
-        node.* = .{
-            .STATEMENT = .{
-                .statement = print_statement,
-            },
-        };
-        return node;
+            const node = try self.allocator.create(Node);
+            node.* = .{
+                .STATEMENT = .{
+                    .statement = print_statement,
+                },
+            };
+            return node;
+        } else {
+            const variable_statement = try self.parse_variable_statement();
+            _ = try self.accept_token(tokenizer.TokenType.SEMICOLON);
+
+            const node = try self.allocator.create(Node);
+            node.* = .{
+                .STATEMENT = .{
+                    .statement = variable_statement,
+                },
+            };
+            return node;
+        }
+    }
+
+    fn parse_variable_statement(_: *Parser) ParserError!*Node {
+        @panic("UNIMPLEMENTED parse_variable_statement");
     }
 
     fn parse_print_statement(self: *Parser) ParserError!*Node {