Ejemplo n.º 1
0
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        private static IExpression  ParseAddExpression()
        {
            if (Check(FirstMultExp))
            {
                IExpression exp = ParseMultExpression();

                while (Check(new TokenSet(TokenType.Plus | TokenType.Minus)))
                {
                    TokenType opType = CurrentToken.type;
                    Eat(opType);
                    if (!Check(FirstMultExp))
                    {
                        throw new InvalidSyntaxException("Expected an expression after + or - operator");
                    }
                    IExpression right = ParseMultExpression();

                    switch (opType)
                    {
                    case TokenType.Plus:
                        exp = new AddExpression(exp, right);
                        break;

                    case TokenType.Minus:
                        exp = new SubExpression(exp, right);
                        break;

                    default:
                        throw new UnexpectedBehaviorException("Expected plus or minus, got: " + opType);
                    }
                }

                return(exp);
            }
            else
            {
                throw new InvalidSyntaxException("Invalid expression");
            }
        }
Ejemplo n.º 2
0
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        private static IExpression ParseAddExpression()
        {
            if ( Check( FirstMultExp ) )
            {
                IExpression exp = ParseMultExpression();

                while ( Check( new TokenSet( TokenType.Plus | TokenType.Minus ) ) )
                {
                    TokenType opType = CurrentToken.type;
                    Eat( opType );
                    if ( !Check( FirstMultExp ) )
                    {
                        throw new InvalidSyntaxException( "Expected an expression after + or - operator" );
                    }
                    IExpression right = ParseMultExpression();

                    switch ( opType )
                    {
                    case TokenType.Plus:
                        exp = new AddExpression( exp, right );
                        break;

                    case TokenType.Minus:
                        exp = new SubExpression( exp, right );
                        break;

                    default:
                        throw new UnexpectedBehaviorException( "Expected plus or minus, got: " + opType );
                    }
                }

                return exp;
            }
            else
            {
                throw new InvalidSyntaxException( "Invalid expression" );
            }
        }