1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 19:38:12 +00:00

LibCpp: Parse If statements

This commit is contained in:
Itamar 2021-01-29 12:03:02 +02:00 committed by Andreas Kling
parent e8f040139b
commit 8ed65d7b48
4 changed files with 70 additions and 0 deletions

View file

@ -186,6 +186,10 @@ NonnullRefPtr<Statement> Parser::parse_statement(ASTNode& parent)
if (match_keyword("for")) {
consume_semicolumn.disarm();
return parse_for_statement(parent);
}
if (match_keyword("if")) {
consume_semicolumn.disarm();
return parse_if_statement(parent);
} else {
error("unexpected statement type");
consume_semicolumn.disarm();
@ -997,4 +1001,23 @@ NonnullRefPtr<ForStatement> Parser::parse_for_statement(ASTNode& parent)
return for_statement;
}
NonnullRefPtr<IfStatement> Parser::parse_if_statement(ASTNode& parent)
{
SCOPE_LOGGER();
auto if_statement = create_ast_node<IfStatement>(parent, position(), {});
consume(Token::Type::Keyword);
consume(Token::Type::LeftParen);
if_statement->m_predicate = parse_expression(*if_statement);
consume(Token::Type::RightParen);
if_statement->m_then = parse_statement(*if_statement);
if (match_keyword("else")) {
consume(Token::Type::Keyword);
if_statement->m_else = parse_statement(*if_statement);
if_statement->set_end(if_statement->m_else->end());
} else {
if_statement->set_end(if_statement->m_then->end());
}
return if_statement;
}
}