internal override void AddSubItem(DCExpressionItem item)
 {
     if (_Parameters == null)
     {
         _Parameters = new DCExpressoinItemList();
     }
     _Parameters.Add(item);
 }
Ejemplo n.º 2
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);
        }
 internal override void AddSubItem(DCExpressionItem item)
 {
     if (this._Items == null)
     {
         this._Items = new DCExpressoinItemList();
     }
     this._Items.Add(item);
 }
Ejemplo n.º 4
0
        private string GetStringValue(DCExpressionItem item, IDCExpressionContext context)
        {
            if (item == null)
            {
                return(null);
            }
            object v = item.Eval(context);

            return(DCExpression.ToString(v));
        }
Ejemplo n.º 5
0
        private bool GetBooleanValue(DCExpressionItem item, IDCExpressionContext context, bool defaultValue = false)
        {
            if (item == null)
            {
                return(defaultValue);
            }
            object v = item.Eval(context);

            return(DCExpression.ToBoolean(v));
        }
Ejemplo n.º 6
0
        private double GetDoubleValue(DCExpressionItem item, IDCExpressionContext context, double defaultValue = 0)
        {
            if (item == null)
            {
                return(defaultValue);
            }
            object v = item.Eval(context);

            return(DCExpression.ToDouble(v, defaultValue));
        }
Ejemplo n.º 7
0
 internal override void AddSubItem(DCExpressionItem item)
 {
     if (this._Left == null)
     {
         this._Left = item;
     }
     else if (this._Right == null)
     {
         this._Right = item;
     }
     else
     {
         throw new NotSupportedException("无法添加超过2个子项目。");
     }
 }
Ejemplo n.º 8
0
 internal virtual void AddSubItem(DCExpressionItem item)
 {
 }
Ejemplo n.º 9
0
        /// <summary>
        /// 表达式元素列表的收缩
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        private DCExpressionItem CollpaseItems(List <DCExpressionItem> items)
        {
            if (items.Count == 1)
            {
                return(items[0]);
            }
            if (items.Count == 0)
            {
                return(null);
            }
            // 特别处理减法操作
            for (int iCount = 0; iCount < items.Count; iCount++)
            {
                if (items[iCount] is DCOperatorExpressionItem && iCount < items.Count - 1)
                {
                    DCOperatorExpressionItem item = (DCOperatorExpressionItem)items[iCount];
                    if (item.Operator == DCOperatorType.Minus)
                    {
                        bool isNegative = false;
                        if (iCount == 0)
                        {
                            //  处于第一的位置,必然是负数操作。
                            isNegative = true;
                        }
                        else
                        {
                            DCExpressionItem preItem = items[iCount - 1];
                            if (preItem is DCOperatorExpressionItem)
                            {
                                DCOperatorExpressionItem preOper = (DCOperatorExpressionItem)preItem;
                                if (preOper.IsLogicExpression)
                                {
                                    // 前面的元素是逻辑判断元素,是负数操作。
                                    isNegative = true;
                                }
                            }
                            else if (preItem is DCGroupExpressionItem)
                            {
                                // 前面的是表达式组元素,为负数操作。
                                isNegative = true;
                            }
                        }
                        if (isNegative)
                        {
                            // 将减法转换为负数运算
                            item.Operator = DCOperatorType.Negative;
                            item.Rigth    = items[iCount + 1];
                            items.RemoveAt(iCount + 1);
                            item.Priority  = 0;
                            item.Collapsed = true;
                            //iCount--;
                        }
                    } //if
                }     //if
            }         //for
            // 此处进行多次循环,每次循环将优先级最高的操作符元素提升层次。
            // 一般来说到最后只剩下一个操作符元素。
            while (items.Count > 1)
            {
                DCOperatorExpressionItem maxPriorityItem = null;
                int maxIndex = -1;
                int len      = items.Count;
                // 首先找到优先级最高的操作符。
                for (int iCount = 0; iCount < len; iCount++)
                {
                    DCExpressionItem item = items[iCount];
                    if (item.Collapsed == false && item is DCOperatorExpressionItem)
                    {
                        if (maxPriorityItem == null || maxPriorityItem.Priority < item.Priority)
                        {
                            maxPriorityItem = (DCOperatorExpressionItem)item;
                            maxIndex        = iCount;
                        }
                    }
                }//for
                if (maxPriorityItem == null)
                {
                    // 没找到要操作的运算符,则退出循环.
                    break;
                }

                if (maxIndex < items.Count - 1)
                {
                    // 吞并右边的项目
                    maxPriorityItem.Rigth = items[maxIndex + 1];
                    items.RemoveAt(maxIndex + 1);
                }
                if (maxIndex > 0)
                {
                    // 吞并左边的项目
                    maxPriorityItem.Left = items[maxIndex - 1];
                    items.RemoveAt(maxIndex - 1);
                }
                //if (maxPriorityItem.Operator == DCOperatorType.Minus
                //    && maxPriorityItem.Left == null
                //    && maxPriorityItem.Rigth != null)
                //{
                //    // 将减法操作转换为负数操作
                //    maxPriorityItem.Operator = DCOperatorType.Negative;
                //}
                // 设置运算符已经被处理过了。
                maxPriorityItem.Collapsed = true;
            }//while
            if (items.Count > 0)
            {
                return(items[0]);
            }
            return(null);
        }
Ejemplo n.º 10
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);
            }
        }