Skip to content

Documentation: Clarify codegen environment rules for top-level const and function bindings #89

Description

@hyperpolymath

Describe the bug
Documentation needs to clarify that both top-level consts and fns must be present in the codegen environment. Current docs may only discuss top-level fns.

To Reproduce
Look for existing documentation on module environment in codegen.

Expected behavior
Section covering environment population rules for codegen makes explicit how top-level const and function bindings are represented and threaded.

Additional context

Activity

  1. added
    bugSomething is broken or behaves incorrectly
    on May 10, 2026
  2. added a commit that references this issue on May 11, 2026
    c392e49
  3. hyperpolymath commented on May 11, 2026

    @hyperpolymath
    OwnerAuthor

    Resolved by PR #91 (merged as c392e49).

    The SPEC.md changes:

    • Section 2.1 (Program Structure): top_level production now lists const_decl, extern_fn_decl, and extern_type_decl alongside fn_decl so the grammar matches what the parser actually accepts after PR feat: typed-wasm xmod + Node backend + extern + VS Code (#35, #42) #90 landed extern support.
    • Section 2.9 (Constant Declarations): new EBNF and prose noting that const initializers must reduce to Wasm constant expressions, with a forward-reference to section 8 for the codegen environment encoding.
    • Section 2.10 (Extern Declarations): new EBNF for extern fn / extern type, with the runtime contract (extern fn → (import "env" "<name>" ...), extern type → no Wasm artifact).
    • Section 8 (Codegen Module Environment): new section documenting the func_indices : (string * int) list table. Positive k ≥ 0 is a Wasm function index (defined functions or extern-imported functions); negative k < 0 encodes -(global_idx + 1) for top-level constants. Includes per-case gen_decl behaviour for TopFn (defined), TopFn with FnExtern body, and TopConst. Explicitly notes the call-site ExprApp lookup currently emits call k unconditionally — decoding the negative sentinel back to global.get is the implementation half tracked under Codegen.UnboundVariable for top-level const bindings (typecheck OK, compile fails) #73.

    The companion lib/codegen.ml doc-comment on the func_indices field was updated to mention both TopFn paths (defined and extern) and that insertion is in source declaration order.

    Note: PR #94 introduces a more detailed docs/specs/codegen-environment.adoc that goes deeper on cross-module threading and non-WASM target behaviour. That landing will subsume the SPEC.md §8 content here; this issue is resolved at the level it asked for (clarify that both consts and fns must be present in the codegen environment, document the population rules).

  4. added 2 commits that reference this issue on May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething is broken or behaves incorrectly

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions