compile method

String compile()

Compile the program entry module and return formatted Dart source.

Implementation

String compile() {
  final mainModule = program.modules.firstWhere(
    (m) => m.name == program.entryModule,
    orElse: () =>
        throw StateError('Entry module "${program.entryModule}" not found'),
  );

  final entryFunc = mainModule.functions.firstWhere(
    (f) => f.name == program.entryFunction,
    orElse: () => throw StateError(
      'Entry function "${program.entryFunction}" not found',
    ),
  );

  final lib = _buildLibrary(mainModule, entryFunc);
  if (noFormat) return _emitRaw(lib);
  try {
    return _format(lib);
  } catch (_) {
    // Defensive: the dart_style formatter can choke on pathological output
    // (e.g. deeply nested expressions); fall back to raw, unformatted source.
    // No deterministic input triggers this in the test corpus.
    return _emitRaw(lib); // coverage:ignore-line
  }
}