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

LibJS: Support VariableDeclaration with multiple declarators

This patch adds support in the parser and interpreter for this:

    var a = 1, b = 2, c = a + b;

VariableDeclaration is now a sequence of VariableDeclarators. :^)
This commit is contained in:
Andreas Kling 2020-04-04 21:46:25 +02:00
parent 9691286cf0
commit 5e40aa182b
4 changed files with 76 additions and 22 deletions

View file

@ -654,13 +654,23 @@ NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration()
default:
ASSERT_NOT_REACHED();
}
auto name = consume(TokenType::Identifier).value();
RefPtr<Expression> initializer;
if (match(TokenType::Equals)) {
consume();
initializer = parse_expression(0);
NonnullRefPtrVector<VariableDeclarator> declarations;
for (;;) {
auto id = consume(TokenType::Identifier).value();
RefPtr<Expression> init;
if (match(TokenType::Equals)) {
consume();
init = parse_expression(0);
}
declarations.append(create_ast_node<VariableDeclarator>(create_ast_node<Identifier>(move(id)), move(init)));
if (match(TokenType::Comma)) {
consume();
continue;
}
break;
}
return create_ast_node<VariableDeclaration>(create_ast_node<Identifier>(name), move(initializer), declaration_type);
return create_ast_node<VariableDeclaration>(declaration_type, move(declarations));
}
NonnullRefPtr<ThrowStatement> Parser::parse_throw_statement()