Skip to content

ADR 0003: Whole-script compilation (implicit main, print, stdout differential)

Status: Final Type: Feature Created: 2026-07-27 Related-ADRs: 0004 (the scalar formatting print grew into), 0005 (the builtin surface that followed)

Context

Real Python programs are overwhelmingly scripts (top-level executable statements plus print), and while both were refused (PP900), no whole program could be compiled, or compared against CPython at the stdout level.

Decision

  1. Script mode. When building an executable, a module whose body contains top-level executable statements gets them collected, in source order, interleaved constants included, into a synthesized entry function (__post_main__() -> None); imports, constant bindings usable at module scope, class and function definitions stay module-level. A name the top level binds and a function reads is a module global (0060). Library and ext-module builds keep rejecting top-level statements (PP900): script mode is an executable-build frontend transform, not a general relaxation, and a library has no entry point to run them in.
  2. print is an intrinsic. print(...) writes each argument as CPython's print does, space-separated and newline-terminated, sep=, end= and file= included; the scalar text is 0004's and an aggregate's 0028's. An argument with no lowered text gets a PP900-family diagnostic.
  3. Executable emission reuses the existing object pipeline: the emitted object provides __post_main__; a small driver main calls it. All registered native backends (c, qbe, llvm) implement the print lowering. The byte-identical parity oracle applies to executables exactly as it does to kernels.
  4. The differential gate. For every whole-script program the frontend accepts, compiled stdout must equal interpreted stdout, byte for byte, per backend.
  5. A program of several files. A script on disk is compiled with the POST modules it imports. The executable links them all, since real programs are split over packages.
    • What an import binds. from mod import f binds a function, as in a library build. from mod import Rect binds a class: this module constructs it, reads its fields and calls its methods, which the defining module compiled, and those methods get external linkage because another unit calls them by name. import mod and from pkg import sub (a module of a package) bind the module, and mod.f(...), mod.Rect(...) and mod.CONST resolve to its public names. A local named like the module is the local.
    • Every call across units is verified. A unit is verified against the units compiled before it, so a call into another module is checked against the callee's declared signature.
    • Every backend names the callee alike. A function named like a C keyword is __pp_double in its own unit and from every other unit.
    • A public name is the program's. Every unit's public functions are symbols of one linked artifact, so two units that define the same public name are refused (PP501) before either is verified. Python's module namespaces would accept them; module-qualified symbols are spec work (§9.1).
    • Relative imports are refused, as the spec refuses them (PP008).

Consequences

Whole programs become compilable and differentially testable end to end, while library and ext-module builds keep exactly the surface they had.