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

LibJS: Implement for statement

This commit is contained in:
Conrad Pankoff 2020-03-12 23:12:12 +11:00 committed by Andreas Kling
parent e88f2f15ee
commit 097e1af4e8
6 changed files with 119 additions and 0 deletions

View file

@ -105,6 +105,30 @@ Value WhileStatement::execute(Interpreter& interpreter) const
return last_value;
}
Value ForStatement::execute(Interpreter& interpreter) const
{
Value last_value = js_undefined();
if (m_init)
m_init->execute(interpreter);
if (m_test) {
while (m_test->execute(interpreter).to_boolean()) {
last_value = interpreter.run(*m_body);
if (m_update)
m_update->execute(interpreter);
}
} else {
while (true) {
last_value = interpreter.run(*m_body);
if (m_update)
m_update->execute(interpreter);
}
}
return last_value;
}
Value BinaryExpression::execute(Interpreter& interpreter) const
{
auto lhs_result = m_lhs->execute(interpreter);
@ -362,6 +386,21 @@ void WhileStatement::dump(int indent) const
body().dump(indent + 1);
}
void ForStatement::dump(int indent) const
{
ASTNode::dump(indent);
print_indent(indent);
printf("For\n");
if (init())
init()->dump(indent + 1);
if (test())
test()->dump(indent + 1);
if (update())
update()->dump(indent + 1);
body().dump(indent + 1);
}
Value Identifier::execute(Interpreter& interpreter) const
{
return interpreter.get_variable(string());