public void Visit(BinaryExpression expression) { if (expression == null) { throw new ArgumentNullException("expression"); } expression.LeftExpression.Accept(this); object left = result; expression.RightExpression.Accept(this); object right = result; if (left is int && right is int) { switch (expression.Type) { case BinaryExpressionType.Plus: result = Convert.ToInt32(left) + Convert.ToInt32(right); break; case BinaryExpressionType.Minus: result = Convert.ToInt32(left) - Convert.ToInt32(right); break; case BinaryExpressionType.Multiply: result = Convert.ToInt32(left) * Convert.ToInt32(right); break; case BinaryExpressionType.Div: result = Convert.ToInt32(left) / Convert.ToInt32(right); break; case BinaryExpressionType.Mod: result = Convert.ToInt32(left) % Convert.ToInt32(right); break; case BinaryExpressionType.ShiftLeft: result = Convert.ToInt32(left) << Convert.ToInt32(right); break; case BinaryExpressionType.ShiftRight: result = Convert.ToInt32(left) >> Convert.ToInt32(right); break; case BinaryExpressionType.GreaterOrEqual: result = (Convert.ToInt32(left) >= Convert.ToInt32(right)) ? 1 : 0; break; case BinaryExpressionType.LesserOrEqual: result = (Convert.ToInt32(left) <= Convert.ToInt32(right)) ? 1 : 0; break; case BinaryExpressionType.Greater: result = (Convert.ToInt32(left) > Convert.ToInt32(right)) ? 1 : 0; break; case BinaryExpressionType.Lesser: result = (Convert.ToInt32(left) < Convert.ToInt32(right)) ? 1 : 0; break; case BinaryExpressionType.Equal: result = (Convert.ToInt32(left) == Convert.ToInt32(right)) ? 1 : 0; break; case BinaryExpressionType.NotEqual: result = (Convert.ToInt32(left) != Convert.ToInt32(right)) ? 1 : 0; break; case BinaryExpressionType.BitXor: result = Convert.ToInt32(left) ^ Convert.ToInt32(right); break; case BinaryExpressionType.BitAnd: result = Convert.ToInt32(left) & Convert.ToInt32(right); break; case BinaryExpressionType.BitOr: result = Convert.ToInt32(left) | Convert.ToInt32(right); break; case BinaryExpressionType.And: bool andResult = Convert.ToBoolean(left) && Convert.ToBoolean(right); result = Convert.ToInt32(andResult); break; case BinaryExpressionType.Or: bool orResult = Convert.ToBoolean(left) || Convert.ToBoolean(right); result = Convert.ToInt32(orResult); break; default: throw new ExpressionEvaluatorException( String.Format("binary operation '{0}' is not supported.", expression.Type)); } } else { result = String.Format("{0} {1} {2}", left, GetBinaryOperatorString(expression.Type), right); } }
public void Visit(BinaryExpression expression) { throw new NotImplementedException(); }