Example #1
0
 private GStmt.Stmt tryStatement()
 {
     GStmt.Stmt tryBlock = statement();
     consume(tt.CATCH, "Expect catch block after try");
     GStmt.Stmt catchBlock = statement();
     return(new GStmt.Try(tryBlock, catchBlock));
 }
Example #2
0
        private GStmt.Stmt whileStatement()
        {
            consume(tt.LEFT_PAREN, "Expect '(' after 'while'");
            GExpr.Expr condition = expression();
            consume(tt.RIGHT_PAREN, "Expect ')' after condition");
            GStmt.Stmt body = statement();

            return(new GStmt.While(condition, body));
        }
Example #3
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);
        }
Example #4
0
        private GStmt.Stmt ifStatement()
        {
            consume(tt.LEFT_PAREN, "Expect ( after 'if'");
            GExpr.Expr condition = expression();
            consume(tt.RIGHT_PAREN, "Expect ) after if condition.");

            GStmt.Stmt thenBranch = statement();
            GStmt.Stmt elseBranch = null;
            if (Match(tt.ELSE))
            {
                elseBranch = statement();
            }

            return(new GStmt.If(condition, thenBranch, elseBranch));
        }
Example #5
0
 private void resolve(GStmt.Stmt stmt)
 {
     stmt.Accept(this);
 }
Example #6
0
 private void execute(GStmt.Stmt st)
 {
     st.Accept(this);
 }
Example #7
0
 public If(Expr condition, Stmt thenBranch, Stmt elseBranch)
 {
     this.condition  = condition;
     this.thenBranch = thenBranch;
     this.elseBranch = elseBranch;
 }
Example #8
0
 public Try(Stmt tryStmt, Stmt catchStmt)
 {
     this.tryStmt   = tryStmt;
     this.catchStmt = catchStmt;
 }
Example #9
0
 public While(Expr condition, Stmt body)
 {
     this.condition = condition;
     this.body      = body;
 }