/// <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) { long lLeft = 0;; long lRight = 0;; if (long.TryParse(((NumberToken)left).WholePart(), out lLeft) && long.TryParse(((NumberToken)right).WholePart(), out lRight)) { if (Operation.ToString().Equals("&", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((lLeft & lRight).ToString()); } else if (Operation.ToString().Equals("|", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((lLeft | lRight).ToString()); } else if (Operation.ToString().Equals("^", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((lLeft ^ lRight).ToString()); } else { throw new FormatException(String.Format("Could not parse long value from {0} for binary 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 if (Operation.ToString().Equals("^", StringComparison.InvariantCultureIgnoreCase)) { result = new BooleanToken((bLeft ^ bRight).ToString()); } else { throw new FormatException(String.Format("Could not parse bool value from {0} for binary expression.", left.ToString())); } } } else { throw new MalformedExpressionException(String.Concat(ToString(), " is not a valid binary expression.")); } return(result); }
/// <summary> /// Returns a string representation of this VariableExpression. /// </summary> /// <returns></returns> public override string ToString() { return(_value.ToString()); }
/// <summary> /// Creates an instance of NumberToken. /// </summary> /// <param name="other"></param> public NumberToken(Token other) : base(other) { _representation = other.ToString(); }
/// <summary> /// Creates an instance of ArithmeticOperatorToken. /// </summary> /// <param name="other"></param> public ArithmeticOperatorToken(Token other) : base(other) { DeterminePrecedence(other.ToString()); }
/// <summary> /// Creates an instance of BooleanToken. /// </summary> /// <param name="other"></param> public BooleanToken(Token other) : base(other) { _representation = other.ToString(); }
/// <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); }