예제 #1
0
        private static object PerformBinaryOp(ExpressionEval.BinaryOp op, object v1, object v2)
        {
            IExpression expression1 = v1 as IExpression;

            if (expression1 != null)
            {
                v1 = expression1.Evaluate();
            }
            IExpression expression2 = v2 as IExpression;

            if (expression2 != null)
            {
                v2 = expression2.Evaluate();
            }
            switch (op.Op)
            {
            case "!=":
            case "+":
            case "-":
            case "<":
            case "<=":
            case "=":
            case "==":
            case ">":
            case ">=":
                return(ExpressionEval.DoSpecialOperator(op, v1, v2));

            case "%":
                return((object)(Convert.ToInt64(v1, (IFormatProvider)CultureInfo.CurrentCulture) % Convert.ToInt64(v2, (IFormatProvider)CultureInfo.CurrentCulture)));

            case "&":
                return((object)(ulong)((long)Convert.ToUInt64(v1, (IFormatProvider)CultureInfo.CurrentCulture) & (long)Convert.ToUInt64(v2, (IFormatProvider)CultureInfo.CurrentCulture)));

            case "&&":
                return((object)(bool)(!Convert.ToBoolean(v1, (IFormatProvider)CultureInfo.CurrentCulture) ? false : (Convert.ToBoolean(v2, (IFormatProvider)CultureInfo.CurrentCulture) ? true : false)));

            case "*":
                return((object)(Convert.ToDouble(v1, (IFormatProvider)CultureInfo.CurrentCulture) * Convert.ToDouble(v2, (IFormatProvider)CultureInfo.CurrentCulture)));

            case "/":
                return((object)(Convert.ToDouble(v1, (IFormatProvider)CultureInfo.CurrentCulture) / Convert.ToDouble(v2, (IFormatProvider)CultureInfo.CurrentCulture)));

            case "<<":
                return((object)(Convert.ToInt64(v1, (IFormatProvider)CultureInfo.CurrentCulture) << Convert.ToInt32(v2, (IFormatProvider)CultureInfo.CurrentCulture)));

            case ">>":
                return((object)(Convert.ToInt64(v1, (IFormatProvider)CultureInfo.CurrentCulture) >> Convert.ToInt32(v2, (IFormatProvider)CultureInfo.CurrentCulture)));

            case "^":
                return((object)(ulong)((long)Convert.ToUInt64(v1, (IFormatProvider)CultureInfo.CurrentCulture) ^ (long)Convert.ToUInt64(v2, (IFormatProvider)CultureInfo.CurrentCulture)));

            case "|":
                return((object)(ulong)((long)Convert.ToUInt64(v1, (IFormatProvider)CultureInfo.CurrentCulture) | (long)Convert.ToUInt64(v2, (IFormatProvider)CultureInfo.CurrentCulture)));

            case "||":
                return((object)(bool)(Convert.ToBoolean(v1, (IFormatProvider)CultureInfo.CurrentCulture) ? true : (Convert.ToBoolean(v2, (IFormatProvider)CultureInfo.CurrentCulture) ? true : false)));

            default:
                throw new ArgumentException("Binary Operator " + op.Op + "not defined.");
            }
        }