diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-08 23:03:42 +0100 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-08 23:04:58 +0100 |
commit | ad908730f06ba0f3bf314d33fb07ad61f5ac281e (patch) | |
tree | fb3f8c48b7b6dbaf750f2aed03b557f889e94ec8 | |
parent | Parser: Cleanup node creation (diff) | |
download | interpreter-ad908730f06ba0f3bf314d33fb07ad61f5ac281e.tar.gz interpreter-ad908730f06ba0f3bf314d33fb07ad61f5ac281e.tar.bz2 interpreter-ad908730f06ba0f3bf314d33fb07ad61f5ac281e.zip |
Parser: general cleanup
-rw-r--r-- | src/parser.zig | 56 |
1 files changed, 22 insertions, 34 deletions
diff --git a/src/parser.zig b/src/parser.zig index ba3d435..52a34b5 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -70,26 +70,18 @@ pub const Parser = struct { fn parse_statement(self: *Parser) ParserError!*Node { const token = self.peek_token() orelse return ParserError.ParsingError; - //TODO: Cleanup (avoid dupl) - if (token == .PRINT) { - const print_statement = try self.parse_print_statement(); - _ = try self.accept_token(tokenizer.TokenType.SEMICOLON); - - return self.create_node(.{ - .STATEMENT = .{ - .statement = print_statement, - }, - }); - } else { - const variable_statement = try self.parse_variable_statement(); - _ = try self.accept_token(tokenizer.TokenType.SEMICOLON); - - return self.create_node(.{ - .STATEMENT = .{ - .statement = variable_statement, - }, - }); - } + const statement = switch (token) { + .PRINT => try self.parse_print_statement(), + else => try self.parse_variable_statement(), + }; + + _ = try self.accept_token(tokenizer.TokenType.SEMICOLON); + + return self.create_node(.{ + .STATEMENT = .{ + .statement = statement, + }, + }); } // VariableStatement ::= ("let" IDENTIFIER | IDENTIFIER) EQUALS Expression @@ -125,8 +117,6 @@ pub const Parser = struct { const expression = try self.parse_expression(); - std.debug.print("PARSED expression: {any}\n", .{expression}); - _ = try self.accept_token(tokenizer.TokenType.RPAREN); return self.create_node(.{ @@ -138,27 +128,25 @@ pub const Parser = struct { // Expression :== NUMBER | IDENTIFIER fn parse_expression(self: *Parser) ParserError!*Node { - const token = self.peek_token() orelse return ParserError.ParsingError; + const token = self.consume_token() orelse return ParserError.ParsingError; - if (token == .NUMBER) { - const a = try self.accept_token(tokenizer.TokenType.NUMBER); - return self.create_node(.{ + return switch (token) { + .NUMBER => |number_token| self.create_node(.{ .EXPRESSION = .{ .NUMBER = .{ - .value = a.NUMBER, + .value = number_token, }, }, - }); - } else { - const a = try self.accept_token(tokenizer.TokenType.IDENTIFIER); - return self.create_node(.{ + }), + .IDENTIFIER => |identifier_token| self.create_node(.{ .EXPRESSION = .{ .IDENTIFIER = .{ - .name = a.IDENTIFIER, + .name = identifier_token, }, }, - }); - } + }), + else => unreachable, + }; } fn accept_token(self: *Parser, expected_token: tokenizer.TokenType) ParserError!tokenizer.Token { |