Exemple #1
0
        /// <summary>
        /// 解析表达式
        /// </summary>
        /// <param name="text"></param>
        public void Parse(string text)
        {
            this._Text = text;
            DCTokenList tokens = new DCTokenList(text);

            this._RootItem = new DCGroupExpressionItem();
            ParseItem(this._RootItem, tokens);
        }
Exemple #2
0
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            DCTokenList   tokens = new DCTokenList(cboExpression.Text);
            StringBuilder result = new StringBuilder();

            for (int iCount = 0; iCount < tokens.Count; iCount++)
            {
                result.AppendLine(iCount.ToString("00") + ":" + tokens[iCount].ToString());
            }
            DCExpression exp = new DCExpression(cboExpression.Text);

            result.AppendLine(exp.ToDebugString());
            MyContext c       = new MyContext();
            object    vresult = exp.Eval(c);

            result.AppendLine("运算结果:" + vresult);
            txtResult.Text = result.ToString();
        }
Exemple #3
0
        /// <summary>
        /// 解析表达式项目
        /// </summary>
        /// <param name="rootItem"></param>
        /// <param name="tokens"></param>
        private void ParseItem(DCExpressionItem rootItem, DCTokenList tokens)
        {
            List <DCExpressionItem> items = new List <DCExpressionItem>();

            while (tokens.MoveNext())
            {
                DCExpressionItem newItem = null;
                DCToken          token   = tokens.Current;
                if (token.Type == CharType.Symbol)
                {
                    // 根据关键字 And Or 进行修复。
                    if (string.Equals(token.Text, "And", StringComparison.CurrentCultureIgnoreCase))
                    {
                        token.Type = CharType.MathOperator;
                        token.Text = "&&";
                    }
                    else if (string.Equals(token.Text, "or", StringComparison.CurrentCultureIgnoreCase))
                    {
                        token.Type = CharType.MathOperator;
                        token.Text = "||";
                    }
                }
                if (token.Type == CharType.Symbol)
                {
                    // 标识符
                    DCToken next = tokens.NextItem;
                    if (next != null && next.Type == CharType.CurLeft)
                    {
                        // 函数调用
                        tokens.MoveNext();
                        DCFunctionExpressionItem func = new DCFunctionExpressionItem();
                        func.Name = token.Text;
                        ParseItem(func, tokens);
                        newItem          = func;
                        newItem.Priority = 0;
                    }
                    else
                    {
                        if (string.Compare(token.Text, "true", true) == 0)
                        {
                            // 布尔值常量
                            newItem = new DCConstExpressionItem(true, DCValueType.Boolean);
                        }
                        else if (string.Compare(token.Text, "false", true) == 0)
                        {
                            // 布尔值常量
                            newItem = new DCConstExpressionItem(false, DCValueType.Boolean);
                        }
                        else
                        {
                            double dbl = 0;
                            if (double.TryParse(token.Text, out dbl))
                            {
                                // 数字常量
                                newItem = new DCConstExpressionItem(dbl, DCValueType.Number);
                            }
                            else
                            {
                                // 引用的变量
                                DCVariableExpressionItem var = new DCVariableExpressionItem();
                                var.Name = token.Text;
                                newItem  = var;
                            }
                        }
                        newItem.Priority = 0;
                    }
                }
                else if (token.Type == CharType.StringConst)
                {
                    // 字符串常量
                    var strV = token.Text;
                    if (strV != null && strV.Length >= 2)
                    {
                        if (strV[0] == '\'' || strV[0] == '"')
                        {
                            strV = strV.Substring(1, strV.Length - 2);
                        }
                    }
                    newItem = new DCConstExpressionItem(strV, DCValueType.String);
                }
                else if (token.Type == CharType.CurLeft)
                {
                    // 左圆括号,则进行分组
                    DCGroupExpressionItem group = new DCGroupExpressionItem();
                    newItem          = group;
                    newItem.Priority = 0;
                    ParseItem(group, tokens);
                }
                else if (token.Type == CharType.Spliter ||
                         token.Type == CharType.CurRight)
                {
                    // 分隔符号或者右圆括号则退出分组
                    if (items == null || items.Count == 0)
                    {
                        throw new System.Exception("项目分组无效:" + this.Text);
                    }
                    if (items != null && items.Count > 0)
                    {
                        DCExpressionItem item = CollpaseItems(items);
                        rootItem.AddSubItem(item);
                    }
                    items = new List <DCExpressionItem>();
                    if (token.Type == CharType.CurRight)
                    {
                        //退出分组
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
                else if (token.Type == CharType.MathOperator ||
                         token.Type == CharType.LogicOperator)
                {
                    // 操作符号

                    DCOperatorExpressionItem math = new DCOperatorExpressionItem();
                    math.Text = token.Text;
                    switch (token.Text)
                    {
                    case "+":
                        math.Operator = DCOperatorType.Plus;
                        math.Priority = 1;
                        break;

                    case "-":
                        math.Operator = DCOperatorType.Minus;
                        math.Priority = 1;
                        break;

                    case "*":
                        math.Operator = DCOperatorType.Multi;
                        math.Priority = 2;
                        break;

                    case "/":
                        math.Operator = DCOperatorType.Division;
                        math.Priority = 2;
                        break;

                    case ">":
                        math.Operator          = DCOperatorType.Bigger;
                        math.Priority          = 0;
                        math.IsLogicExpression = true;
                        break;

                    case ">=":
                        math.Operator          = DCOperatorType.BiggerOrEqual;
                        math.Priority          = 0;
                        math.IsLogicExpression = true;
                        break;

                    case "=":
                        math.Operator          = DCOperatorType.Equal;
                        math.Priority          = 0;
                        math.IsLogicExpression = true;
                        break;

                    case "==":
                        math.Operator          = DCOperatorType.Equal;
                        math.Priority          = 0;
                        math.IsLogicExpression = true;
                        break;

                    case "<":
                        math.Operator          = DCOperatorType.Less;
                        math.Priority          = 0;
                        math.IsLogicExpression = true;
                        break;

                    case "<=":
                        math.Operator          = DCOperatorType.LessOrEqual;
                        math.Priority          = 0;
                        math.IsLogicExpression = true;
                        break;

                    case "%":
                        math.Operator = DCOperatorType.Mod;
                        math.Priority = 2;
                        break;

                    case "||":
                        math.Operator          = DCOperatorType.Or;
                        math.Priority          = -1;
                        math.IsLogicExpression = true;
                        break;

                    case "&&":
                        math.Operator          = DCOperatorType.And;
                        math.Priority          = -1;
                        math.IsLogicExpression = true;
                        break;

                    case "!=":
                        math.Operator          = DCOperatorType.Unequal;
                        math.Priority          = 0;
                        math.IsLogicExpression = true;
                        break;

                    default:
                        throw new NotSupportedException("无效操作符:" + token.Text);
                    }
                    newItem = math;
                }
                else
                {
                    throw new NotSupportedException(token.Type + ":" + token.Text);
                }
                items.Add(newItem);
            }//while
            DCExpressionItem item2 = CollpaseItems(items);

            if (item2 != null)
            {
                rootItem.AddSubItem(item2);
            }
        }