コード例 #1
0
        /// <summary>
        /// Evaluates this expression.
        /// </summary>
        /// <returns></returns>
        public override Token Evaluate()
        {
            Token result = new NullToken();
            Token left   = EvaluateOperand(1);
            Token right  = EvaluateOperand(0);

            if (left is NumberToken && right is NumberToken)
            {
                double dLeft  = 0.0;
                double dRight = 0.0;

                if (double.TryParse(left.ToString(), out dLeft) && double.TryParse(right.ToString(), out dRight))
                {
                    if (Operation.ToString().Equals("==", StringComparison.InvariantCultureIgnoreCase))
                    {
                        result = new BooleanToken((dLeft == dRight).ToString());
                    }
                    else if (Operation.ToString().Equals("<=", StringComparison.InvariantCultureIgnoreCase))
                    {
                        result = new BooleanToken((dLeft <= dRight).ToString());
                    }
                    else if (Operation.ToString().Equals(">=", StringComparison.InvariantCultureIgnoreCase))
                    {
                        result = new BooleanToken((dLeft >= dRight).ToString());
                    }
                    else if (Operation.ToString().Equals("<", StringComparison.InvariantCultureIgnoreCase))
                    {
                        result = new BooleanToken((dLeft < dRight).ToString());
                    }
                    else if (Operation.ToString().Equals(">", StringComparison.InvariantCultureIgnoreCase))
                    {
                        result = new BooleanToken((dLeft > dRight).ToString());
                    }
                    else if (Operation.ToString().Equals("!=", StringComparison.InvariantCultureIgnoreCase))
                    {
                        result = new BooleanToken((dLeft != dRight).ToString());
                    }
                    else
                    {
                        throw new MalformedExpressionException(String.Format("Unknown boolean operator {0}.", Operation));
                    }
                }
                else
                {
                    throw new FormatException(String.Format("Could not parse double value from {0} for boolean expression.", left.ToString()));
                }
            }
            else if (left is BooleanToken && right is BooleanToken)
            {
                bool bLeft  = false;
                bool bRight = false;

                if (bool.TryParse(left.ToString(), out bLeft) && bool.TryParse(right.ToString(), out bRight))
                {
                    if (Operation.ToString().Equals("==", StringComparison.InvariantCultureIgnoreCase))
                    {
                        result = new BooleanToken((bLeft == bRight).ToString());
                    }
                    else if (Operation.ToString().Equals("!=", StringComparison.InvariantCultureIgnoreCase))
                    {
                        result = new BooleanToken((bLeft != bRight).ToString());
                    }
                    else
                    {
                        throw new MalformedExpressionException("Booleans cannot be less than or greater than each other.");
                    }
                }
                else
                {
                    throw new FormatException(String.Format("Could not parse bool value from {0} for boolean expression.", left.ToString()));
                }
            }
            else
            {
                throw new MalformedExpressionException(String.Concat(ToString(), " is not a valid boolean expression."));
            }

            return(result);
        }
コード例 #2
0
 /// <summary>
 /// Creates an instance of BooleanExpression.
 /// </summary>
 /// <param name="operation"></param>
 /// <param name="left"></param>
 /// <param name="right"></param>
 public BooleanExpression(Token operation, Expression left, Expression right)
     : base(ExpressionPrecedence.Boolean, operation, new List <Expression>() { left, right })
 {
 }