stmt method

Stmt? stmt()

Implementation

Stmt? stmt() {
  final Token token = peek();

  // Consume tokens which consist of a newline
  // at the start of a statement.
  if (token.type == TokenType.kNewLine) {
    advance();
    return null;
  }

  // Consume comments.
  if ([
    TokenType.kLineComment,
    TokenType.kBlockComment,
  ].contains(token.type)) {
    advance();
    return null;
  }

  // Added feature late: https://www.lua.org/manual/5.5/manual.html#3.3.3
  handleMultiAssignExpr(MathExpr expr) {
    if (peek().type == TokenType.kComma) {
      // Ugly hack. Clearly one term is bound.
      // We can drop the other terms, but we need to
      // collect them.
      if (expr is AssignExpr) {
        while (peek().type == TokenType.kComma) {
          advance();
          math();
        }
      } else {
        return multiAssignExpr(first: expr);
      }
    }
    return expr;
  }

  return echo(switch (token.type) {
    TokenType.kLocal => localStmt(),
    TokenType.kReturn => returnStmt(),
    TokenType.kIf => ifStmt(),
    TokenType.kFor => forLoopStmt(),
    TokenType.kWhile => whileLoopStmt(),
    TokenType.kRepeat => repeatUntilLoopStmt(),
    TokenType.kBreak => breakStmt(),
    TokenType.kGoto => gotoStmt(),
    TokenType.kGotoLabel => gotoLabelStmt(),
    _ => handleMultiAssignExpr(math()),
  });
}