Example #1
0
        private Expression GetLeft(string rOperator)
        {
            if (StackToken.Count == 0)
            {
                if (rOperator == "-")
                {
                    return(null);
                }
                throw new Exception($"操作符{rOperator} 参数不匹配");
            }

            if (StackOperator.Count == 0)
            {
                return(GetExpression(StackToken.Pop()));
            }
            var level  = ExpressionOperator.GetOperatorLevel(rOperator);
            var lLevel = ExpressionOperator.GetOperatorLevel(StackOperator.Peek().Value);

            if (level > lLevel)
            {
                return(GetExpression(StackToken.Pop()));
            }

            var             rToken = StackOperator.Pop();
            ExpressionMatch rMatch = new ExpressionMatch();

            rMatch.Operator = rToken.Value;

            rMatch.Right = GetExpression(StackToken.Pop());

            rMatch.Left = GetLeft(rMatch.Operator);
            return(rMatch);
        }
Example #2
0
        public override Expression Clone()
        {
            var expression = new ExpressionMatch();

            expression.Left     = Left.Clone();
            expression.Right    = Right.Clone();
            expression.Operator = Operator;
            return(expression);
        }
Example #3
0
        /// <summary>
        /// 解析一则表达式
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        private bool Resolve_FirstOrder(out Expression e)
        {
            e = null;
            bool firstOrder = false;

            switch (Current.Value)
            {
            case "+":
            case "-":
                firstOrder = Last == null || (Last.Type == ExpressionToken.eType.Operator && Last.Value != ")");
                break;

            case "!":
                firstOrder = true;
                break;
            }
            if (firstOrder)
            {
                var tokens = new List <ExpressionToken>();
                Read();
                if (Current == null)
                {
                    throw new Exception($"解析表达式异常,操作符{Last.Value}参数错误");
                }
                tokens.Add(Current);
                var m = new ExpressionMatch()
                {
                    Operator = Last.Value,
                    Left     = null,
                };
                if (!Resolve_Identifier(out m.Right))
                {
                    if (!Resolve_Block(out m.Right))
                    {
                        m.Right = Expression.Resolve(tokens.ToArray());
                        Read();
                    }
                }
                e = m;
                return(true);
            }
            return(false);
        }
Example #4
0
        /// <summary>
        /// 生成表达式
        /// </summary>
        /// <returns></returns>
        private Expression GenerateSubMatch()
        {
            ExpressionMatch rCurrentMatch = null;

            if (StackOperator.Count == 0 || StackOperator.Peek().Type == ExpressionToken.eType.End)
            {
                if (StackToken.Count == 1)
                {
                    return(GetExpression(StackToken.Pop()));
                }
                throw new Exception("语法解析失败");
            }

            while (StackOperator.Count > 0)
            {
                var rToken = StackOperator.Pop();

                if (rToken.Type == ExpressionToken.eType.End || rToken.Value == "(")
                {
                    break;
                }

                var rMatch = new ExpressionMatch();
                rMatch.Operator = rToken.Value;

                if (null == rCurrentMatch)
                {
                    rMatch.Right = GetExpression(StackToken.Pop());
                }
                else
                {
                    rMatch.Right = rCurrentMatch;
                }

                rMatch.Left = GetLeft(rMatch.Operator);

                rCurrentMatch = rMatch;
            }

            return(rCurrentMatch);
        }