Beispiel #1
0
        public override Completion Evaluate(Interpreter interpreter)
        {
            var lhs = unaryExpression.Evaluate(interpreter);

            if (lhs.IsAbrupt())
            {
                return(lhs);
            }

            var lhsValue = lhs.GetValue();

            if (lhsValue.IsAbrupt())
            {
                return(lhsValue);
            }

            var oldValueComp = lhsValue.value !.ToNumber();

            if (oldValueComp.IsAbrupt())
            {
                return(oldValueComp);
            }
            var oldValue = oldValueComp.value as NumberValue;

            if (!(lhs.value is ReferenceValue reference))
            {
                throw new InvalidOperationException("PrefixUpdateExpression.Evaluate: unaryExpression did not return a reference");
            }

            NumberValue newValue;

            if (updateOperation == UpdateOperator.Decrement)
            {
                newValue = new NumberValue(oldValue !.number - 1);
            }
            else
            {
                newValue = new NumberValue(oldValue !.number + 1);
            }

            var putComp = reference.PutValue(newValue);

            if (putComp.IsAbrupt())
            {
                return(putComp);
            }

            return(Completion.NormalCompletion(newValue));
        }
        public override Completion Evaluate(Interpreter interpreter)
        {
            Completion comp, comp2;

            switch (unaryOperator)
            {
            case UnaryOperator.Delete:
                var deleteComp = EvaluateDelete(interpreter);
                if (deleteComp.IsAbrupt())
                {
                    return(deleteComp);
                }
                return(Completion.NormalCompletion(deleteComp.Other ? BooleanValue.True : BooleanValue.False));

            case UnaryOperator.Void:
                unaryExpression.Evaluate(interpreter).GetValue();
                return(Completion.NormalCompletion(UndefinedValue.Instance));

            case UnaryOperator.Typeof:
                return(EvaluateTypeof(interpreter));

            case UnaryOperator.Plus:
                comp = unaryExpression.Evaluate(interpreter).GetValue();
                if (comp.IsAbrupt())
                {
                    return(comp);
                }
                return(comp.value !.ToNumber());

            case UnaryOperator.Negate:
                comp = unaryExpression.Evaluate(interpreter).GetValue();
                if (comp.IsAbrupt())
                {
                    return(comp);
                }
                comp2 = comp.value !.ToNumber();
                if (comp2.IsAbrupt())
                {
                    return(comp2);
                }
                return(Completion.NormalCompletion(new NumberValue(-(comp2.value as NumberValue) !.number)));

            case UnaryOperator.BitwiseNot:
                comp = unaryExpression.Evaluate(interpreter).GetValue();
                if (comp.IsAbrupt())
                {
                    return(comp);
                }
                comp2 = comp.value !.ToNumber();
                if (comp2.IsAbrupt())
                {
                    return(comp2);
                }
                return(Completion.NormalCompletion(new NumberValue(~(int)(comp2.value as NumberValue) !.number)));

            case UnaryOperator.LogicalNot:
                comp = unaryExpression.Evaluate(interpreter).GetValue();
                if (comp.IsAbrupt())
                {
                    return(comp);
                }
                return(Completion.NormalCompletion(comp.value !.ToBoolean().boolean ? BooleanValue.False : BooleanValue.True));

            default:
                throw new InvalidOperationException($"OperatorUnaryExpression.Evaluate: Unknown UnaryOperator enum value {(int)unaryOperator}");
            }
        }