forStatement method
Implementation
Stmt forStatement() {
consume(TokenType.LEFT_PAREN, "Expect '(' after 'for'.");
Stmt? initializer;
if (match([TokenType.SEMICOLON])) {
initializer = null;
} else if (match([TokenType.VAR])) {
initializer = varDeclaration();
} else {
initializer = expressionStatement();
}
Expr? condition;
if (!check(TokenType.SEMICOLON)) {
condition = expression();
}
consume(TokenType.SEMICOLON, "Expect ';' after loop condition.");
Expr? increment;
if (!check(TokenType.RIGHT_PAREN)) {
increment = expression();
}
consume(TokenType.RIGHT_PAREN, "Expect ')' after for clause.");
Stmt body = statement();
if (increment != null) {
body = Block([body, Expression(increment)]);
}
condition ??= Literal(true);
body = While(condition, body);
if (initializer != null) {
body = Block([initializer, body]);
}
return body;
}