Add parse_initialization_statement
This commit is contained in:
parent
9969ad12f6
commit
48feebc714
@ -43,6 +43,28 @@ namespace parsing {
|
||||
}
|
||||
}
|
||||
|
||||
Result<std::unique_ptr<AST::InitializationStatement>, std::string> parse_init_statement(token::TokenStream& stream) {
|
||||
token::TokenStream inner{ stream };
|
||||
try {
|
||||
auto ty = parse_type(inner).unwrap();
|
||||
auto name = inner.expect(token::Type::Ident);
|
||||
|
||||
std::optional<std::unique_ptr<AST::Expression>> expr = {};
|
||||
if (inner.peek().type == token::Type::Symbol && inner.peek().content == "=") {
|
||||
inner.expect(token::Type::Symbol, "=");
|
||||
expr = parse_expression(inner).unwrap();
|
||||
}
|
||||
|
||||
inner.expect(token::Type::Symbol, ";");
|
||||
|
||||
stream.m_position = inner.m_position;
|
||||
return &std::make_unique<AST::InitializationStatement>(std::move(ty), name.content, std::move(expr));
|
||||
}
|
||||
catch (std::runtime_error error) {
|
||||
return new std::string{ error.what() };
|
||||
}
|
||||
}
|
||||
|
||||
Result<std::unique_ptr<AST::Statement>, std::string> parse_statement(token::TokenStream& stream) {
|
||||
token::TokenStream inner{ stream };
|
||||
try {
|
||||
@ -57,20 +79,13 @@ namespace parsing {
|
||||
return new std::unique_ptr<AST::Statement>{ ret };
|
||||
}
|
||||
else if (inner.peek().type == token::Type::Ident) {
|
||||
auto ty = parse_type(inner).unwrap();
|
||||
auto name = inner.expect(token::Type::Ident);
|
||||
|
||||
std::optional<std::unique_ptr<AST::Expression>> expr = {};
|
||||
if (inner.peek().type == token::Type::Symbol && inner.peek().content == "=") {
|
||||
inner.expect(token::Type::Symbol, "=");
|
||||
expr = parse_expression(inner).unwrap();
|
||||
auto init = parse_init_statement(inner);
|
||||
if (init.ok()) {
|
||||
return new std::unique_ptr<AST::Statement>{ init.unwrap() };
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Expected initialization statement");
|
||||
}
|
||||
|
||||
inner.expect(token::Type::Symbol, ";");
|
||||
|
||||
stream.m_position = inner.m_position;
|
||||
auto init = new AST::InitializationStatement{ std::move(ty), name.content, std::move(expr) };
|
||||
return new std::unique_ptr<AST::Statement>{ init };
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Expected return-keyword");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user