Esempio n. 1
0
        public static IExpression BuildExpression(List <Token> tokens, ref int index, Stack <int?> precedenceStack)
        {
            if (index >= tokens.Count)
            {
                throw new TokenParserException("The script seems to have ended prematurely while I was doing calculations. Shouldn't there be something here?", tokens[tokens.Count - 1]);
            }

            Token       token = tokens[index];
            IExpression expr  = null;

            switch (token.TokenType)
            {
            case TokenType.Number:
            case TokenType.StringLiteral:
                expr = BuildSingularExpression(tokens, ref index);
                break;

            case TokenType.Word:
                if (token.Value == "true" || token.Value == "false")
                {
                    expr = new BooleanExpression();
                    expr.BuildExpression(tokens, ref index, precedenceStack);
                }
                else
                {
                    expr = BuildWordExpression(tokens, ref index, precedenceStack);
                }
                break;

            //case TokenType.GroupStart:
            //    expr = BuildGroupedExpression(tokens, ref index);
            //    break;

            default:
                throw new TokenParserException("This part of the script should equate to a value but instead I got \"" + token.Value + "\", which doesn't really mean anything in this context.", token);
            }

            int?precedence = precedenceStack.Peek();

            while (NextHasGreaterPrecedence(precedence, tokens, index))
            {
                expr = BuildBinaryExpression(tokens, ref index, expr, precedenceStack);
            }

            return(expr);
        }
Esempio n. 2
0
        public static IExpression BuildExpression(List<Token> tokens, ref int index, Stack<int?> precedenceStack)
        {
            if (index >= tokens.Count)
                throw new TokenParserException("The script seems to have ended prematurely while I was doing calculations. Shouldn't there be something here?", tokens[tokens.Count - 1]);

            Token token = tokens[index];
            IExpression expr = null;
            switch (token.TokenType)
            {
                case TokenType.Number:
                case TokenType.StringLiteral:
                    expr = BuildSingularExpression(tokens, ref index);
                    break;

                case TokenType.Word:
                    if (token.Value == "true" || token.Value == "false")
                    {
                        expr = new BooleanExpression();
                        expr.BuildExpression(tokens, ref index, precedenceStack);
                    }
                    else
                        expr = BuildWordExpression(tokens, ref index, precedenceStack);
                    break;

                //case TokenType.GroupStart:
                //    expr = BuildGroupedExpression(tokens, ref index);
                //    break;

                default:
                    throw new TokenParserException("This part of the script should equate to a value but instead I got \"" + token.Value + "\", which doesn't really mean anything in this context.", token);
            }

            int? precedence = precedenceStack.Peek();
            while (NextHasGreaterPrecedence(precedence, tokens, index))
                expr = BuildBinaryExpression(tokens, ref index, expr, precedenceStack);

            return expr;
        }