Esempio n. 1
0
        private AstNode Statement()
        {
            // STATEMENT = BLOCK |
            //             var VARIABLE-DECL | // implicit variable declaration
            //             IDENITIFER VARIABLE-DECL | // explicit variable declaration with user-defined type
            //             TYPE-SPEC VARAIBLE-DECL | // explicit variable declaration with primitive type
            //             IDENTIFIER ASSIGNMENT |
            //			   IDENTIFIER INCREMENT-BY EXPRESSION |
            //			   IDENTIFIER DECREMENT-BY EXPRESSION |
            //			   IDENTIFIER MULTIPLY-BY EXPRESSION |
            //			   IDENTIFIER DIVIDE-BY EXPRESSION |
            //             RETURN EXPRESSION |
            //             BRANCH |
            //             NO-OP

            if (_currentToken.Type == Tokens.BlockStart)
            {
                return(Block());
            }
            else if (_currentToken.Type == Tokens.Variable)
            {
                Eat(_currentToken.Type);
                return(VariableDeclaration(TypeNode.Inferred));
            }
            else if (_currentToken.Type == Tokens.Identifier)
            {
                var id = _currentToken.Value.ToString();
                Eat(Tokens.Identifier);
                if (_currentToken.Type == Tokens.Assign)
                {
                    return(Assignment(new VariableNode(id)));
                }
                else if (_currentToken.Type == Tokens.Identifier)
                {
                    return(VariableDeclaration(new TypeNode(id)));
                }
                else if (_currentToken.Type == Tokens.IncrementBy)
                {
                    Eat(Tokens.IncrementBy);
                    return(new IncrementByNode(new VariableNode(id), Expression()));
                }
                else if (_currentToken.Type == Tokens.DecrementBy)
                {
                    Eat(Tokens.DecrementBy);
                    return(new DecrementByNode(new VariableNode(id), Expression()));
                }
                else if (_currentToken.Type == Tokens.MultiplyBy)
                {
                    Eat(Tokens.MultiplyBy);
                    return(new MultiplyByNode(new VariableNode(id), Expression()));
                }
                else if (_currentToken.Type == Tokens.DivideBy)
                {
                    Eat(Tokens.DivideBy);
                    return(new DivideByNode(new VariableNode(id), Expression()));
                }
            }
            else if (Tokens.IsPrimitiveType(_currentToken.Type))
            {
                return(VariableDeclaration(TypeSpec()));
            }
            else if (_currentToken.Type == Tokens.Return)
            {
                Eat(Tokens.Return);
                return(new ReturnNode(Expression()));
            }
            else if (_currentToken.Type == Tokens.Branch)
            {
                Eat(Tokens.Branch);
                return(Branch());
            }
            else
            {
                return(new NoOpNode());
            }
            throw new Exception("bad statement");
        }