Exemple #1
0
        /// <summary>
        /// 计算返回的数值
        /// </summary>
        /// <returns></returns>
        public object Evaluate(string text)
        {
            Stack <Operand> operandStack = new Stack <Operand>();

            //解析
            Parse(text);

            foreach (var item in postExpression)
            {
                //获取参数的个数
                int       dimension = RegsitryManager.GetDimension(item);
                Operand[] operands  = null;
                if (dimension > 0)
                {
                    operands = new Operand[dimension];
                    for (int i = 0; i < dimension; i++)
                    {
                        operands[i] = operandStack.Pop();
                    }
                    //调整正确参数位置
                    Array.Reverse(operands);
                }
                //根据identity
                //对每个节点进行计算,返回操作数
                operandStack.Push(new Operand(item.Eval(operands)));
            }

            //最后面的值
            return(operandStack.Pop().Value);
        }
Exemple #2
0
        public object Eval(Operand[] operands)
        {
            object result = null;

            OperatorExprssion opExpression = RegsitryManager.GetOperatorEval(this) as OperatorExprssion;

            try
            {
                if (opExpression != null)
                {
                    string className  = opExpression.EvalClassName;
                    string methodName = opExpression.EvalMethodName;

                    Type t      = Type.GetType(className);
                    var  target = Activator.CreateInstance(t);
                    if (target != null)
                    {
                        MethodInfo method = t.GetMethod(methodName);
                        result = method.Invoke(target, new object[] { operands });
                    }
                }
                return(result);
            }
            catch (Exception e)
            {
                LogHelper <OperatorExprssion> .Error("表达式运算符计算出错!" + e.Message);

                throw;
            }
        }
Exemple #3
0
        private Stack <IExpression> opStack       = new Stack <IExpression>(); //堆栈


        //解析表达式,返回后缀表达式
        private bool Parse(string text)
        {
            string expression = text.Trim();

            string      literal       = string.Empty; //变量/常数/函数名等
            IExpression op            = null;         //操作符
            bool        isStringToken = false;

            int pos = 0;

            while (pos < expression.Length)
            {
                isStringToken = false;
                string token = GetToken(expression, ref pos, ref isStringToken);

                if (RegsitryManager.ISOperator(token))
                {
                    InsertPostfixExpression(new OperatorExprssion(token));
                }
                else if (RegsitryManager.IsFunction(token))
                {
                    //函数其实是一种特殊的运算符
                    InsertPostfixExpression(new FuncExpression(token));
                }
                else
                {
                    //值
                    postExpression.Add(new VariableExpression(token, isStringToken));
                }
            }
            //AddPostExpressionLiteral();
            //最后stack中的所有数据弹出加入到对应中
            while (opStack.Count > 0)
            {
                op = opStack.Pop();
                if (op.Identity == RegsitryManager.TOKEN_LEFT)
                {
                    //括号不匹配
                    throw new AddinException(text + "Expression invalid!");
                }
                else
                {
                    postExpression.Add(op);
                }
            }
            //去掉逗号运算符
            postExpression.RemoveAll(item => item.Identity == RegsitryManager.TOKEN_COMMA);
            return(true);
        }
Exemple #4
0
        public object Eval(Operand[] operands)
        {
            object result = null;

            FuncExpression opExpression = RegsitryManager.GetFunctionEval(this) as FuncExpression;


            if (opExpression != null)
            {
                string className  = opExpression.EvalClassName;
                string methodName = opExpression.EvalMethodName;

                Type t      = Type.GetType(className);
                var  target = Activator.CreateInstance(t);
                if (target != null)
                {
                    MethodInfo method = t.GetMethod(methodName);
                    result = method.Invoke(target, new object[] { operands });
                }
            }
            return(result);
        }
Exemple #5
0
        public static OperatorType OpType(string text)
        {
            if (text == RegsitryManager.TOKEN_LEFT)
            {
                return(OperatorType.LEFT);
            }
            if (text == RegsitryManager.TOKEN_RIGHT)
            {
                return(OperatorType.RIGHT);
            }
            if (text == RegsitryManager.TOKEN_ADD)
            {
                return(OperatorType.ADD);
            }
            if (text == RegsitryManager.TOKEN_SUB)
            {
                return(OperatorType.SUB);
            }
            if (text == RegsitryManager.TOKEN_MUL)
            {
                return(OperatorType.MUL);
            }
            if (text == RegsitryManager.TOKEN_DIV)
            {
                return(OperatorType.DIV);
            }

            if (text == RegsitryManager.TOKEN_AND)
            {
                return(OperatorType.AND);
            }
            if (text == RegsitryManager.TOKEN_OR)
            {
                return(OperatorType.OR);
            }
            if (text == RegsitryManager.TOKEN_LT)
            {
                return(OperatorType.LT);
            }
            if (text == RegsitryManager.TOKEN_GT)
            {
                return(OperatorType.GT);
            }
            if (text == RegsitryManager.TOKEN_LE)
            {
                return(OperatorType.LE);
            }

            if (text == RegsitryManager.TOKEN_GE)
            {
                return(OperatorType.GE);
            }
            if (text == RegsitryManager.TOKEN_ET)
            {
                return(OperatorType.ET);
            }
            if (text == RegsitryManager.TOKEN_UT)
            {
                return(OperatorType.UT);
            }

            //返回函数
            if (RegsitryManager.IsFunction(text))
            {
                return(OperatorType.FUNC);
            }


            return(OperatorType.ERR);
        }