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 semicolon statements.
  if ([TokenType.kSemicolon].contains(token.type)) {
    advance();
    return null;
  }

  /// Misleading name. Rationale: The next line could be
  /// an expression or one of two assignment statements.
  /// If the next expression would preceed an assignment operation,
  /// then that expression is forwarded to [multiAssignStmt].
  Stmt assignExpr() {
    final lhs = math();
    final token = peek().type;
    if (token == TokenType.kComma || token == TokenType.kAssign) {
      return multiAssignStmt(first: lhs);
    }

    return lhs;
  }

  // Pipe result through [echo] to debug when needed.
  return echo(switch (token.type) {
    TokenType.kLocal => localStmt(),
    TokenType.kFunc => declFuncExpr(),
    TokenType.kReturn => returnStmt(),
    TokenType.kIf => ifStmt(),
    TokenType.kFor => forLoopStmt(),
    TokenType.kWhile => whileLoopStmt(),
    TokenType.kRepeat => repeatUntilLoopStmt(),
    TokenType.kBreak => breakStmt(),
    TokenType.kGoto => gotoStmt(),
    TokenType.kGotoLabel => gotoLabelStmt(),
    _ => assignExpr(),
  });
}