Exemplo n.º 1
0
        private CalResult CallResult(BinTree <IExpressPart> calBinTree)
        {
            if (calBinTree.LeftTree == null && calBinTree.RightTree == null)
            {
                if (calBinTree.TreeNode == null)
                {
                    return(new CalResult
                    {
                        Result = ""
                    });
                }
                if (calBinTree.TreeNode is CalExpress)
                {
                    CalExpress ce = calBinTree.TreeNode as CalExpress;

                    return(new CalResult
                    {
                        Result = ce.Value,
                        //ResultType = Comm.GetType(ce.Express)
                    });
                }
            }

            if (calBinTree.TreeNode is IIteratorCollectionFun && this.CalCurrent.CurrentIndex != -1)
            {
                var indx = this.CalCurrent.CurrentIndex;
                (calBinTree.TreeNode as CalSign).OnBeginOperator += () =>
                {
                    this.CalCurrent.CurrentIndex = indx;
                };
                this.CalCurrent.CurrentIndex = -1;
            }

            if (!(calBinTree.TreeNode is CalSign))
            {
                throw new ExpressErrorException(string.Format("未能计算的表达式:{0}。", calBinTree.TreeNode));
            }

            var calSign = (calBinTree.TreeNode as CalSign);
            //var time1 = DateTime.Now;

            CalResult leftResult  = null;
            CalResult rightResult = null;

            if (calBinTree.LeftTree != null)
            {
                //如果左边是赋值的则不调取值方法
                if (calBinTree.TreeNode is SetValueSign)
                {
                    ((VarSign)calBinTree.LeftTree.TreeNode).IsSetValue = true;
                }

                leftResult = CallResult(calBinTree.LeftTree);
            }


            if (calBinTree.RightTree != null)
            {
                if (calBinTree.TreeNode is ConditionSign)
                {
                    if (leftResult.Results != null)
                    {
                        throw new Exception("条件表达式左值太多");
                    }
                    else
                    {
                        if (!(leftResult.Result is bool))
                        {
                            throw new ExpressErrorException("if then的条件表达式条件必须为bool值!");
                        }

                        if (leftResult.Result.ToBool())
                        {
                            if (calBinTree.RightTree.TreeNode is ElseSign)
                            {
                                rightResult = CallResult(calBinTree.RightTree.LeftTree);
                            }
                            else
                            {
                                rightResult = CallResult(calBinTree.RightTree);
                            }
                        }
                        else
                        {
                            if (calBinTree.RightTree.TreeNode is ElseSign)
                            {
                                rightResult = CallResult(calBinTree.RightTree.RightTree);
                            }
                            else
                            {
                                rightResult = new CalResult
                                {
                                    Result     = null,
                                    ResultType = typeof(bool)
                                };
                            }
                        }
                    }
                }
                else
                {
                    rightResult = CallResult(calBinTree.RightTree);
                }
            }

            CalSign cs = calBinTree.TreeNode as CalSign;

            cs.LeftVal  = leftResult;
            cs.RightVal = rightResult;

            var result = cs.Operate();

            //calSign.ExeTicks += DateTime.Now.Subtract(time1).TotalMilliseconds;
            return(result);
        }