|
| 1 | +# Compiler source language contract |
| 2 | + |
| 3 | +PARSE-01 adopts `python-browser/1` as the initial language profile, using Python |
| 4 | +3.14 grammar and semantics for ordinary Python constructs. This contract is |
| 5 | +future compiler work; the released package still provides Python-side |
| 6 | +JavaScript compatibility objects. The reference corpus checks Python behavior, |
| 7 | +not successful translation. |
| 8 | + |
| 9 | +## First application and accepted source |
| 10 | + |
| 11 | +Start with a single-module equivalent of Shimify's text_metrics application: |
| 12 | +Unicode character count, explicitly selected ASCII word separators, LF-based |
| 13 | +line count and a tuple of Python integer results. MOD-01 later adds its local |
| 14 | +helper import and package re-export. The selected input domain is builtin str; |
| 15 | +non-string input raises TypeError("text must be str"). String subclass overrides |
| 16 | +and arbitrary user objects are outside the first profile. |
| 17 | + |
| 18 | +| Syntax/API | First adopted contract | |
| 19 | +| --- | --- | |
| 20 | +| Scalars | None, bool, arbitrary-precision int and Unicode str literals; tuples of supported values. Floats/bytes/complex wait for separate numeric/binary contracts. | |
| 21 | +| Bindings | Simple names, immutable module constants, ordinary local assignments and top-level plain functions. Preserve lexical binding identity and evaluation order. | |
| 22 | +| Functions | Positional arguments and immutable literal defaults; return, direct calls and recursion. Keyword/variadic calls, nested closures, decorators, annotations and async functions are deferred. | |
| 23 | +| Control flow | if/elif/else, for over approved strings/tuples, break/continue, ordinary return and expression statements. while/comprehensions/generators are deferred initially. | |
| 24 | +| Operations | Supported scalar arithmetic/comparisons, Python and/or/not, identity on supported values, string membership and supported indexing. Mixed or unknown operand forms need helpers or a source-located rejection. | |
| 25 | +| Builtins/errors | len on adopted strings/tuples, print of adopted values, approved isinstance checks and explicit builtin TypeError raising needed by the fixture. No eval/exec, reflection or undeclared I/O. | |
| 26 | +| JavaScript API | Resolve explicit imports/aliases through a versioned symbol/method registry. Start with Array.of and length as a binding fixture; other exports/methods need adopted signatures and parity cases before lowering. | |
| 27 | +| Imports | Only approved JavaScript registry imports in the single-module profile. Local modules follow MOD-01; Python stdlib/third-party/native imports require explicit future adapters. | |
| 28 | + |
| 29 | +Every AST form and operation is supported, rejected or deferred explicitly. A |
| 30 | +Python-side JavaScript API existing today does not automatically make it |
| 31 | +translatable. No Date/RegExp/Map/Set expansion is a prerequisite for this compiler. |
| 32 | + |
| 33 | +The first operators are integer +, -, *, // and %, unary +/-, boolean and/or/not, |
| 34 | +adopted scalar comparisons and string membership. True division, exponentiation |
| 35 | +and bitwise operations are deferred. Indexing supports builtin strings/tuples |
| 36 | +with integer indices and Python negative-index/error behavior; slicing is |
| 37 | +deferred. Identity tests initially cover None/bool singletons and retained |
| 38 | +object aliases, not implementation-dependent integer/string interning. |
| 39 | + |
| 40 | +## Python and JavaScript semantics |
| 41 | + |
| 42 | +Preserve Python truthiness and short-circuit operand results, rather than native |
| 43 | +JavaScript truthiness for containers. Strings count and iterate Unicode code |
| 44 | +points, preserving astral characters and combining marks without normalization. |
| 45 | +Python integers retain precision beyond 2**53; use deliberate representations |
| 46 | +and conversions, not silent Number rounding. Floor division/modulo must match |
| 47 | +negative operands and Python errors. Compare values and side effects in the |
| 48 | +original order with each operand evaluated once. |
| 49 | + |
| 50 | +Tuple results remain distinguishable from JavaScript arrays in internal and |
| 51 | +transport contracts; implement required Python operations or reject them. Do not |
| 52 | +claim general tuple hashing, mixed comparisons or arbitrary object equality. |
| 53 | +Explicit JavaScript Array/String/Number/Object values retain their own approved |
| 54 | +API semantics. Crossing the two models needs an explicit conversion/signature. |
| 55 | + |
| 56 | +Defaults are definition-time values, with immutable literal restrictions in the |
| 57 | +first profile. A direct call resolves to the actual lexical binding; aliases, |
| 58 | +local shadowing and recursion must not be rewritten by spelling alone. Preserve |
| 59 | +source-visible guards and errors even when static types guide lowering. |
| 60 | + |
| 61 | +Runtime boundary values must satisfy the export/input contract. Incompatible |
| 62 | +operations raise the adopted Python error or fail validation clearly; the |
| 63 | +compiler must not emit superficially equivalent JavaScript. Unsupported async, |
| 64 | +classes, reflection, mutable module initialization and dynamic execution remain |
| 65 | +source-located diagnostics, not an interpreter/server fallback. |
| 66 | + |
| 67 | +## Front end and diagnostics |
| 68 | + |
| 69 | +Use ast -> bind -> validate -> typed IR -> lower -> emit. Never import or execute |
| 70 | +application/dependency modules to compile them. Carry original file/line/column |
| 71 | +spans and stable binding identities through every pass. Validate imports, |
| 72 | +exports, operation signatures and unsupported forms before publishing output. |
| 73 | + |
| 74 | +Diagnostic families: PJS100 unsupported syntax/feature, PJS110 unresolved or |
| 75 | +unsupported binding/import, PJS120 incompatible operation/value contract. |
| 76 | +Include code, phase, original path, one-based line and column, and a concrete |
| 77 | +message. Reject an entire unsupported compilation without partially publishing |
| 78 | +an executable artifact. Do not catch an application exception and retry a |
| 79 | +callback or silently alter evaluation order. |
| 80 | + |
| 81 | +## Output and conformance |
| 82 | + |
| 83 | +EMIT-01 produces deterministic ES modules, source maps and an explicit helper |
| 84 | +closure. Its manifest identifies compiler/language/target versions, source and |
| 85 | +output hashes, entrypoint/exports, static module imports, required helpers and |
| 86 | +notices. Application profile schema 2 is defined by pocket-python; it must not |
| 87 | +be confused with its current MicroPython runtime asset lock. |
| 88 | + |
| 89 | +Use CPython as the ordinary Python semantics oracle and native Node/browser |
| 90 | +observations for explicit JavaScript APIs. tests/fixtures/compiler_contract.json |
| 91 | +provides initial positive and rejected/deferred cases; parsing known sources |
| 92 | +for the contract does not prove a compiler already exists. PARSE-02 implements |
| 93 | +the front end and EMIT-01 must execute emitted output against that corpus. |
| 94 | + |
| 95 | +MOD-01 adds explicit local-module identity and initialization; ASYNC-01 adds |
| 96 | +supported await/Promise/cancellation behavior. BROWSER-01 integrates main-thread |
| 97 | +DOM/events and native Fetch through the versioned host protocol; QA-03 verifies |
| 98 | +CPython/Node/browser parity and source-map errors. Origin/CORS and worker message |
| 99 | +constraints remain explicit. |
| 100 | + |
| 101 | +This build-time application compiler does not execute newly entered source in |
| 102 | +the browser. That is a separate independent-interpreter milestone. Shared |
| 103 | +fixtures/host protocols do not make the two engines' IR, bytecode or value models |
| 104 | +interchangeable. |
0 commit comments