1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 07:55:07 +00:00

LibJS: Implement multiplication and division operators

This commit is contained in:
Conrad Pankoff 2020-03-12 23:04:52 +11:00 committed by Andreas Kling
parent 9f3f0d9983
commit fdf7f81ba9
5 changed files with 34 additions and 1 deletions

View file

@ -115,6 +115,10 @@ 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::Asterisk:
return mul(lhs_result, rhs_result);
case BinaryOp::Slash:
return div(lhs_result, rhs_result);
case BinaryOp::TypedEquals: case BinaryOp::TypedEquals:
return typed_eq(lhs_result, rhs_result); return typed_eq(lhs_result, rhs_result);
case BinaryOp::TypedInequals: case BinaryOp::TypedInequals:
@ -194,6 +198,12 @@ void BinaryExpression::dump(int indent) const
case BinaryOp::Minus: case BinaryOp::Minus:
op_string = "-"; op_string = "-";
break; break;
case BinaryOp::Asterisk:
op_string = "*";
break;
case BinaryOp::Slash:
op_string = "/";
break;
case BinaryOp::TypedEquals: case BinaryOp::TypedEquals:
op_string = "==="; op_string = "===";
break; break;

View file

@ -214,6 +214,8 @@ private:
enum class BinaryOp { enum class BinaryOp {
Plus, Plus,
Minus, Minus,
Asterisk,
Slash,
TypedEquals, TypedEquals,
TypedInequals, TypedInequals,
GreaterThan, GreaterThan,

View file

@ -127,6 +127,12 @@ NonnullOwnPtr<Expression> Parser::parse_secondary_expression(NonnullOwnPtr<Expre
case TokenType::Minus: case TokenType::Minus:
consume(); consume();
return make<BinaryExpression>(BinaryOp::Minus, move(lhs), parse_expression()); return make<BinaryExpression>(BinaryOp::Minus, move(lhs), parse_expression());
case TokenType::Asterisk:
consume();
return make<BinaryExpression>(BinaryOp::Asterisk, move(lhs), parse_expression());
case TokenType::Slash:
consume();
return make<BinaryExpression>(BinaryOp::Slash, move(lhs), parse_expression());
case TokenType::ParenOpen: case TokenType::ParenOpen:
return parse_call_expression(move(lhs)); return parse_call_expression(move(lhs));
case TokenType::Equals: case TokenType::Equals:

View file

@ -145,7 +145,6 @@ Value right_shift(Value lhs, Value rhs)
} }
Value add(Value lhs, Value rhs) Value add(Value lhs, Value rhs)
{ {
ASSERT(lhs.is_number()); ASSERT(lhs.is_number());
ASSERT(rhs.is_number()); ASSERT(rhs.is_number());
@ -159,6 +158,20 @@ Value sub(Value lhs, Value rhs)
return Value(lhs.as_double() - rhs.as_double()); return Value(lhs.as_double() - rhs.as_double());
} }
Value mul(Value lhs, Value rhs)
{
ASSERT(lhs.is_number());
ASSERT(rhs.is_number());
return Value(lhs.as_double() * rhs.as_double());
}
Value div(Value lhs, Value rhs)
{
ASSERT(lhs.is_number());
ASSERT(rhs.is_number());
return Value(lhs.as_double() / rhs.as_double());
}
Value typed_eq(Value lhs, Value rhs) Value typed_eq(Value lhs, Value rhs)
{ {
if (rhs.type() != lhs.type()) if (rhs.type() != lhs.type())

View file

@ -168,6 +168,8 @@ Value left_shift(Value lhs, Value rhs);
Value right_shift(Value lhs, Value rhs); Value right_shift(Value lhs, Value rhs);
Value add(Value lhs, Value rhs); Value add(Value lhs, Value rhs);
Value sub(Value lhs, Value rhs); Value sub(Value lhs, Value rhs);
Value mul(Value lhs, Value rhs);
Value div(Value lhs, Value rhs);
Value typed_eq(Value lhs, Value rhs); Value typed_eq(Value lhs, Value rhs);
const LogStream& operator<<(const LogStream&, const Value&); const LogStream& operator<<(const LogStream&, const Value&);