mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 20:57:35 +00:00
LibJS/AST: Implement prefixed update expressions
This commit is contained in:
parent
20a0fc5576
commit
db64f364f0
2 changed files with 17 additions and 4 deletions
|
@ -450,15 +450,21 @@ Value UpdateExpression::execute(Interpreter& interpreter) const
|
|||
auto previous_value = interpreter.get_variable(name);
|
||||
ASSERT(previous_value.is_number());
|
||||
|
||||
int op_result = 0;
|
||||
switch (m_op) {
|
||||
case UpdateOp::Increment:
|
||||
interpreter.set_variable(name, Value(previous_value.as_double() + 1));
|
||||
op_result = 1;
|
||||
break;
|
||||
case UpdateOp::Decrement:
|
||||
interpreter.set_variable(name, Value(previous_value.as_double() - 1));
|
||||
op_result = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
interpreter.set_variable(name, Value(previous_value.as_double() + op_result));
|
||||
|
||||
if (m_prefixed)
|
||||
return JS::Value(previous_value.as_double() + op_result);
|
||||
|
||||
return previous_value;
|
||||
}
|
||||
|
||||
|
@ -504,8 +510,13 @@ void UpdateExpression::dump(int indent) const
|
|||
|
||||
ASTNode::dump(indent);
|
||||
print_indent(indent + 1);
|
||||
printf("%s\n", op_string);
|
||||
if (m_prefixed)
|
||||
printf("%s\n", op_string);
|
||||
m_argument->dump(indent + 1);
|
||||
if (!m_prefixed) {
|
||||
print_indent(indent + 1);
|
||||
printf("%s\n", op_string);
|
||||
}
|
||||
}
|
||||
|
||||
Value VariableDeclaration::execute(Interpreter& interpreter) const
|
||||
|
|
|
@ -451,9 +451,10 @@ enum class UpdateOp {
|
|||
|
||||
class UpdateExpression : public Expression {
|
||||
public:
|
||||
UpdateExpression(UpdateOp op, NonnullOwnPtr<Expression> argument)
|
||||
UpdateExpression(UpdateOp op, NonnullOwnPtr<Expression> argument, bool prefixed = false)
|
||||
: m_op(op)
|
||||
, m_argument(move(argument))
|
||||
, m_prefixed(prefixed)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -465,6 +466,7 @@ private:
|
|||
|
||||
UpdateOp m_op;
|
||||
NonnullOwnPtr<Identifier> m_argument;
|
||||
bool m_prefixed;
|
||||
};
|
||||
|
||||
enum class DeclarationType {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue