예제 #1
0
        protected override object Evaluate(IExpression left, IExpression right, ExecutionState state)
        {
            object  b = TokenParser.VerifyUnderlyingType(right.Evaluate(state, token));
            object  a = TokenParser.VerifyUnderlyingType(left.Evaluate(state, token));
            decimal x, y;

            if (a is decimal)
            {
                x = (decimal)a;
            }
            else
            {
                TypeConverter tc = TypeDescriptor.GetConverter(a);
                if (!tc.CanConvertTo(typeof(decimal)))
                {
                    ThrowException(a);
                }
                x = (decimal)tc.ConvertTo(a, typeof(decimal));
            }
            if (b is decimal)
            {
                y = (decimal)b;
            }
            else
            {
                TypeConverter tc = TypeDescriptor.GetConverter(b);
                if (!tc.CanConvertTo(typeof(decimal)))
                {
                    ThrowException(b);
                }
                y = (decimal)tc.ConvertTo(b, typeof(decimal));
            }
            return(x - y);
        }
예제 #2
0
 public static object EvaluateArguments(ExecutionState state, object o, List <ExpressionArgument> args, Token contextToken)
 {
     if (o == null)
     {
         if (contextToken.Previous != null)
         {
             throw new InstructionExecutionException("\"" + contextToken.Previous.Value + "\" is not a keyword and has not been assigned a value as a variable. As such, it cannot evaluate the specified argument list.", contextToken.Previous);
         }
         throw new InstructionExecutionException("The value here is null, which isn't able to process an argument list.", contextToken);
     }
     if (o is IList)
     {
         if (args.Count > 1)
         {
             throw new InstructionExecutionException("I can't evaluate the arguments for this list because you've specified more than one argument. The only argument you can specify for a list is a numeric expression indicating which list item you're referring to.", contextToken);
         }
         object n = TokenParser.VerifyUnderlyingType(args[0].Expression.Evaluate(state, args[0].Token));
         if (!(n is decimal))
         {
             throw new InstructionExecutionException("The argument you specified doesn't equate to a number, which is required to specify which list item you're referring to.", args[0].Token);
         }
         int index = Convert.ToInt32(n);
         if (index >= ((IList)o).Count)
         {
             throw new InstructionExecutionException("The index specified here is higher than the highest index in the list. Remember, the lowest index is 0 and the highest is one less than the total number of items in the list.", args[0].Token);
         }
         return(((IList)o)[index]);
     }
     throw new InstructionExecutionException("This type of object isn't able to process an argument list. (Underlying type: " + o.GetType().Name, contextToken);
 }
예제 #3
0
        protected override object Evaluate(IExpression left, IExpression right, ExecutionState state)
        {
            object b = TokenParser.VerifyUnderlyingType(right.Evaluate(state, token));
            object a = TokenParser.VerifyUnderlyingType(left.Evaluate(state, token));

            return(!(a.Equals(b) || b.Equals(a)));            // we check both sides in case one side has overridden the Equals method
        }
예제 #4
0
        protected override object Evaluate(IExpression left, IExpression right, ExecutionState state)
        {
            object b = TokenParser.VerifyUnderlyingType(right.Evaluate(state, token));
            object a = TokenParser.VerifyUnderlyingType(left.Evaluate(state, token));

            if (a is IComparable && b is IComparable)
            {
                return(((IComparable)a).CompareTo(b) <= 0);
            }
            throw new InstructionExecutionException("I can't check if the first thing is less than or equal to the second thing because they're not really comparable in that way.", token);
        }
예제 #5
0
            public override bool Equals(object o)
            {
                object val = TokenParser.VerifyUnderlyingType(o);

                if (val == null)
                {
                    return(!value);
                }
                bool oValue = !(val.Equals(0m) || val.Equals(null) || val.Equals("") || val.Equals(false));

                return(value == oValue);
            }
예제 #6
0
        protected override object Evaluate(IExpression left, IExpression right, ExecutionState state)
        {
            object  b = TokenParser.VerifyUnderlyingType(right.Evaluate(state, token));
            object  a = TokenParser.VerifyUnderlyingType(left.Evaluate(state, token));
            decimal x, y;

            if (a is decimal)
            {
                x = (decimal)a;
            }
            else
            {
                TypeConverter tc = TypeDescriptor.GetConverter(a);
                if (!tc.CanConvertTo(typeof(decimal)))
                {
                    ThrowException(a);
                }
                x = (decimal)tc.ConvertTo(a, typeof(decimal));
            }
            if (b is decimal)
            {
                y = (decimal)b;
            }
            else
            {
                TypeConverter tc = TypeDescriptor.GetConverter(b);
                if (!tc.CanConvertTo(typeof(decimal)))
                {
                    ThrowException(b);
                }
                y = (decimal)tc.ConvertTo(b, typeof(decimal));
            }
            if (y == 0)
            {
                throw new InstructionExecutionException("I can't divide the first number by the second because the second is zero. i.e. Noone can divide by zero. Not even me.", token);
            }
            return(x / y);
        }
예제 #7
0
        protected override object Evaluate(IExpression left, IExpression right, ExecutionState state)
        {
            object b = TokenParser.VerifyUnderlyingType(TokenParser.ReduceFromExpression(state, token, right.Evaluate(state, token)));
            object a = TokenParser.VerifyUnderlyingType(TokenParser.ReduceFromExpression(state, token, left.Evaluate(state, token)));

            if (a == null || b == null)
            {
                if (a == null || b == null)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            if ((a is decimal && b is string) || (a is string && b is decimal))
            {
                decimal k, m;
                bool    result;
                if (a is decimal && b is string)
                {
                    k      = (decimal)a;
                    result = decimal.TryParse((string)b, out m);
                }
                else
                {
                    k      = (decimal)b;
                    result = decimal.TryParse((string)a, out m);
                }
                if (result)
                {
                    return(k.Equals(m));
                }
            }
            return(a.Equals(b) || b.Equals(a));            // we check both sides in case one side has overridden the Equals method
        }
예제 #8
0
        protected override object Evaluate(IExpression left, IExpression right, ExecutionState state)
        {
            object  b = TokenParser.VerifyUnderlyingType(right.Evaluate(state, token));
            object  a = TokenParser.VerifyUnderlyingType(left.Evaluate(state, token));
            decimal x, y;

            //typeof(decimal).IsAssignableFrom(typeof(a))
            if (a is decimal)
            {
                x = (decimal)a;
            }
            else
            {
                //if(a is int || a is long || a is double || a is float || a is short
                TypeConverter tc = TypeDescriptor.GetConverter(a);
                if (!tc.CanConvertTo(typeof(decimal)))
                {
                    return(string.Concat(a, b));
                }
                x = (decimal)tc.ConvertTo(a, typeof(decimal));
            }
            if (b is decimal)
            {
                y = (decimal)b;
            }
            else
            {
                TypeConverter tc = TypeDescriptor.GetConverter(b);
                if (!tc.CanConvertTo(typeof(decimal)))
                {
                    return(string.Concat(a, b));
                }
                y = (decimal)tc.ConvertTo(b, typeof(decimal));
            }
            return(x + y);
        }