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)); }
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)); }
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); }
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)); }
private void resolve(GStmt.Stmt stmt) { stmt.Accept(this); }
private void execute(GStmt.Stmt st) { st.Accept(this); }
public If(Expr condition, Stmt thenBranch, Stmt elseBranch) { this.condition = condition; this.thenBranch = thenBranch; this.elseBranch = elseBranch; }
public Try(Stmt tryStmt, Stmt catchStmt) { this.tryStmt = tryStmt; this.catchStmt = catchStmt; }
public While(Expr condition, Stmt body) { this.condition = condition; this.body = body; }