Exemple #1
0
 public ExecuteFilterExp(GetFieldExp getfieldExp, FilterExp exp)
     : base(Token.FilterOP)
 {
     this.GetfieldExp = getfieldExp;
     this.FilterExp   = exp;
 }
        public static ExpNode CreateExpTree(this List<Token> tokenList)
        {
            tokenList.Add(Token.END);
            Stack<ExpNode> expStack = new Stack<ExpNode>();
            Stack<Stack<ExpNode>> expStackStack = new Stack<Stack<ExpNode>>();
            Stack<Token> verbStack = new Stack<Token>();
            Stack<Stack<Token>> verbStackStack = new Stack<Stack<Token>>();
            verbStack.Push(Token.BEGIN);
            for (int i = 0; i < tokenList.Count; i++)
            {
                Token token = tokenList[i];
                if (token.OpType == TokenType.Field)
                {
                    var temp = new FieldExp(token);
                    expStack.Push(temp);
                    continue;
                }

                if (token.OpType == TokenType.ConstStr || token.OpType == TokenType.Int || token.OpType == TokenType.Double)
                {
                    var temp = new ConstExp(token);
                    expStack.Push(temp);
                    continue;
                }

                if (token.OpType == TokenType.FilterStart)
                {
                    expStackStack.Push(expStack);
                    verbStackStack.Push(verbStack);
                    expStack = new Stack<ExpNode>();
                    verbStack = new Stack<Token>();
                    verbStack.Push(token);
                    continue;
                }

                if (token.OpType == TokenType.FilterEnd)
                {
                    while (verbStack.Peek().OpType != TokenType.FilterStart)
                    {
                        CreateNode(expStack, verbStack.Pop());
                    }
                    if (expStack.Count != 1)
                        throw new Exception();
                    var subExp = expStack.Pop();
                    if (!(subExp is FilterExp))
                    {
                        throw new Exception();
                    }

                    FilterExp subExpFilter = (FilterExp)subExp;
                    expStack = expStackStack.Pop();
                    verbStack = verbStackStack.Pop();
                    if (!priority.TryGetValue(token.OpType, out var endPriority))
                        throw new Exception();
                    while (true)
                    {
                        Token topToken = verbStack.Peek();
                        var topinfo = priority[topToken.OpType];
                        if (endPriority.Item1 > topinfo.Item1)
                            break;
                        CreateNode(expStack, verbStack.Pop());
                    }
                    var getFieldExp = expStack.Pop();
                    if (!(getFieldExp is GetFieldExp))
                        throw new Exception();
                    expStack.Push(new ExecuteFilterExp((GetFieldExp)getFieldExp, subExpFilter));
                    continue;
                }

                if (priority.TryGetValue(token.OpType, out var nowinfo))
                {
                    while (true)
                    {
                        Token topToken = verbStack.Peek();
                        var topinfo = priority[topToken.OpType];
                        if (topinfo.Item1 == nowinfo.Item1)
                        {
                            if (topinfo.Item2 == Combination.LEFT)
                            {
                                CreateNode(expStack, verbStack.Pop());
                            }

                            if (topinfo.Item2 == Combination.RIGHT)
                            {
                                verbStack.Push(token);
                                break;
                            }

                            if (topinfo.Item2 == Combination.NON)
                                throw new Exception();
                            continue;
                        }
                        if (topinfo.Item1 > nowinfo.Item1)
                        {
                            CreateNode(expStack, verbStack.Pop());
                        }
                        if (topinfo.Item1 < nowinfo.Item1)
                        {
                            verbStack.Push(token);
                            break;
                        }
                    }
                }
                else
                {
                    throw new Exception();
                }
            }
            //判断
            if (verbStackStack.Count != 0 || expStackStack.Count != 0)
            {
                throw new Exception();
            }

            if (verbStack.Count == 2 && expStack.Count == 1 && verbStack.Pop().OpType == TokenType.End &&
                verbStack.Pop().OpType == TokenType.Begin)
                return expStack.Pop();
            throw new Exception();
        }