コード例 #1
0
        protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
        {
            PrimitiveExpression o = other as PrimitiveExpression;

            return(o != null && (this.Value == AnyValue || object.Equals(this.Value, o.Value)));
        }
コード例 #2
0
 void IAstVisitor.VisitPrimitiveExpression(PrimitiveExpression primitiveExpression)
 {
     Visit(EnterPrimitiveExpression, LeavePrimitiveExpression, primitiveExpression);
 }
        public override void VisitCastExpression(CastExpression castExpression)
        {
            ParenthesizeIfRequired(castExpression.Expression, InsertParenthesesForReadability ? Primary : Unary);
            // There's a nasty issue in the C# grammar: cast expressions including certain operators are ambiguous in some cases
            // "(int)-1" is fine, but "(A)-b" is not a cast.
            UnaryOperatorExpression uoe = castExpression.Expression as UnaryOperatorExpression;

            if (uoe != null && !(uoe.Operator == UnaryOperatorType.BitNot || uoe.Operator == UnaryOperatorType.Not))
            {
                if (TypeCanBeMisinterpretedAsExpression(castExpression.Type))
                {
                    Parenthesize(castExpression.Expression);
                }
            }
            // The above issue can also happen with PrimitiveExpressions representing negative values:
            PrimitiveExpression pe = castExpression.Expression as PrimitiveExpression;

            if (pe != null && pe.Value != null && TypeCanBeMisinterpretedAsExpression(castExpression.Type))
            {
                TypeCode typeCode = Type.GetTypeCode(pe.Value.GetType());
                switch (typeCode)
                {
                case TypeCode.SByte:
                    if ((sbyte)pe.Value < 0)
                    {
                        Parenthesize(castExpression.Expression);
                    }
                    break;

                case TypeCode.Int16:
                    if ((short)pe.Value < 0)
                    {
                        Parenthesize(castExpression.Expression);
                    }
                    break;

                case TypeCode.Int32:
                    if ((int)pe.Value < 0)
                    {
                        Parenthesize(castExpression.Expression);
                    }
                    break;

                case TypeCode.Int64:
                    if ((long)pe.Value < 0)
                    {
                        Parenthesize(castExpression.Expression);
                    }
                    break;

                case TypeCode.Single:
                    if ((float)pe.Value < 0)
                    {
                        Parenthesize(castExpression.Expression);
                    }
                    break;

                case TypeCode.Double:
                    if ((double)pe.Value < 0)
                    {
                        Parenthesize(castExpression.Expression);
                    }
                    break;

                case TypeCode.Decimal:
                    if ((decimal)pe.Value < 0)
                    {
                        Parenthesize(castExpression.Expression);
                    }
                    break;
                }
            }
            base.VisitCastExpression(castExpression);
        }