fixed tests

This commit is contained in:
Karma Riuk
2025-07-08 18:08:17 +02:00
parent 74c555bfc0
commit e3cbba08b1
2 changed files with 17 additions and 0 deletions

View File

@@ -16,6 +16,12 @@ namespace parser {
next = lexer.next_token(); next = lexer.next_token();
} }
void parser::skip_until_semicolon() {
for (; current.type != token::type::SEMICOLON
&& current.type != token::type::END_OF_FILE;
next_token()) {};
}
ast::program* parser::parse_program() { ast::program* parser::parse_program() {
ast::program* p = new ast::program(); ast::program* p = new ast::program();
@@ -51,6 +57,8 @@ namespace parser {
next_token(); next_token();
stmt->value = parse_expression(); stmt->value = parse_expression();
if (next.type == token::type::SEMICOLON)
next_token();
return stmt; return stmt;
} }
@@ -60,6 +68,7 @@ namespace parser {
if (!expect_next(token::type::IDENTIFIER)) { if (!expect_next(token::type::IDENTIFIER)) {
delete stmt; delete stmt;
skip_until_semicolon();
return nullptr; return nullptr;
} }
@@ -67,10 +76,14 @@ namespace parser {
if (!expect_next(token::type::ASSIGN)) { if (!expect_next(token::type::ASSIGN)) {
delete stmt; delete stmt;
skip_until_semicolon();
return nullptr; return nullptr;
} }
next_token();
stmt->value = parse_expression(); stmt->value = parse_expression();
if (next.type == token::type::SEMICOLON)
next_token();
return stmt; return stmt;
} }
@@ -79,6 +92,9 @@ namespace parser {
stmt->expression = parse_expression(); stmt->expression = parse_expression();
if (next.type == token::type::SEMICOLON)
next_token();
return stmt; return stmt;
}; };

View File

@@ -32,6 +32,7 @@ namespace parser {
std::unordered_map<token::type, infix_parse_fn> infix_parse_fns; std::unordered_map<token::type, infix_parse_fn> infix_parse_fns;
void next_token(); void next_token();
void skip_until_semicolon();
ast::statement* parse_statement(); ast::statement* parse_statement();
ast::expression* parse_expression(); ast::expression* parse_expression();
ast::let_stmt* parse_let(); ast::let_stmt* parse_let();