visitIfStmt method

  1. @override
Object? visitIfStmt(
  1. IfStmt stmt
)
override

Implementation

@override
Object? visitIfStmt(IfStmt stmt) {
  // Set to true in order to handle `else` branch.
  bool visitBody = true;

  if (!stmt.isTerminalElse) {
    visitBody = stmt.expr!.accept(this)?.toLua('ctrl').isTruthy ?? false;
  }

  if (visitBody) {
    Object? res;
    final prevScope = scope;
    pushScope();
    try {
      for (Stmt s in stmt.body) {
        final out = s.accept(this);

        // One of these code paths must be non-null
        // unless all paths are null.
        // TODO: better value inference.
        if (out != null) {
          res = out;
        }
      }
    } catch (e) {
      rethrow;
    } finally {
      restoreScope(prevScope);
    }

    return res;
  }

  return stmt.nextIfStmt?.accept(this);
}