public override Expression Reduce()
        {
            NRefactory.UnaryOperatorType @operator = _unaryOperatorExpression.Operator;
            ExpressionType unaryType = GetUnaryOperator(@operator);

            return(Expression.MakeUnary(unaryType, Operand, Type));
        }
        private ExpressionType GetUnaryOperator(NRefactory.UnaryOperatorType @operator)
        {
            ExpressionType type;

            if (!Enum.TryParse <ExpressionType>(@operator.ToString(), true, out type))
            {
                switch (@operator)
                {
                case ICSharpCode.NRefactory.CSharp.UnaryOperatorType.BitNot:

                    return(ExpressionType.Not);

                case ICSharpCode.NRefactory.CSharp.UnaryOperatorType.Decrement:

                    return(ExpressionType.Decrement);

                case ICSharpCode.NRefactory.CSharp.UnaryOperatorType.Increment:

                    return(ExpressionType.Increment);

                case ICSharpCode.NRefactory.CSharp.UnaryOperatorType.Minus:

                    return(ExpressionType.Negate);

                case ICSharpCode.NRefactory.CSharp.UnaryOperatorType.Not:

                    return(ExpressionType.NotEqual);

                case ICSharpCode.NRefactory.CSharp.UnaryOperatorType.Plus:

                    return(ExpressionType.Add);

                case ICSharpCode.NRefactory.CSharp.UnaryOperatorType.PostDecrement:

                    return(ExpressionType.PostDecrementAssign);

                case ICSharpCode.NRefactory.CSharp.UnaryOperatorType.PostIncrement:

                    return(ExpressionType.PostIncrementAssign);

                default:

                    throw new NotSupportedException();
                }
            }

            return(type);
        }
Example #3
0
        internal object ApplyUnaryOperation(ICSharpCode.NRefactory.CSharp.UnaryOperatorType unaryOperatorType, object originalValue, ExpressionParseType referenceOrValue)
        {
            object valueToModify = originalValue;

            if (originalValue is IAssignableReference)
            {
                valueToModify = ((IAssignableReference)originalValue).CurrentValue;
            }
            object toReturn = null;


            if (valueToModify == null)
            {
                toReturn = null;
            }

            else if (valueToModify is float)
            {
                if (unaryOperatorType == UnaryOperatorType.Minus)
                {
                    toReturn = (float)valueToModify * -1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostIncrement)
                {
                    toReturn = (float)valueToModify + 1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostDecrement)
                {
                    toReturn = (float)valueToModify - 1;
                }
            }
            else if (valueToModify is int)
            {
                if (unaryOperatorType == UnaryOperatorType.Minus)
                {
                    toReturn = (int)valueToModify * -1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostIncrement)
                {
                    toReturn = (int)valueToModify + 1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostDecrement)
                {
                    toReturn = (int)valueToModify - 1;
                }
            }
            else if (valueToModify is long)
            {
                if (unaryOperatorType == UnaryOperatorType.Minus)
                {
                    toReturn = (long)valueToModify * -1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostIncrement)
                {
                    toReturn = (long)valueToModify + 1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostDecrement)
                {
                    toReturn = (long)valueToModify - 1;
                }
            }
            else if (valueToModify is double)
            {
                if (unaryOperatorType == ICSharpCode.NRefactory.CSharp.UnaryOperatorType.Minus)
                {
                    toReturn = (double)valueToModify * -1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostIncrement)
                {
                    toReturn = (double)valueToModify + 1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostDecrement)
                {
                    toReturn = (double)valueToModify - 1;
                }
            }
            else if (valueToModify is string)
            {
            }
            else if (valueToModify is bool)
            {
                if (unaryOperatorType == ICSharpCode.NRefactory.CSharp.UnaryOperatorType.Not)
                {
                    toReturn = !((bool)valueToModify);
                }
            }

            if (toReturn == null)
            {
                throw new NotImplementedException();
            }
            else
            {
                // If the operator is one that modifies the object (like ++), we want to apply it back
                if (referenceOrValue == ExpressionParseType.GetReference && originalValue is IAssignableReference)
                {
                    ((IAssignableReference)originalValue).CurrentValue = toReturn;
                    toReturn = originalValue;
                }


                return(toReturn);
            }
        }