mirror of
https://github.com/RGBCube/serenity
synced 2025-05-18 19:05:06 +00:00
LibJS: Implement logical expressions
Logical expressions are expressions which can return either true or false according to a provided condition.
This commit is contained in:
parent
4e62dcd6e6
commit
a96bf2c22e
2 changed files with 68 additions and 0 deletions
|
@ -114,6 +114,22 @@ Value BinaryExpression::execute(Interpreter& interpreter) const
|
|||
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
Value LogicalExpression::execute(Interpreter& interpreter) const
|
||||
{
|
||||
auto lhs_result = m_lhs->execute(interpreter).as_bool();
|
||||
|
||||
if (m_op == LogicalOp::Not)
|
||||
return Value(!lhs_result);
|
||||
|
||||
auto rhs_result = m_rhs->execute(interpreter).as_bool();
|
||||
switch (m_op) {
|
||||
case LogicalOp::And:
|
||||
return Value(lhs_result && rhs_result);
|
||||
case LogicalOp::Or:
|
||||
return Value(lhs_result || rhs_result);
|
||||
case LogicalOp::Not:
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
ASSERT_NOT_REACHED();
|
||||
|
@ -161,6 +177,24 @@ void BinaryExpression::dump(int indent) const
|
|||
m_rhs->dump(indent + 1);
|
||||
}
|
||||
|
||||
void LogicalExpression::dump(int indent) const
|
||||
{
|
||||
const char* op_string = nullptr;
|
||||
switch (m_op) {
|
||||
case LogicalOp::And:
|
||||
op_string = "&&";
|
||||
break;
|
||||
case LogicalOp::Or:
|
||||
op_string = "||";
|
||||
break;
|
||||
case LogicalOp::Not:
|
||||
op_string = "!";
|
||||
print_indent(indent);
|
||||
printf("%s\n", class_name());
|
||||
print_indent(indent + 1);
|
||||
printf("%s\n", op_string);
|
||||
m_lhs->dump(indent + 1);
|
||||
return;
|
||||
}
|
||||
|
||||
print_indent(indent);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue