mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:57:35 +00:00
LibJS: Add typed comparison operator
The `===` operator allows us to compare two values while taking their type into consideration.
This commit is contained in:
parent
3fb0ff102c
commit
4e62dcd6e6
2 changed files with 43 additions and 0 deletions
|
@ -75,6 +75,29 @@ Value sub(Value lhs, Value rhs)
|
||||||
return Value(lhs.as_double() - rhs.as_double());
|
return Value(lhs.as_double() - rhs.as_double());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Value typed_eq(const Value lhs, const Value rhs)
|
||||||
|
{
|
||||||
|
if (rhs.type() != lhs.type())
|
||||||
|
return Value(false);
|
||||||
|
|
||||||
|
switch (lhs.type()) {
|
||||||
|
case Value::Type::Undefined:
|
||||||
|
return Value(true);
|
||||||
|
case Value::Type::Null:
|
||||||
|
return Value(true);
|
||||||
|
case Value::Type::Number:
|
||||||
|
return Value(lhs.as_double() == rhs.as_double());
|
||||||
|
case Value::Type::String:
|
||||||
|
return Value(lhs.as_string() == rhs.as_string());
|
||||||
|
case Value::Type::Boolean:
|
||||||
|
return Value(lhs.as_bool() == rhs.as_bool());
|
||||||
|
case Value::Type::Object:
|
||||||
|
return Value(lhs.as_object() == rhs.as_object());
|
||||||
|
}
|
||||||
|
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
Value BinaryExpression::execute(Interpreter& interpreter) const
|
Value BinaryExpression::execute(Interpreter& interpreter) const
|
||||||
{
|
{
|
||||||
auto lhs_result = m_lhs->execute(interpreter);
|
auto lhs_result = m_lhs->execute(interpreter);
|
||||||
|
@ -85,6 +108,12 @@ Value BinaryExpression::execute(Interpreter& interpreter) const
|
||||||
return add(lhs_result, rhs_result);
|
return add(lhs_result, rhs_result);
|
||||||
case BinaryOp::Minus:
|
case BinaryOp::Minus:
|
||||||
return sub(lhs_result, rhs_result);
|
return sub(lhs_result, rhs_result);
|
||||||
|
case BinaryOp::TypedEquals:
|
||||||
|
return typed_eq(lhs_result, rhs_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
|
@ -119,6 +148,19 @@ void BinaryExpression::dump(int indent) const
|
||||||
case BinaryOp::Minus:
|
case BinaryOp::Minus:
|
||||||
op_string = "-";
|
op_string = "-";
|
||||||
break;
|
break;
|
||||||
|
case BinaryOp::TypedEquals:
|
||||||
|
op_string = "===";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
print_indent(indent);
|
||||||
|
printf("%s\n", class_name());
|
||||||
|
m_lhs->dump(indent + 1);
|
||||||
|
print_indent(indent + 1);
|
||||||
|
printf("%s\n", op_string);
|
||||||
|
m_rhs->dump(indent + 1);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
print_indent(indent);
|
print_indent(indent);
|
||||||
|
|
|
@ -129,6 +129,7 @@ private:
|
||||||
enum class BinaryOp {
|
enum class BinaryOp {
|
||||||
Plus,
|
Plus,
|
||||||
Minus,
|
Minus,
|
||||||
|
TypedEquals,
|
||||||
};
|
};
|
||||||
|
|
||||||
class BinaryExpression : public Expression {
|
class BinaryExpression : public Expression {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue