Esempio n. 1
0
        /// <summary>
        /// This routine will actually execute an operation and return its value
        /// </summary>
        /// <param name="op">Operator Information</param>
        /// <param name="left">left operand</param>
        /// <param name="right">right operand</param>
        /// <returns>v1 (op) v2</returns>
        public static object PerformBinaryOp(BinaryOperator op, object left, object right)
        {
            IEvaluator tv = left as IEvaluator;

            if (tv != null)
            {
                left = tv.Evaluate();
            }

            try {
                switch (op.OperatorString)   // short circuit evaluation 1/6/16
                {
                case "AND":
                    if (Convert.ToBoolean(left, CultureInfo.CurrentCulture))
                    {
                        tv = right as IEvaluator;
                        if (tv != null)
                        {
                            right = tv.Evaluate();
                        }
                        if (Convert.ToBoolean(right, CultureInfo.CurrentCulture))
                        {
                            return(true);
                        }
                    }
                    return(false);

                case "OR":
                    if (Convert.ToBoolean(left, CultureInfo.CurrentCulture))
                    {
                        return(true);
                    }
                    else
                    {
                        tv = right as IEvaluator;
                        if (tv != null)
                        {
                            right = tv.Evaluate();
                        }
                        if (Convert.ToBoolean(right, CultureInfo.CurrentCulture))
                        {
                            return(true);
                        }
                    }
                    return(false);
                }

                tv = right as IEvaluator;
                if (tv != null)
                {
                    right = tv.Evaluate();
                }
                return(op.ExecuteOperator(left, right));
            }
            catch (Exception ex) when(ex is InvalidCastException || ex is FormatException || ex is ArgumentException || ex is OverflowException)
            {
                throw new FormatException(string.Format(
                                              "Operator '{0}' cannot be applied to objects of type '{1}' and '{2}'",
                                              op.OperatorString, GetTypeName(right), GetTypeName(left)
                                              ));
            }
        }