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¶
- 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. printis an intrinsic.print(...)writes each argument as CPython'sprintdoes, space-separated and newline-terminated,sep=,end=andfile=included; the scalar text is 0004's and an aggregate's 0028's. An argument with no lowered text gets a PP900-family diagnostic.- Executable emission reuses the existing object pipeline: the emitted object provides
__post_main__; a small drivermaincalls 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. - The differential gate. For every whole-script program the frontend accepts, compiled stdout must equal interpreted stdout, byte for byte, per backend.
- 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 fbinds a function, as in a library build.from mod import Rectbinds 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 modandfrom pkg import sub(a module of a package) bind the module, andmod.f(...),mod.Rect(...)andmod.CONSTresolve 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_doublein 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).
- What an import binds.
Consequences¶
Whole programs become compilable and differentially testable end to end, while library and ext-module builds keep exactly the surface they had.