1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:08:10 +00:00

LibJS: Implement "else" parsing

We can now handle scripts with if/else in LibJS. Most of the changes
are about fixing IfStatement to store the consequent and alternate node
as Statements.

Interpreter now also runs Statements, rather than running ScopeNodes.
This commit is contained in:
Andreas Kling 2020-03-23 16:46:41 +01:00
parent b2f005125d
commit fbb9e1b715
5 changed files with 31 additions and 16 deletions

View file

@ -197,8 +197,12 @@ NonnullRefPtr<Statement> Parser::parse_statement()
case TokenType::If:
return parse_if_statement();
default:
if (match_expression())
return adopt(*new ExpressionStatement(parse_expression(0)));
if (match_expression()) {
auto statement = adopt(*new ExpressionStatement(parse_expression(0)));
if (match(TokenType::Semicolon))
consume();
return statement;
}
m_has_errors = true;
expected("statement (missing switch case)");
consume();
@ -520,9 +524,13 @@ NonnullRefPtr<IfStatement> Parser::parse_if_statement()
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);
auto consequent = parse_statement();
RefPtr<Statement> alternate;
if (match(TokenType::Else)) {
consume(TokenType::Else);
alternate = parse_statement();
}
return create_ast_node<IfStatement>(move(predicate), move(consequent), move(alternate));
}
NonnullRefPtr<ForStatement> Parser::parse_for_statement()