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

LibJS: Implement ConditionalExpression (ternary "?:" operator)

This commit is contained in:
Andreas Kling 2020-04-03 12:14:28 +02:00
parent 522d8c5d71
commit 0622181d1f
5 changed files with 83 additions and 1 deletions

View file

@ -970,4 +970,34 @@ void SwitchCase::dump(int indent) const
}
}
Value ConditionalExpression::execute(Interpreter& interpreter) const
{
auto test_result = m_test->execute(interpreter);
if (interpreter.exception())
return {};
Value result;
if (test_result.to_boolean()) {
result = m_consequent->execute(interpreter);
} else {
result = m_alternate->execute(interpreter);
}
if (interpreter.exception())
return {};
return result;
}
void ConditionalExpression::dump(int indent) const
{
ASTNode::dump(indent);
print_indent(indent);
printf("(Test)\n");
m_test->dump(indent + 1);
print_indent(indent);
printf("(Consequent)\n");
m_test->dump(indent + 1);
print_indent(indent);
printf("(Alternate)\n");
m_test->dump(indent + 1);
}
}