Example #1
0
        internal static Expression SubExpr(ScriptLoadingContext lcontext, bool isPrimary)
        {
            Expression e = null;

            Token T = lcontext.Lexer.Current;

            if (T.IsUnaryOperator())
            {
                lcontext.Lexer.Next();
                e = SubExpr(lcontext, false);

                // check for power operator -- it be damned forever and ever for being higher priority than unary ops
                Token unaryOp = T;
                T = lcontext.Lexer.Current;

                if (isPrimary && T.Type == TokenType.Op_Pwr)
                {
                    List <Expression> powerChain = new List <Expression>();
                    powerChain.Add(e);

                    while (isPrimary && T.Type == TokenType.Op_Pwr)
                    {
                        lcontext.Lexer.Next();
                        powerChain.Add(SubExpr(lcontext, false));
                        T = lcontext.Lexer.Current;
                    }

                    e = powerChain[powerChain.Count - 1];

                    for (int i = powerChain.Count - 2; i >= 0; i--)
                    {
                        e = BinaryOperatorExpression.CreatePowerExpression(powerChain[i], e, lcontext);
                    }
                }

                e = new UnaryOperatorExpression(lcontext, e, unaryOp);
            }
            else
            {
                e = SimpleExp(lcontext);
            }

            T = lcontext.Lexer.Current;

            if (isPrimary && T.IsBinaryOperator())
            {
                object chain = BinaryOperatorExpression.BeginOperatorChain();

                BinaryOperatorExpression.AddExpressionToChain(chain, e);

                while (T.IsBinaryOperator())
                {
                    BinaryOperatorExpression.AddOperatorToChain(chain, T);
                    lcontext.Lexer.Next();
                    Expression right = SubExpr(lcontext, false);
                    BinaryOperatorExpression.AddExpressionToChain(chain, right);
                    T = lcontext.Lexer.Current;
                }

                e = BinaryOperatorExpression.CommitOperatorChain(chain, lcontext);
            }

            return(e);
        }