forStatement method

AstNode forStatement()

Implementation

AstNode forStatement() {
  consume(TokenType.leftParen, "Expect '(' after 'for'.");
  AstNode? initializer;
  if (match([TokenType.semicolon])) {
    initializer = null;
  } else if (match(
      [TokenType.tartVar, TokenType.tartConst, TokenType.tartFinal])) {
    initializer = varDeclaration();
  } else {
    initializer = expressionStatement();
  }
  AstNode? condition = !check(TokenType.semicolon) ? expression() : null;
  consume(TokenType.semicolon, "Expect ';' after loop condition.");
  AstNode? increment = !check(TokenType.rightParen) ? expression() : null;
  consume(TokenType.rightParen, "Expect ')' after for clauses.");
  AstNode body = statement();
  return ForStatement(initializer, condition, increment, body);
}