Пример #1
0
        //Less than.
        public static bool LessThan(ParserRuleContext context, AlgoValue left, AlgoValue right, bool equalTo)
        {
            //Get the values for both left and right in a specific format.
            //(Statements must be in order of casting heirarchy, top down).
            if (left.Type == AlgoValueType.Float || right.Type == AlgoValueType.Float)
            {
                //Convert both to float.
                AlgoValue leftConverted  = AlgoOperators.ConvertType(context, left, AlgoValueType.Float);
                AlgoValue rightConverted = AlgoOperators.ConvertType(context, right, AlgoValueType.Float);

                //Check if the left is less than the right.
                if (equalTo)
                {
                    return((BigFloat)left.Value <= (BigFloat)right.Value);
                }
                else
                {
                    return((BigFloat)left.Value < (BigFloat)right.Value);
                }
            }
            else if (left.Type == AlgoValueType.Rational || right.Type == AlgoValueType.Rational)
            {
                //Convert both to rational.
                AlgoValue leftConverted  = AlgoOperators.ConvertType(context, left, AlgoValueType.Rational);
                AlgoValue rightConverted = AlgoOperators.ConvertType(context, right, AlgoValueType.Rational);

                //Check if the left is less than the right.
                if (equalTo)
                {
                    return((BigRational)left.Value <= (BigRational)right.Value);
                }
                else
                {
                    return((BigRational)left.Value < (BigRational)right.Value);
                }
            }
            else if (left.Type == AlgoValueType.Integer || right.Type == AlgoValueType.Integer)
            {
                //Convert both to integer.
                AlgoValue leftConverted  = AlgoOperators.ConvertType(context, left, AlgoValueType.Integer);
                AlgoValue rightConverted = AlgoOperators.ConvertType(context, right, AlgoValueType.Integer);

                //Check if the left is less than the right.
                if (equalTo)
                {
                    return((BigInteger)left.Value <= (BigInteger)right.Value);
                }
                else
                {
                    return((BigInteger)left.Value < (BigInteger)right.Value);
                }
            }
            else
            {
                Error.Fatal(context, "Cannot compare values with type " + left.Type.ToString() + " and " + right.Type.ToString() + ".");
                return(false);
            }
        }
Пример #2
0
        //Rational.
        public static AlgoValue ConvertRat(ParserRuleContext context, params AlgoValue[] args)
        {
            //Is it a string?
            if (args[0].Type == AlgoValueType.String)
            {
                //Return a parsed bigint.
                return(new AlgoValue()
                {
                    Type = AlgoValueType.Rational,
                    Value = BigRational.Parse((string)args[0].Value)
                });
            }

            //Nah, then just cast.
            return(AlgoOperators.ConvertType(context, args[0], AlgoValueType.Rational));
        }
Пример #3
0
        public void NullConversion()
        {
            //Create a test conversion for all of types from null.
            AlgoValue nullVal = AlgoValue.Null;

            ANTLRDebug.EnterTestMode();

            //Test all conversions (these are expected to throw).
            for (int i = 0; i < Enum.GetNames(typeof(AlgoValueType)).Length; i++)
            {
                try
                {
                    var attemptConv = AlgoOperators.ConvertType(null, nullVal, (AlgoValueType)i);
                }
                catch { Assert.Pass(); }
            }

            Assert.Fail();
        }
Пример #4
0
        public void StringConversions()
        {
            //Create test conversions for strings.
            AlgoValue stringVal = new AlgoValue()
            {
                Type  = AlgoValueType.String,
                Value = "the quick brown fox jumps over the lazy dog"
            };

            ANTLRDebug.EnterTestMode();

            //Values that should pass.
            AlgoValue testVal;

            try
            {
                testVal = AlgoOperators.ConvertType(null, stringVal, AlgoValueType.String);
            }
            catch
            {
                Assert.Fail();
            }

            //Values that should fail.
            for (int i = 0; i < Enum.GetNames(typeof(AlgoValueType)).Length; i++)
            {
                if ((AlgoValueType)i == AlgoValueType.String)
                {
                    continue;
                }

                try
                {
                    testVal = AlgoOperators.ConvertType(null, stringVal, (AlgoValueType)i);
                    Assert.Fail();
                }
                catch { }
            }

            Assert.Pass();
        }
Пример #5
0
 //Bytes
 public static AlgoValue ConvertBytes(ParserRuleContext context, params AlgoValue[] args)
 {
     //Just return convertType, no special cases here.
     return(AlgoOperators.ConvertType(context, args[0], AlgoValueType.Bytes));
 }