public void EvaluateSubtractExpressionWithTwoIntegers() { Context context = new Context(); context.SetValue("one", 1); context.SetValue("two", 2); var expr = new BinaryExpression(new SubtractOperation(), new NameExpression("one"), new NameExpression("two")); var result = expr.Evaluate(context); Assert.IsNotNull(result); Assert.AreEqual(-1, result); }
public void EvaluateMultiplyExpressionWithTwoIntegers() { Context context = new Context(); context.SetValue("two", 2); context.SetValue("three", 3); var expr = new BinaryExpression(new MultiplyOperation(), new NameExpression("two"), new NameExpression("three")); var result = expr.Evaluate(context); Assert.IsNotNull(result); Assert.AreEqual(6, result); }
// 访问二元表达式节点 private Expression VisitBinaryImpl(BinaryExpression node) { if (node == null) { return(node); } // array[0] if (node.NodeType == ExpressionType.ArrayIndex) { return(this.Visit(node.Evaluate())); } Expression left = node.Left.ReduceUnary(); Expression right = node.Right.ReduceUnary(); if (node.NodeType == ExpressionType.AndAlso || node.NodeType == ExpressionType.OrElse) { // expression maybe a.Name == "TAN" && a.Allowused left = BooleanUnaryToBinary(node.Left); right = BooleanUnaryToBinary(node.Right); if (left != node.Left || right != node.Right) { node = Expression.MakeBinary(node.NodeType, left, right); return(this.Visit(node)); } } // 例: a.Name ?? "TAN" if (node.NodeType == ExpressionType.Coalesce) { return(this.VisitMethodCall(node, MethodCallType.Coalesce)); } // 例: a.Name == null var constExpression = left as ConstantExpression ?? right as ConstantExpression; if (constExpression != null && constExpression.Value == null) { return(this.VisitMethodCall(node, MethodCallType.EqualNull)); } // 例: a.Name == a.FullName or like a.Name == "TAN" return(this.VisitBinary_Condition(node)); }
// 访问二元表达式 Expression VisitBinaryImpl(BinaryExpression b) { if (b == null) { return(b); } // array[0] if (b.NodeType == ExpressionType.ArrayIndex) { return(this.Visit(b.Evaluate())); } Expression left = b.Left.ReduceUnary(); Expression right = b.Right.ReduceUnary(); if (b.NodeType == ExpressionType.AndAlso || b.NodeType == ExpressionType.OrElse) { // expression maybe a.Name == "TAN" && a.Allowused left = TryMakeBinary(b.Left); right = TryMakeBinary(b.Right); if (left != b.Left || right != b.Right) { b = Expression.MakeBinary(b.NodeType, left, right); return(this.Visit(b)); } } // 例: a.Name ?? "TAN" if (b.NodeType == ExpressionType.Coalesce) { return(_methodVisitor.Visit(b, MethodCall.Coalesce)); } // 例: a.Name == null var constExpression = left as ConstantExpression ?? right as ConstantExpression; if (constExpression != null && constExpression.Value == null) { return(_methodVisitor.Visit(b, MethodCall.EqualNull)); } // 例: a.Name == a.FullName or like a.Name == "TAN" return(this.VisitBinary_Condition(b)); }
public void TestExpressAdd() { // GIVEN: an expression representing 2 + 5 var lhs = new ValueExpression { Value = 2 }; var rhs = new ValueExpression { Value = 5 }; var binaryExpression = new BinaryExpression { LeftExpression = lhs, RightExpression = rhs, Operator = Operator.Plus }; // WHEN: it is evaluated var evaluation = binaryExpression.Evaluate(); // THEN: the result has a value of 7 Assert.AreEqual(7, evaluation.Value); }
public void TestExpressDivide() { // GIVEN: an expression representing 8 / 4 var lhs = new ValueExpression { Value = 8 }; var rhs = new ValueExpression { Value = 4 }; var binaryExpression = new BinaryExpression { LeftExpression = lhs, RightExpression = rhs, Operator = Operator.Slash }; // WHEN: it is evaluated var evaluation = binaryExpression.Evaluate(); // THEN: the result has a value of 2 Assert.AreEqual(2, evaluation.Value); }
public void TestExpressMultiply() { // GIVEN: an expression representing 6 * 3 var lhs = new ValueExpression { Value = 6 }; var rhs = new ValueExpression { Value = 3 }; var binaryExpression = new BinaryExpression { LeftExpression = lhs, RightExpression = rhs, Operator = Operator.Asterisk }; // WHEN: it is evaluated var evaluation = binaryExpression.Evaluate(); // THEN: the result has a value of 18 Assert.AreEqual(18, evaluation.Value); }
public void TestExpressSubtract() { // GIVEN: an expression representing 9 - 4 var lhs = new ValueExpression { Value = 9 }; var rhs = new ValueExpression { Value = 4 }; var binaryExpression = new BinaryExpression { LeftExpression = lhs, RightExpression = rhs, Operator = Operator.Minus }; // WHEN: it is evaluated var evaluation = binaryExpression.Evaluate(); // THEN: the result has a value of 5 Assert.AreEqual(5, evaluation.Value); }