Esempio n. 1
0
        public override object Evaluate(NodeEvaluationContext evaluationContext)
        {
            var value = Node.Evaluate(evaluationContext);

            if (value == DependencyProperty.UnsetValue)
            {
                return(DependencyProperty.UnsetValue);
            }

            var nodeValueType = GetNodeValueType(value);

            exceptionHelper.ResolveAndThrowIf(!Node.IsIntegralNodeValueType(nodeValueType), "NotIntegralType", nodeValueType);

            switch (nodeValueType)
            {
            case NodeValueType.Byte:
                return(~((byte)value));

            case NodeValueType.Int16:
                return(~((short)value));

            case NodeValueType.Int32:
                return(~((int)value));

            case NodeValueType.Int64:
                return(~((long)value));
            }

            Debug.Assert(false);
            return(null);
        }
        public sealed override object Evaluate(NodeEvaluationContext evaluationContext)
        {
            var leftNodeValue = LeftNode.Evaluate(evaluationContext);

            if (leftNodeValue == DependencyProperty.UnsetValue)
            {
                return(DependencyProperty.UnsetValue);
            }

            var leftNodeValueType = GetNodeValueType(leftNodeValue);

            if (leftNodeValueType == NodeValueType.Boolean)
            {
                // give base a chance to yield a result without evaluating the right node
                var result = this.DetermineResultPreRightEvaluation((bool)leftNodeValue);

                if (result.HasValue)
                {
                    return(result.Value);
                }
            }

            var rightNodeValue = RightNode.Evaluate(evaluationContext);

            if (rightNodeValue == DependencyProperty.UnsetValue)
            {
                return(DependencyProperty.UnsetValue);
            }

            var rightNodeValueType = GetNodeValueType(rightNodeValue);

            exceptionHelper.ResolveAndThrowIf(leftNodeValueType != NodeValueType.Boolean || rightNodeValueType != NodeValueType.Boolean, "OperandsNotBoolean", this.OperatorSymbols, leftNodeValueType, rightNodeValueType);

            return(this.DetermineResultPostRightEvaluation((bool)leftNodeValue, (bool)rightNodeValue));
        }
Esempio n. 3
0
        public override object Evaluate(NodeEvaluationContext evaluationContext)
        {
            Debug.Assert(evaluationContext != null);
            exceptionHelper.ResolveAndThrowIf(!evaluationContext.HasArgument(this.index), "ArgumentNotFound", this.index);

            // variable values are passed inside the context for each evaluation
            return(evaluationContext.GetArgument(this.index));
        }
Esempio n. 4
0
        public override object Evaluate(NodeEvaluationContext evaluationContext)
        {
            var value = Node.Evaluate(evaluationContext);

            if (value == DependencyProperty.UnsetValue)
            {
                return(DependencyProperty.UnsetValue);
            }

            var nodeValueType = GetNodeValueType(value);

            exceptionHelper.ResolveAndThrowIf(nodeValueType != NodeValueType.Boolean, "NotBooleanType", nodeValueType);
            return(!((bool)value));
        }
Esempio n. 5
0
        public override object Evaluate(NodeEvaluationContext evaluationContext)
        {
            var value = Node.Evaluate(evaluationContext);

            if (value == DependencyProperty.UnsetValue)
            {
                return(DependencyProperty.UnsetValue);
            }

            var nodeValueType = GetNodeValueType(value);
            var canCast       = (IsNumericalNodeValueType(nodeValueType) && IsNumericalNodeValueType(this.targetType)) ||
                                (nodeValueType == NodeValueType.Boolean && this.targetType == NodeValueType.Boolean) ||
                                (nodeValueType == NodeValueType.String && this.targetType == NodeValueType.String);

            exceptionHelper.ResolveAndThrowIf(!canCast, "CannotCast", nodeValueType, this.targetType);

            switch (nodeValueType)
            {
            case NodeValueType.Boolean:
                return(this.Cast((bool)value));

            case NodeValueType.String:
                return(this.Cast(value as string));

            case NodeValueType.Byte:
                return(this.Cast((byte)value));

            case NodeValueType.Int16:
                return(this.Cast((short)value));

            case NodeValueType.Int32:
                return(this.Cast((int)value));

            case NodeValueType.Int64:
                return(this.Cast((long)value));

            case NodeValueType.Single:
                return(this.Cast((float)value));

            case NodeValueType.Double:
                return(this.Cast((double)value));

            case NodeValueType.Decimal:
                return(this.Cast((decimal)value));
            }

            Debug.Assert(false);
            return(null);
        }
Esempio n. 6
0
        public override object Evaluate(NodeEvaluationContext evaluationContext)
        {
            var leftNodeValue = LeftNode.Evaluate(evaluationContext);

            if (leftNodeValue == DependencyProperty.UnsetValue)
            {
                return(DependencyProperty.UnsetValue);
            }

            var rightNodeValue = RightNode.Evaluate(evaluationContext);

            if (rightNodeValue == DependencyProperty.UnsetValue)
            {
                return(DependencyProperty.UnsetValue);
            }

            var leftNodeValueType  = GetNodeValueType(leftNodeValue);
            var rightNodeValueType = GetNodeValueType(rightNodeValue);

            // right operand must always be Int32
            exceptionHelper.ResolveAndThrowIf(!Node.IsNumericalNodeValueType(leftNodeValueType) || rightNodeValueType != NodeValueType.Int32, "NodeValuesNotSupportedTypes", this.OperatorSymbols, leftNodeValueType, rightNodeValueType);

            switch (leftNodeValueType)
            {
            case NodeValueType.Byte:
                return(this.DoByte((byte)leftNodeValue, (int)rightNodeValue));

            case NodeValueType.Int16:
                return(this.DoInt16((short)leftNodeValue, (int)rightNodeValue));

            case NodeValueType.Int32:
                return(this.DoInt32((int)leftNodeValue, (int)rightNodeValue));

            case NodeValueType.Int64:
                return(this.DoInt64((long)leftNodeValue, (int)rightNodeValue));
            }

            Debug.Assert(false);
            return(null);
        }
        public override object Evaluate(NodeEvaluationContext evaluationContext)
        {
            var firstNodeValue = this.FirstNode.Evaluate(evaluationContext);

            if (firstNodeValue == DependencyProperty.UnsetValue)
            {
                return(DependencyProperty.UnsetValue);
            }

            var firstNodeValueType = GetNodeValueType(firstNodeValue);

            exceptionHelper.ResolveAndThrowIf(firstNodeValueType != NodeValueType.Boolean, "FirstNodeMustBeBoolean", this.OperatorSymbols, firstNodeValueType);

            if ((bool)firstNodeValue)
            {
                return(this.SecondNode.Evaluate(evaluationContext));
            }
            else
            {
                return(this.ThirdNode.Evaluate(evaluationContext));
            }
        }
Esempio n. 8
0
        public override object Evaluate(NodeEvaluationContext evaluationContext)
        {
            var value = Node.Evaluate(evaluationContext);

            if (value == DependencyProperty.UnsetValue)
            {
                return(DependencyProperty.UnsetValue);
            }

            var nodeValueType = GetNodeValueType(value);

            switch (nodeValueType)
            {
            case NodeValueType.Byte:
                return(-1 * (byte)value);

            case NodeValueType.Int16:
                return(-1 * (short)value);

            case NodeValueType.Int32:
                return(-1 * (int)value);

            case NodeValueType.Int64:
                return(-1 * (long)value);

            case NodeValueType.Single:
                return(-1 * (float)value);

            case NodeValueType.Double:
                return(-1 * (double)value);

            case NodeValueType.Decimal:
                return(-1 * (decimal)value);
            }

            throw exceptionHelper.Resolve("CannotNegateValue", nodeValueType);
        }
Esempio n. 9
0
 public abstract object Evaluate(NodeEvaluationContext evaluationContext);
        public sealed override object Evaluate(NodeEvaluationContext evaluationContext)
        {
            var leftNodeValue  = LeftNode.Evaluate(evaluationContext);
            var rightNodeValue = RightNode.Evaluate(evaluationContext);

            if (leftNodeValue == DependencyProperty.UnsetValue || rightNodeValue == DependencyProperty.UnsetValue)
            {
                return(DependencyProperty.UnsetValue);
            }

            var leftNodeValueType  = GetNodeValueType(leftNodeValue);
            var rightNodeValueType = GetNodeValueType(rightNodeValue);

            // determine the type to which we will need to widen any narrower value
            var maxNodeValueType = (NodeValueType)Math.Max((int)leftNodeValueType, (int)rightNodeValueType);

            // we have to convert rather than just cast because the value is boxed
            var convertibleLeftNodeValue  = leftNodeValue as IConvertible;
            var convertibleRightNodeValue = rightNodeValue as IConvertible;

            Debug.Assert(convertibleLeftNodeValue != null || (maxNodeValueType == NodeValueType.String || maxNodeValueType == NodeValueType.ReferenceType));
            Debug.Assert(convertibleRightNodeValue != null || (maxNodeValueType == NodeValueType.String || maxNodeValueType == NodeValueType.ReferenceType));

            var    succeeded = false;
            object result    = null;

            switch (maxNodeValueType)
            {
            case NodeValueType.String:
                succeeded = this.DoString(convertibleLeftNodeValue == null ? null : convertibleLeftNodeValue.ToString(null), convertibleRightNodeValue == null ? null : convertibleRightNodeValue.ToString(null), out result);
                break;

            case NodeValueType.Boolean:
                succeeded = this.DoBoolean(convertibleLeftNodeValue.ToBoolean(null), convertibleRightNodeValue.ToBoolean(null), out result);
                break;

            case NodeValueType.Byte:
                succeeded = this.DoByte(convertibleLeftNodeValue.ToByte(null), convertibleRightNodeValue.ToByte(null), out result);
                break;

            case NodeValueType.Int16:
                succeeded = this.DoInt16(convertibleLeftNodeValue.ToInt16(null), convertibleRightNodeValue.ToInt16(null), out result);
                break;

            case NodeValueType.Int32:
                succeeded = this.DoInt32(convertibleLeftNodeValue.ToInt32(null), convertibleRightNodeValue.ToInt32(null), out result);
                break;

            case NodeValueType.Int64:
                succeeded = this.DoInt64(convertibleLeftNodeValue.ToInt64(null), convertibleRightNodeValue.ToInt64(null), out result);
                break;

            case NodeValueType.Single:
                succeeded = this.DoSingle(convertibleLeftNodeValue.ToSingle(null), convertibleRightNodeValue.ToSingle(null), out result);
                break;

            case NodeValueType.Double:
                succeeded = this.DoDouble(convertibleLeftNodeValue.ToDouble(null), convertibleRightNodeValue.ToDouble(null), out result);
                break;

            case NodeValueType.Decimal:
                succeeded = this.DoDecimal(convertibleLeftNodeValue.ToDecimal(null), convertibleRightNodeValue.ToDecimal(null), out result);
                break;

            case NodeValueType.ValueType:
                succeeded = this.DoValueType(leftNodeValue, rightNodeValue, out result);
                break;

            case NodeValueType.ReferenceType:
                succeeded = this.DoReferenceType(leftNodeValue, rightNodeValue, out result);
                break;
            }

            exceptionHelper.ResolveAndThrowIf(!succeeded, "OperatorNotSupportedWithOperands", this.OperatorSymbols, leftNodeValueType, rightNodeValueType);
            return(result);
        }