mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 04:17:35 +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:
parent
9691286cf0
commit
5e40aa182b
4 changed files with 76 additions and 22 deletions
|
@ -747,17 +747,24 @@ void UpdateExpression::dump(int indent) const
|
|||
|
||||
Value VariableDeclaration::execute(Interpreter& interpreter) const
|
||||
{
|
||||
interpreter.declare_variable(name().string(), m_declaration_type);
|
||||
if (m_initializer) {
|
||||
auto initalizer_result = m_initializer->execute(interpreter);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
interpreter.set_variable(name().string(), initalizer_result, true);
|
||||
for (auto& declarator : m_declarations) {
|
||||
interpreter.declare_variable(declarator.id().string(), m_declaration_type);
|
||||
if (auto* init = declarator.init()) {
|
||||
auto initalizer_result = init->execute(interpreter);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
interpreter.set_variable(declarator.id().string(), initalizer_result, true);
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
Value VariableDeclarator::execute(Interpreter &) const
|
||||
{
|
||||
// NOTE: This node is handled by VariableDeclaration.
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
void VariableDeclaration::dump(int indent) const
|
||||
{
|
||||
const char* declaration_type_string = nullptr;
|
||||
|
@ -776,9 +783,17 @@ void VariableDeclaration::dump(int indent) const
|
|||
ASTNode::dump(indent);
|
||||
print_indent(indent + 1);
|
||||
printf("%s\n", declaration_type_string);
|
||||
m_name->dump(indent + 1);
|
||||
if (m_initializer)
|
||||
m_initializer->dump(indent + 1);
|
||||
|
||||
for (auto& declarator : m_declarations)
|
||||
declarator.dump(indent + 1);
|
||||
}
|
||||
|
||||
void VariableDeclarator::dump(int indent) const
|
||||
{
|
||||
ASTNode::dump(indent);
|
||||
m_id->dump(indent + 1);
|
||||
if (m_init)
|
||||
m_init->dump(indent + 1);
|
||||
}
|
||||
|
||||
void ObjectExpression::dump(int indent) const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue