Example #1
0
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        private static IExpression ParseMultExpression()
        {
            if (Check(FirstExpExp))
            {
                var exp = ParseExpExpression();

                while (Check(new TokenSet(TokenType.Multiply | TokenType.Divide)))
                {
                    var opType = _currentToken.Type;
                    Eat(opType);
                    if (!Check(FirstExpExp))
                    {
                        throw new InvalidSyntaxException("Expected an expression after * or / operator");
                    }
                    var right = ParseExpExpression();

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

                    case TokenType.Divide:
                        exp = new DivExpression(exp, right);
                        break;

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

                return(exp);
            }
            throw new InvalidSyntaxException("Invalid expression");
        }
Example #2
0
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        private static IExpression ParseMultExpression()
        {
            if (Check(FirstExpExp))
            {
                var exp = ParseExpExpression();

                while (Check(new TokenSet(TokenType.Multiply | TokenType.Divide)))
                {
                    var opType = _currentToken.Type;
                    Eat(opType);
                    if (!Check(FirstExpExp))
                    {
                        throw new InvalidSyntaxException("Expected an expression after * or / operator");
                    }
                    var right = ParseExpExpression();

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

                        case TokenType.Divide:
                            exp = new DivExpression(exp, right);
                            break;

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

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