예제 #1
0
 private static void Validate(BinaryExpression.ExpressionType type)
 {
     if (type < BinaryExpression.ExpressionType.Add || type >= BinaryExpression.ExpressionType._Count)
     {
         throw new ArgumentException("Invalid ExpressionType value", nameof(type));
     }
 }
예제 #2
0
파일: Extras.cs 프로젝트: nike4613/MathExpr
        public void BinaryExpressionTypeChecks(BinaryExpression.ExpressionType type, bool isBool, bool isComp)
        {
            Assert.Equal(isBool, type.IsBooleanType());
            Assert.Equal(isComp, type.IsComparisonType());

            Assert.Equal(isBool, type.IsBooleanType());
            Assert.Equal(isComp, type.IsComparisonType());
        }
예제 #3
0
        public static bool IsComparisonType(this BinaryExpression.ExpressionType type)
        {
            Validate(type);

            var idx  = (int)type / FlagsPerItem;
            var offs = (int)type % FlagsPerItem;

            var flags = (typeflags[idx] >> (offs * BitsPerFlag)) & Flags;

            if (flags == FlagUnset)
            {
                flags = InitFlagsFor(type);
            }

            return(flags == FlagComparison);
        }
예제 #4
0
        private static int InitFlagsFor(BinaryExpression.ExpressionType type)
        {
            var field = typeof(BinaryExpression.ExpressionType).GetField(type.ToString(), BindingFlags.Public | BindingFlags.Static);

            if (field == null)
            {
                throw new InvalidOperationException("Could not get field for expression type");
            }

            int flags = FlagNothing;

            if (field.GetCustomAttribute <BinaryExpression.BooleanAttribute>() != null)
            {
                flags = FlagBoolean;
            }
            else if (field.GetCustomAttribute <BinaryExpression.ComparisonAttribute>() != null)
            {
                flags = FlagComparison;
            }

            var idx  = (int)type / FlagsPerItem;
            var offs = (int)type % FlagsPerItem;

            var bitoffs = offs * BitsPerFlag;
            int read2;
            var read = typeflags[idx];

            do
            {
                read2 = read;
                read &= ~(Flags << bitoffs);
                read |= flags << bitoffs;
                read  = Interlocked.CompareExchange(ref typeflags[idx], read, read2);
            }while (read != read2);

            return(flags);
        }