Beispiel #1
0
        private GStmt.Stmt forStatement()
        {
            consume(tt.LEFT_PAREN, "Expect '(' after 'for'.");

            GStmt.Stmt initializer;
            if (Match(tt.SEMICOLON))
            {
                initializer = null;
            }
            else if (Match(tt.VAR))
            {
                initializer = varDeclaration();
            }
            else
            {
                initializer = expressionStatement();
            }

            GExpr.Expr condition = null;
            if (!check(tt.SEMICOLON))
            {
                condition = expression();
            }
            consume(tt.SEMICOLON, "Expect ';' after loop condition.");

            GExpr.Expr increment = null;
            if (!check(tt.RIGHT_PAREN))
            {
                increment = expression();
            }
            consume(tt.RIGHT_PAREN, "Expect ')' after for clauses");
            GStmt.Stmt body = statement();
            if (increment != null)
            {
                body = new GStmt.Block(new List <GStmt.Stmt>()
                {
                    body, new GStmt.Expression(increment)
                });
            }
            if (condition == null)
            {
                condition = new GExpr.Literal(true);
            }
            body = new GStmt.While(condition, body);

            if (initializer != null)
            {
                body = new GStmt.Block(new List <GStmt.Stmt>()
                {
                    initializer, body
                });
            }


            return(body);
        }
Beispiel #2
0
 public object visit_Literal_Expr(GExpr.Literal expr)
 {
     return(expr.value);
 }