/// <summary>
        /// Gets the row number in the C# 4.0 spec operator precedence table.
        /// </summary>
        static int GetPrecedence(Expression expr)
        {
            // Note: the operator precedence table on MSDN is incorrect
            if (expr is QueryExpression)
            {
                // Not part of the table in the C# spec, but we need to ensure that queries within
                // primary expressions get parenthesized.
                return(QueryOrLambda);
            }
            UnaryOperatorExpression uoe = expr as UnaryOperatorExpression;

            if (uoe != null)
            {
                if (uoe.Operator == UnaryOperatorType.PostDecrement || uoe.Operator == UnaryOperatorType.PostIncrement)
                {
                    return(Primary);
                }
                else
                {
                    return(Unary);
                }
            }
            if (expr is CastExpression)
            {
                return(Unary);
            }
            BinaryOperatorExpression boe = expr as BinaryOperatorExpression;

            if (boe != null)
            {
                switch (boe.Operator)
                {
                case BinaryOperatorType.Multiply:
                case BinaryOperatorType.Divide:
                case BinaryOperatorType.Modulus:
                    return(13);                            // multiplicative

                case BinaryOperatorType.Add:
                case BinaryOperatorType.Subtract:
                    return(12);                            // additive

                case BinaryOperatorType.ShiftLeft:
                case BinaryOperatorType.ShiftRight:
                    return(11);

                case BinaryOperatorType.GreaterThan:
                case BinaryOperatorType.GreaterThanOrEqual:
                case BinaryOperatorType.LessThan:
                case BinaryOperatorType.LessThanOrEqual:
                    return(RelationalAndTypeTesting);

                case BinaryOperatorType.Equality:
                case BinaryOperatorType.InEquality:
                    return(Equality);

                case BinaryOperatorType.BitwiseAnd:
                    return(8);

                case BinaryOperatorType.ExclusiveOr:
                    return(7);

                case BinaryOperatorType.BitwiseOr:
                    return(6);

                case BinaryOperatorType.ConditionalAnd:
                    return(5);

                case BinaryOperatorType.ConditionalOr:
                    return(4);

                case BinaryOperatorType.NullCoalescing:
                    return(3);

                default:
                    throw new NotSupportedException("Invalid value for BinaryOperatorType");
                }
            }
            if (expr is IsExpression || expr is AsExpression)
            {
                return(RelationalAndTypeTesting);
            }
            if (expr is ConditionalExpression)
            {
                return(Conditional);
            }
            if (expr is AssignmentExpression || expr is LambdaExpression)
            {
                return(Assignment);
            }
            // anything else: primary expression
            return(Primary);
        }
Beispiel #2
0
 public virtual S VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression, T data)
 {
     return(VisitChildren(binaryOperatorExpression, data));
 }