示例#1
0
文件: TagParse.cs 项目: vbyte/fmq
        /// <summary>
        /// 计算表达式
        /// </summary>
        public static object ExecExpressions(List<InnerExpression> exps)
        {
            if (exps.Count == 0) return "";
            while (exps.Count > 1)
            {
                List<int> priIdxes = GetMaxPriorityExpIndexes(exps);
                int total = exps.Count;

                int offSet = 0, cIdx = 0;
                InnerExpression tempExp = null;
                InnerOperate op = null;

                if (priIdxes.Count == 0)
                {
                    #region 没有操作符号的数学运算
                    while (exps.Count >= 2)
                    {
                        tempExp = new InnerExpression();
                        if (Regex.IsMatch(exps[1].TagDefinition,
                            InnerExpression.ArrayIndexFetchPattern, RegexOptions.Compiled))
                        {
                            #region 数组下标直接获取 "ab"[0] = "a"
                            string strExpValue = exps[0].GetValue().ToString();
                            int arrIdx = Convert.ToInt32(exps[1].TagDefinition.Trim('[', ']'));
                            tempExp.TagDefinition = (arrIdx < strExpValue.Length) ? strExpValue[arrIdx].ToString() : strExpValue;
                            tempExp.IsString = true;
                            #endregion
                        }
                        else
                        {
                            tempExp.TagDefinition = InnerExpression.ComputeTwo(exps[0], new InnerOperate("+"), exps[1]).ToString();
                            tempExp.IsString = exps[0].IsString;
                        }
                        tempExp.IsEntity = true;
                        exps[0] = tempExp;
                        exps.RemoveAt(1);
                    }
                    #endregion
                }
                else
                {
                    #region 处理同级运算
                    foreach (int idx in priIdxes)
                    {
                        cIdx = idx + offSet;
                        //Util.Debug(false, cIdx);
                        op = new InnerOperate(exps[cIdx].GetValue().ToString());
                        if (op.IsUnary())
                        {
                            #region 一元 自增/减操作 直接运算
                            // ++i
                            if ((idx + 1) < total)
                            {
                                tempExp = new InnerExpression();
                                tempExp.TagDefinition = InnerExpression.ComputeOne(exps[cIdx + 1], op).ToString();
                                tempExp.IsEntity = true;
                                exps[cIdx + 1] = tempExp;

                                exps.RemoveAt(cIdx);
                                offSet -= 1;
                            }
                            else
                            {
                                //i++
                                if (idx > 0)
                                {
                                    tempExp = new InnerExpression();
                                    tempExp.TagDefinition = InnerExpression.ComputeOne(exps[cIdx - 1], op).ToString();
                                    tempExp.IsEntity = true;
                                    exps[cIdx - 1] = tempExp;

                                    exps.RemoveAt(cIdx);
                                    offSet -= 1;
                                }
                            }
                            #endregion
                        }
                        else if (op.IsTernary())
                        {
                            //最低优先级 ? a : b
                            #region 三目条件运算符
                            if (idx + 3 < total)
                            {
                                tempExp = new InnerExpression();
                                tempExp.TagDefinition = (Convert.ToBoolean(exps[cIdx - 1].GetValue()) ? exps[cIdx + 1].GetValue() : exps[cIdx + 3].GetValue()).ToString();
                                tempExp.IsEntity = true;

                                exps[cIdx - 1] = tempExp;

                                exps.RemoveAt(cIdx);
                                exps.RemoveAt(cIdx);
                                exps.RemoveAt(cIdx);
                                exps.RemoveAt(cIdx);
                                offSet -= 4;
                            }
                            #endregion
                        }
                        else
                        {
                            #region 双目运算
                            if (exps.Count >= 3)
                            {
                                tempExp = new InnerExpression();
                                tempExp.TagDefinition = InnerExpression.ComputeTwo(exps[cIdx - 1], op, exps[cIdx + 1]).ToString();
                                tempExp.IsString = exps[cIdx - 1].IsString;
                                tempExp.IsEntity = true;
                                exps[cIdx - 1] = tempExp;

                                exps.RemoveAt(cIdx);
                                exps.RemoveAt(cIdx);
                                offSet -= 2;
                            }
                            else
                            {
                                //无效运算操作
                                exps.RemoveAt(cIdx);
                                offSet -= 1;
                            }
                            #endregion
                        }
                    }
                    #endregion
                }
                //Util.Debug(false, "当前还剩余:" + exps.Count.ToString());
                //foreach (InnerExpression tex in exps)
                //{
                //    Util.Debug(false, tex.GetValue());
                //}
            }
            return exps[0].GetValue();
        }