1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 21:57:35 +00:00

LibJS: Implement modulo assignment operator (%=)

This commit is contained in:
Linus Groh 2020-05-04 23:07:05 +01:00 committed by Andreas Kling
parent a2e1f1a872
commit 72d2bd56ce
4 changed files with 18 additions and 0 deletions

View file

@ -850,6 +850,12 @@ Value AssignmentExpression::execute(Interpreter& interpreter) const
return {}; return {};
rhs_result = div(interpreter, lhs_result, rhs_result); rhs_result = div(interpreter, lhs_result, rhs_result);
break; break;
case AssignmentOp::ModuloAssignment:
lhs_result = m_lhs->execute(interpreter);
if (interpreter.exception())
return {};
rhs_result = mod(interpreter, lhs_result, rhs_result);
break;
case AssignmentOp::ExponentiationAssignment: case AssignmentOp::ExponentiationAssignment:
lhs_result = m_lhs->execute(interpreter); lhs_result = m_lhs->execute(interpreter);
if (interpreter.exception()) if (interpreter.exception())
@ -960,6 +966,9 @@ void AssignmentExpression::dump(int indent) const
case AssignmentOp::DivisionAssignment: case AssignmentOp::DivisionAssignment:
op_string = "/="; op_string = "/=";
break; break;
case AssignmentOp::ModuloAssignment:
op_string = "%=";
break;
case AssignmentOp::ExponentiationAssignment: case AssignmentOp::ExponentiationAssignment:
op_string = "**="; op_string = "**=";
break; break;

View file

@ -604,6 +604,7 @@ enum class AssignmentOp {
SubtractionAssignment, SubtractionAssignment,
MultiplicationAssignment, MultiplicationAssignment,
DivisionAssignment, DivisionAssignment,
ModuloAssignment,
ExponentiationAssignment, ExponentiationAssignment,
BitwiseAndAssignment, BitwiseAndAssignment,
BitwiseOrAssignment, BitwiseOrAssignment,

View file

@ -634,6 +634,9 @@ NonnullRefPtr<Expression> Parser::parse_secondary_expression(NonnullRefPtr<Expre
case TokenType::Percent: case TokenType::Percent:
consume(); consume();
return create_ast_node<BinaryExpression>(BinaryOp::Modulo, move(lhs), parse_expression(min_precedence, associativity)); return create_ast_node<BinaryExpression>(BinaryOp::Modulo, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::PercentEquals:
consume();
return create_ast_node<AssignmentExpression>(AssignmentOp::ModuloAssignment, move(lhs), parse_expression(min_precedence, associativity));
case TokenType::DoubleAsterisk: case TokenType::DoubleAsterisk:
consume(); consume();
return create_ast_node<BinaryExpression>(BinaryOp::Exponentiation, move(lhs), parse_expression(min_precedence, associativity)); return create_ast_node<BinaryExpression>(BinaryOp::Exponentiation, move(lhs), parse_expression(min_precedence, associativity));
@ -1194,6 +1197,7 @@ bool Parser::match_secondary_expression() const
|| type == TokenType::Slash || type == TokenType::Slash
|| type == TokenType::SlashEquals || type == TokenType::SlashEquals
|| type == TokenType::Percent || type == TokenType::Percent
|| type == TokenType::PercentEquals
|| type == TokenType::DoubleAsterisk || type == TokenType::DoubleAsterisk
|| type == TokenType::DoubleAsteriskEquals || type == TokenType::DoubleAsteriskEquals
|| type == TokenType::Equals || type == TokenType::Equals

View file

@ -23,6 +23,10 @@ try {
assert((x /= 2) === 3); assert((x /= 2) === 3);
assert(x === 3); assert(x === 3);
x = 6;
assert((x %= 4) === 2);
assert(x === 2);
x = 2; x = 2;
assert((x **= 3) === 8); assert((x **= 3) === 8);
assert(x === 8); assert(x === 8);