// parse
        private string Parse()
        {
            foreach (string token in Tokens)
            {
                if (IsNumber(token))
                {
                    Stack.Push(new Number(token));
                }
                else if (token == "X")
                {
                    Stack.Push(new Number(token));
                }
                else if (IsUnary(token))
                {
                    Stack.Push(UnaryExpr.Create(Stack, token));
                }
                else
                {
                    Stack.Push(BinExpr.Create(Stack, token));
                }
            }

            StringBuilder sb = new StringBuilder();

            Stack.Pop().Write(sb);
            return(sb.ToString());
        }
Esempio n. 2
0
 public Filter BuildFilter()
 {
     if (tokens.Count == 0)
     {
         return(new True());
     }
     if (tokens.Count == 1)
     {
         if (tokens[0] is Expression)
         {
             return((tokens[0] as Expression).BuildFilter());
         }
         else if (tokens[0] is TokenVal)
         {
             return(Checker.Create((tokens[0] as TokenVal).Val));
         }
         throw new FormatException($"语法错误'{tokens[0]}'");
     }
     if (tokens[1] is TokenLogical)
     {
         List <Tuple <LogicalOperator, Filter> > list = new List <Tuple <LogicalOperator, Filter> >();
         LogicalOperator op = LogicalOperator.And;
         for (int i = 0; i < tokens.Count; i++)
         {
             list.Add(new Tuple <LogicalOperator, Filter>(op, (tokens[i] as Expression).BuildFilter()));
             i++;
             if (i < tokens.Count)
             {
                 op = (tokens[i] as TokenLogical).Operator;
             }
         }
         return(new Logic(list));
     }
     if (tokens[1] is TokenCompare)
     {
         if (tokens.Count < 3)
         {
             throw new FormatException($"表达式不完整!");
         }
         if (tokens.Count > 3)
         {
             throw new FormatException($"语法错误'{tokens[3]}'");
         }
         return(Checker.Create((tokens[0] as TokenVal).Val, (tokens[1] as TokenCompare).Operator, (tokens[2] as TokenVal).Val));
     }
     if (tokens[0] is TokenUnary)
     {
         if (tokens.Count == 2 && tokens[1] is Expression)
         {
             return(UnaryExpr.Create((tokens[0] as TokenUnary).Operator, (tokens[1] as Expression).BuildFilter()));
         }
         if (tokens.Count == 3 && tokens[1] is TokenVal && tokens[2] is Expression)
         {
             return(UnaryExpr.Create((tokens[0] as TokenUnary).Operator, (tokens[2] as Expression).BuildFilter(), (tokens[1] as TokenVal).Val));
         }
         throw new FormatException($"错误的用法'{tokens[0]}'");
     }
     throw new FormatException($"语法错误'{tokens[0]}'");
 }