示例#1
0
        public Node ExprPrimary()
        {
            //‹id› | ‹fun-call› | ‹array› | ‹lit› | (‹expr›)
            switch (CurrentToken)
            {
            case TokenCategory.IDENTIFIER:     //var or fun-call
                var idToken = Expect(TokenCategory.IDENTIFIER);
                if (CurrentToken == TokenCategory.OPENEDPAR)
                {
                    Expect(TokenCategory.OPENEDPAR);
                    var fun = new FunCall()
                    {
                        AnchorToken = idToken
                    };
                    fun.Add(ExprList());
                    Expect(TokenCategory.CLOSEDPAR);
                    return(fun);
                }
                else
                {
                    var id = new Identifier()
                    {
                        AnchorToken = idToken
                    };
                    return(id);
                }

            case TokenCategory.OPENEDBRACKET:     //array
                return(Arr());

            case TokenCategory.OPENEDPAR:     //(expr)
                Expect(TokenCategory.OPENEDPAR);
                var n = Expr();
                Expect(TokenCategory.CLOSEDPAR);
                return(n);

            case TokenCategory.STRING:
                return(new Str()
                {
                    AnchorToken = Expect(TokenCategory.STRING)
                });

            case TokenCategory.CHAR:
                return(new Char()
                {
                    AnchorToken = Expect(TokenCategory.CHAR)
                });

            case TokenCategory.INTLITERAL:
                return(new IntLiteral()
                {
                    AnchorToken = Expect(TokenCategory.INTLITERAL)
                });

            default:
                throw new SyntaxError(firstOfExprPrimary, tokenStream.Current);
            }
        }
示例#2
0
        public Node Stmt()
        {
            switch (CurrentToken)
            {
            case TokenCategory.IDENTIFIER:
                var idToken = Expect(TokenCategory.IDENTIFIER);
                switch (CurrentToken)
                {
                case TokenCategory.ASSIGN:
                    Expect(TokenCategory.ASSIGN);
                    var ass = new Assignment()
                    {
                        AnchorToken = idToken
                    };
                    ass.Add(Expr());
                    Expect(TokenCategory.SEMICOLON);
                    return(ass);

                case TokenCategory.INCREMENT:
                    Expect(TokenCategory.INCREMENT);
                    //Expect(TokenCategory.INCREMENT);
                    var inc = new Increment()
                    {
                        AnchorToken = idToken
                    };
                    Expect(TokenCategory.SEMICOLON);
                    return(inc);

                case TokenCategory.DECREMENT:
                    Expect(TokenCategory.DECREMENT);
                    var dec = new Decrement()
                    {
                        AnchorToken = idToken
                    };
                    Expect(TokenCategory.SEMICOLON);
                    return(dec);

                case TokenCategory.OPENEDPAR:
                    Expect(TokenCategory.OPENEDPAR);
                    var fun = new FunCall()
                    {
                        AnchorToken = idToken
                    };
                    //if(CurrentToken != TokenCategory.CLOSEDPAR){
                    fun.Add(ExprList());
                    //}
                    Expect(TokenCategory.CLOSEDPAR);
                    Expect(TokenCategory.SEMICOLON);
                    return(fun);
                }
                break;

            case TokenCategory.IF:
                return(If());

            case TokenCategory.LOOP:
                return(Loop());

            case TokenCategory.BREAK:
                var bre = new Stmt()
                {
                    AnchorToken = Expect(TokenCategory.BREAK)
                };
                Expect(TokenCategory.SEMICOLON);
                return(bre);

            case TokenCategory.RETURN:
                return(Return());

            case TokenCategory.SEMICOLON:
                return(new Stmt()
                {
                    AnchorToken = Expect(TokenCategory.SEMICOLON)
                });

            default:
                throw new SyntaxError(firstOfStmt, tokenStream.Current);
            }
            throw new SyntaxError(firstOfStmt, tokenStream.Current);
        }