LibJS: Implement <= and >= binary operators
This commit is contained in:
parent
fdf7f81ba9
commit
0fe87c5fec
Notes:
sideshowbarker
2024-07-19 08:45:08 +09:00
Author: https://github.com/deoxxa Commit: https://github.com/SerenityOS/serenity/commit/0fe87c5fec8 Pull-request: https://github.com/SerenityOS/serenity/pull/1425 Reviewed-by: https://github.com/sunverwerth
6 changed files with 38 additions and 0 deletions
|
@ -125,8 +125,12 @@ Value BinaryExpression::execute(Interpreter& interpreter) const
|
|||
return Value(!typed_eq(lhs_result, rhs_result).to_boolean());
|
||||
case BinaryOp::GreaterThan:
|
||||
return greater_than(lhs_result, rhs_result);
|
||||
case BinaryOp::GreaterThanEquals:
|
||||
return greater_than_equals(lhs_result, rhs_result);
|
||||
case BinaryOp::LessThan:
|
||||
return less_than(lhs_result, rhs_result);
|
||||
case BinaryOp::LessThanEquals:
|
||||
return less_than_equals(lhs_result, rhs_result);
|
||||
case BinaryOp::BitwiseAnd:
|
||||
return bitwise_and(lhs_result, rhs_result);
|
||||
case BinaryOp::BitwiseOr:
|
||||
|
@ -213,9 +217,15 @@ void BinaryExpression::dump(int indent) const
|
|||
case BinaryOp::GreaterThan:
|
||||
op_string = ">";
|
||||
break;
|
||||
case BinaryOp::GreaterThanEquals:
|
||||
op_string = ">=";
|
||||
break;
|
||||
case BinaryOp::LessThan:
|
||||
op_string = "<";
|
||||
break;
|
||||
case BinaryOp::LessThanEquals:
|
||||
op_string = "<=";
|
||||
break;
|
||||
case BinaryOp::BitwiseAnd:
|
||||
op_string = "&";
|
||||
break;
|
||||
|
|
|
@ -219,7 +219,9 @@ enum class BinaryOp {
|
|||
TypedEquals,
|
||||
TypedInequals,
|
||||
GreaterThan,
|
||||
GreaterThanEquals,
|
||||
LessThan,
|
||||
LessThanEquals,
|
||||
BitwiseAnd,
|
||||
BitwiseOr,
|
||||
BitwiseXor,
|
||||
|
|
|
@ -133,6 +133,12 @@ NonnullOwnPtr<Expression> Parser::parse_secondary_expression(NonnullOwnPtr<Expre
|
|||
case TokenType::Slash:
|
||||
consume();
|
||||
return make<BinaryExpression>(BinaryOp::Slash, move(lhs), parse_expression());
|
||||
case TokenType::GreaterThanEquals:
|
||||
consume();
|
||||
return make<BinaryExpression>(BinaryOp::GreaterThanEquals, move(lhs), parse_expression());
|
||||
case TokenType::LessThanEquals:
|
||||
consume();
|
||||
return make<BinaryExpression>(BinaryOp::LessThanEquals, move(lhs), parse_expression());
|
||||
case TokenType::ParenOpen:
|
||||
return parse_call_expression(move(lhs));
|
||||
case TokenType::Equals:
|
||||
|
@ -248,6 +254,8 @@ bool Parser::match_secondary_expression() const
|
|||
|| type == TokenType::Asterisk
|
||||
|| type == TokenType::Slash
|
||||
|| type == TokenType::Equals
|
||||
|| type == TokenType::GreaterThanEquals
|
||||
|| type == TokenType::LessThanEquals
|
||||
|| type == TokenType::ParenOpen
|
||||
|| type == TokenType::Period;
|
||||
}
|
||||
|
|
|
@ -58,11 +58,13 @@ enum class TokenType {
|
|||
Finally,
|
||||
Function,
|
||||
GreaterThan,
|
||||
GreaterThanEquals,
|
||||
Identifier,
|
||||
If,
|
||||
Interface,
|
||||
Invalid,
|
||||
LessThan,
|
||||
LessThanEquals,
|
||||
Let,
|
||||
Minus,
|
||||
MinusEquals,
|
||||
|
|
|
@ -96,6 +96,13 @@ Value greater_than(Value lhs, Value rhs)
|
|||
return Value(lhs.as_double() > rhs.as_double());
|
||||
}
|
||||
|
||||
Value greater_than_equals(Value lhs, Value rhs)
|
||||
{
|
||||
ASSERT(lhs.is_number());
|
||||
ASSERT(rhs.is_number());
|
||||
return Value(lhs.as_double() >= rhs.as_double());
|
||||
}
|
||||
|
||||
Value less_than(Value lhs, Value rhs)
|
||||
{
|
||||
ASSERT(lhs.is_number());
|
||||
|
@ -103,6 +110,13 @@ Value less_than(Value lhs, Value rhs)
|
|||
return Value(lhs.as_double() < rhs.as_double());
|
||||
}
|
||||
|
||||
Value less_than_equals(Value lhs, Value rhs)
|
||||
{
|
||||
ASSERT(lhs.is_number());
|
||||
ASSERT(rhs.is_number());
|
||||
return Value(lhs.as_double() <= rhs.as_double());
|
||||
}
|
||||
|
||||
Value bitwise_and(Value lhs, Value rhs)
|
||||
{
|
||||
ASSERT(lhs.is_number());
|
||||
|
|
|
@ -159,7 +159,9 @@ inline Value js_null()
|
|||
}
|
||||
|
||||
Value greater_than(Value lhs, Value rhs);
|
||||
Value greater_than_equals(Value lhs, Value rhs);
|
||||
Value less_than(Value lhs, Value rhs);
|
||||
Value less_than_equals(Value lhs, Value rhs);
|
||||
Value bitwise_and(Value lhs, Value rhs);
|
||||
Value bitwise_or(Value lhs, Value rhs);
|
||||
Value bitwise_xor(Value lhs, Value rhs);
|
||||
|
|
Loading…
Add table
Reference in a new issue