Ejemplo n.º 1
0
        public Node AssignmentOrCallStatement()
        {
            var  id_node = Node.fromToken(Expect(TokenCategory.IDENTIFIER));
            Node node    = null;

            if (Has(TokenCategory.PARENTHESIS_OPEN))
            {
                node = new CallStatementNode()
                {
                    AnchorToken = id_node.AnchorToken
                };
                Expect(TokenCategory.PARENTHESIS_OPEN);
                node.Add(Optional(firstOfExpression, () =>
                {
                    var list = new List <Node>()
                    {
                        Expression()
                    };
                    list.AddRange(ZeroOrMore(TokenCategory.COMMA, Expression, true));
                    return(list);
                }));
                Expect(TokenCategory.PARENTHESIS_CLOSE);
            }
            else if (Has(TokenCategory.BRACKET_OPEN) || Has(TokenCategory.COLON_EQUAL))
            {
                node = new AssignmentNode();
                if (Has(TokenCategory.BRACKET_OPEN))
                {
                    Expect(TokenCategory.BRACKET_OPEN);
                    var expression_node = Expression();
                    Expect(TokenCategory.BRACKET_CLOSE);
                    node.Add(new ListIndexNode()
                    {
                        id_node, expression_node
                    });
                }
                else
                {
                    node.Add(id_node);
                }
                node.AnchorToken = Expect(TokenCategory.COLON_EQUAL);
                node.Add(Expression());
            }
            else
            {
                throw new SyntaxError(firstOfAssignmentOrCallStatement, tokenStream.Current);
            }
            Expect(TokenCategory.SEMICOLON);
            return(node);
        }
Ejemplo n.º 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 AssignmentNode()
                    {
                        AnchorToken = idToken
                    };
                    ass.Add(Expr());
                    Expect(TokenCategory.SEMICOLON);
                    return(ass);

                case TokenCategory.INCREMENT:
                    Expect(TokenCategory.INCREMENT);

                    var inc = new IncrementNode()
                    {
                        AnchorToken = idToken
                    };
                    Expect(TokenCategory.SEMICOLON);
                    return(inc);

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

                case TokenCategory.PARENTHESIS_OPEN:
                    Expect(TokenCategory.PARENTHESIS_OPEN);
                    var fun = new FunCallNode()
                    {
                        AnchorToken = idToken
                    };

                    fun.Add(ExprList());

                    Expect(TokenCategory.PARENTHESIS_CLOSE);
                    Expect(TokenCategory.SEMICOLON);
                    return(fun);
                }
                break;

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

            case TokenCategory.WHILE:
                return(While());

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

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

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

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