Пример #1
0
        public object Evaluate()
        {
            EvaluationVisitor visitor = new EvaluationVisitor();

            visitor.TaxFunction                       += TaxFunction;
            visitor.TaxWithPointFunction              += TaxWithPointFunction;
            visitor.AnnualBonusTaxFunction            += AnnualBonusTaxFunction;
            visitor.ForeignTaxFunction                += ForeignTaxFunction;
            visitor.IsSalaryEndDateMonthEquelFunction += IsSalaryEndDateMonthEquelFunction;
            visitor.DoubleSalaryFunction              += DoubleSalaryFunction;
            visitor.AnnualBonusForeignTaxFunction     += AnnualBonusForeignTaxFunction;
            visitor.EvaluateFunction                  += EvaluateFunction;
            visitor.EvaluateParameter                 += EvaluateParameter;
            visitor.Parameters = parameters;

            //LogicalExpression.Create(Parse(expression)).Accept(visitor);
            //modify by wsl
            CommonTree ct = Parse(expression);
            //将表达式转化成LogicalExpression对象
            LogicalExpression le = LogicalExpression.Create(ct);

            //计算表达式
            le.Accept(visitor);
            return(visitor.Result);
        }
Пример #2
0
        public void LogicalExpressionShouldHandlePrecedence()
        {
            BinaryExpresssion expression = LogicalExpression.Create(Parse("3 * (5 + 2)")) as BinaryExpresssion;

            EvaluationVisitor visitor = new EvaluationVisitor();

            visitor.Visit(expression);
            Assert.AreEqual(21, visitor.Result);

            Assert.AreEqual(40, new Expression("5 * 2 ^ 3").Evaluate());
        }
Пример #3
0
        public object Evaluate()
        {
            EvaluationVisitor visitor = new EvaluationVisitor();

            visitor.EvaluateFunction  += EvaluateFunction;
            visitor.EvaluateParameter += EvaluateParameter;
            visitor.Parameters         = parameters;

            LogicalExpression.Create(Parse(expression)).Accept(visitor);
            return(visitor.Result);
        }
Пример #4
0
        public void LogicalExpressionShouldCreateTimes()
        {
            BinaryExpresssion expression = LogicalExpression.Create(Parse("3 * 2")) as BinaryExpresssion;

            Assert.IsNotNull(expression);
            Assert.AreEqual(BinaryExpressionType.Times, expression.Type);

            EvaluationVisitor visitor = new EvaluationVisitor();

            visitor.Visit(expression);
            Assert.AreEqual(6, visitor.Result);
        }
Пример #5
0
        public void LogicalExpressionShouldCreateNot()
        {
            UnaryExpression expression = LogicalExpression.Create(Parse("not true")) as UnaryExpression;

            Assert.IsNotNull(expression);
            Assert.AreEqual(typeof(Value), expression.Expression.GetType());

            EvaluationVisitor visitor = new EvaluationVisitor();

            visitor.Visit(expression);
            Assert.AreEqual(false, visitor.Result);
        }
Пример #6
0
        public void LogicalExpressionShouldCreateInteger()
        {
            Value expression = LogicalExpression.Create(Parse("2")) as Value;

            Assert.IsNotNull(expression);
            Assert.AreEqual("2", expression.Text);

            EvaluationVisitor visitor = new EvaluationVisitor();

            visitor.Visit(expression);
            Assert.AreEqual(2, visitor.Result);
        }