diff options
| author | Baitinq <[email protected]> | 2025-01-08 22:08:11 +0100 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2025-01-08 22:08:29 +0100 |
| commit | c3c5751a9af5ad2c0edc03c78847edde21bd6327 (patch) | |
| tree | c1e0dea968c65ed9f4fd609826a1d9441c4b53c4 | |
| parent | Parser: Use arena allocator (diff) | |
| download | interpreter-c3c5751a9af5ad2c0edc03c78847edde21bd6327.tar.gz interpreter-c3c5751a9af5ad2c0edc03c78847edde21bd6327.tar.bz2 interpreter-c3c5751a9af5ad2c0edc03c78847edde21bd6327.zip | |
Parser: Add support for parsing variable statements
| -rw-r--r-- | src/parser.zig | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/src/parser.zig b/src/parser.zig index e855cbd..d6fb5d9 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -68,9 +68,7 @@ pub const Parser = struct { } fn parse_statement(self: *Parser) ParserError!*Node { - //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}); //TODO: Cleanup (avoid dupl) if (token == .PRINT) { @@ -98,8 +96,30 @@ pub const Parser = struct { } } - fn parse_variable_statement(_: *Parser) ParserError!*Node { - @panic("UNIMPLEMENTED parse_variable_statement"); + fn parse_variable_statement(self: *Parser) ParserError!*Node { + const token = self.peek_token() orelse return ParserError.ParsingError; + + var is_declaration: bool = false; + if (token == .LET) { + is_declaration = true; + _ = self.consume_token() orelse return ParserError.ParsingError; + } + + const identifier = try self.accept_token(tokenizer.TokenType.IDENTIFIER); + + _ = try self.accept_token(tokenizer.TokenType.EQUALS); + + const expression = try self.parse_expression(); + + const node = try self.allocator.create(Node); + node.* = .{ + .VARIABLE_STATEMENT = .{ + .is_declaration = is_declaration, + .name = identifier.IDENTIFIER, + .expression = @constCast(expression), + }, + }; + return node; } fn parse_print_statement(self: *Parser) ParserError!*Node { |