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

LibJS: Parse "if" statements

This patch implements basic parsing of "if" statements. We don't yet
support parsing "else", so I added a FIXME about that.
This commit is contained in:
Andreas Kling 2020-03-21 18:40:17 +01:00
parent 55c845713a
commit 7c48c3c8e1
4 changed files with 26 additions and 7 deletions

View file

@ -194,6 +194,8 @@ NonnullRefPtr<Statement> Parser::parse_statement()
return parse_variable_declaration();
case TokenType::For:
return parse_for_statement();
case TokenType::If:
return parse_if_statement();
default:
if (match_expression())
return adopt(*new ExpressionStatement(parse_expression(0)));
@ -512,6 +514,17 @@ NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration()
return create_ast_node<VariableDeclaration>(create_ast_node<Identifier>(name), move(initializer), declaration_type);
}
NonnullRefPtr<IfStatement> Parser::parse_if_statement()
{
consume(TokenType::If);
consume(TokenType::ParenOpen);
auto predicate = parse_expression(0);
consume(TokenType::ParenClose);
auto consequent = parse_block_statement();
// FIXME: Parse "else"
return create_ast_node<IfStatement>(move(predicate), move(consequent), nullptr);
}
NonnullRefPtr<ForStatement> Parser::parse_for_statement()
{
consume(TokenType::For);