LibJS/AST: Implement prefixed update expressions

This commit is contained in:
0xtechnobabble 2020-03-14 20:44:57 +02:00 committed by Andreas Kling
parent 20a0fc5576
commit db64f364f0
2 changed files with 17 additions and 4 deletions

View file

@ -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

View file

@ -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 {