Fix stmt not consume semicolon
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include <format>
|
#include <format>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace camellya {
|
namespace camellya {
|
||||||
|
|
||||||
@@ -12,6 +13,7 @@ Program Parser::parse() {
|
|||||||
try {
|
try {
|
||||||
statements.push_back(declaration());
|
statements.push_back(declaration());
|
||||||
} catch (const ParseError& error) {
|
} catch (const ParseError& error) {
|
||||||
|
std::cerr << error.what() << std::endl;
|
||||||
synchronize();
|
synchronize();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,7 +120,6 @@ StmtPtr Parser::class_declaration() {
|
|||||||
members.push_back(function_declaration());
|
members.push_back(function_declaration());
|
||||||
} else {
|
} else {
|
||||||
members.push_back(var_declaration());
|
members.push_back(var_declaration());
|
||||||
consume(TokenType::SEMICOLON, "Expected ';' after field declaration.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,6 +169,7 @@ StmtPtr Parser::var_declaration() {
|
|||||||
if (match({TokenType::EQUAL})) {
|
if (match({TokenType::EQUAL})) {
|
||||||
initializer = expression();
|
initializer = expression();
|
||||||
}
|
}
|
||||||
|
consume(TokenType::SEMICOLON, "Expected ';' after variable declaration.");
|
||||||
|
|
||||||
return std::make_unique<VarDecl>(type_name, name.lexeme, std::move(initializer));
|
return std::make_unique<VarDecl>(type_name, name.lexeme, std::move(initializer));
|
||||||
}
|
}
|
||||||
@@ -250,12 +252,7 @@ StmtPtr Parser::block_statement() {
|
|||||||
std::vector<StmtPtr> statements;
|
std::vector<StmtPtr> statements;
|
||||||
|
|
||||||
while (!check(TokenType::RIGHT_BRACE) && !is_at_end()) {
|
while (!check(TokenType::RIGHT_BRACE) && !is_at_end()) {
|
||||||
auto stmt = declaration();
|
statements.push_back(declaration());
|
||||||
// If declaration returned a VarDecl (not a class/function/statement), consume semicolon
|
|
||||||
if (dynamic_cast<VarDecl*>(stmt.get())) {
|
|
||||||
consume(TokenType::SEMICOLON, "Expected ';' after variable declaration.");
|
|
||||||
}
|
|
||||||
statements.push_back(std::move(stmt));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
consume(TokenType::RIGHT_BRACE, "Expected '}' after block.");
|
consume(TokenType::RIGHT_BRACE, "Expected '}' after block.");
|
||||||
|
|||||||
Reference in New Issue
Block a user