Skip to content

[self-hosting B] Close Stage-A gaps so flowc sources compile under flowc #151

Closed

Description

@godofecht

Phase B — Language gap for self-compile

Plan: docs/project/self-hosting.md

Work

Expand flowc until it parses/emits everything compiler/src/*.flow needs:

  • Real multi-file import (no skip-at-emit)
  • Enough typing / diagnostics with locations
  • Constructs used by the frontend (match, strings, richer structs as needed)
  • Shrink FLOWC_TYPECHECK=0 exceptions to documented externs only

Exit

FLOWC_BUNDLE=1 builds all of compiler/src without undocumented opt-outs.

Blocked by

Phase A (#…)

Activity

  1. added
    enhancement
    self-hosting
    on Aug 5, 2026
  2. godofecht commented on Aug 5, 2026

    @godofecht
    ContributorAuthor

    CI bring-up on #171 established the Phase-A boundary. The stable fixture/module/link roundtrip passes, while the full Phase-B path currently reaches bundle parser emission and then produces a TU missing transitive token/lexer declarations (unknown type name Lexer, missing KW_*). Full ./compiler/scripts/roundtrip.sh remains the Phase-B target; CI now runs FLOWC_PHASE_A_ONLY=1 so Phase A is protected without falsely claiming the bundle/fixed-point path is complete.

  3. godofecht commented on Aug 5, 2026

    @godofecht
    ContributorAuthor

    Likely root cause of the parser-bundle Lexer / KW_* failure: reverse-BFS emit ordered shared deps too late (lexer before token when both were imported from parser).

    Fix in #189: keep BFS gather for discovery, then Kahn topo-sort before emit/typecheck so deps are always first. CI now runs the full roundtrip again.

  4. godofecht commented on Aug 5, 2026

    @godofecht
    ContributorAuthor

    Landed in #189:

    • Bundle emit/typecheck now Kahn topo-sorts after BFS gather → parser bundle TU is green (token before lexer)
    • Self-emit/g2 resolve now -include TcCtx
    • Full roundtrip is back on CI
    • Object fixed-point (self.o vs g2.o) still drifts; CI prints per-module C diffs and continues unless FLOWC_REQUIRE_FIXED_POINT=1

    Next for Phase B: eliminate the self/g2 emit drift and re-enable hard fixed-point.

  5. godofecht commented on Aug 5, 2026

    @godofecht
    ContributorAuthor

    Follow-up #191: fixed-point now enforces self_*.c == g2_*.c (which already matched). Relocatable .o byte drift is warn-only.

  6. godofecht commented on Aug 5, 2026

    @godofecht
    ContributorAuthor

    Slice landed in upcoming PR: inferred let + capacity bumps; FLOWC_BUNDLE=1 typechecks compiler/src/main.flow. Remaining: intentional TYPECHECK=0 dogfood paths, location-rich diagnostics.

  7. godofecht commented on Aug 5, 2026

    @godofecht
    ContributorAuthor

    Slice in #212: dogfood paths keep typecheck on (no more FLOWC_TYPECHECK=0 on compile_module / self-emit / frontend bundles). Diagnostics print flowc tc: at line:col (roundtrip asserts undef → at 5:). Remaining for Phase B exit: file-path prefix on diagnostics; any leftover language gaps.

  8. godofecht commented on Aug 5, 2026

    @godofecht
    ContributorAuthor

    Slice in #213: diagnostics print flowc tc: file + path (FLOWC_IN / argv / bundle module) with line:col. Roundtrip asserts path + at 5:.

  9. godofecht commented on Aug 6, 2026

    @godofecht
    ContributorAuthor

    Phase B exit reached: all of compiler/src compiles under flowc

    Branch flowc/self-hosting-chain.

    The audit first

    FLOWC_BUNDLE=1 on every module under compiler/src, then cc -fsyntax-only -ferror-limit=0. Twelve of seventeen were already clean. The failures had exactly two causes:

    1. Inferred let had no type inference. let x = expr with no annotation emitted int32_t for every initialiser. main.flow produced 28 errors, all of the shape

      int32_t la = flowc_lexer_new(ap, 15);   // returns Lexer
      if (expect_kind((t0).kind, TOK_KEYWORD) == 0) {   // member access on an int

      This is also why the earlier stage_a_infer_call fixture was silently wrong rather than merely unsupported.

    2. String + was pointer arithmetic. claim_address, claim_path, know, math_prose, proof_sub — four errors each, all from

      return (((((addr).carrier + ".") + (addr).structure) + ".") + flowc_claim_slug_phrase((addr).law));
      //  error: invalid operands to binary expression ('const char *' and 'char[2]')

    What landed

    Inferred let. cgen infers from the initialiser: expr as T and calls to functions declared in the same module use the declared type node; string / float / struct literals write the type directly; calls into other bundle modules read a name\0ctype\0 signature table that flowc_bundle_emit fills deps-first (flowc_cgen_collect_sigs / flowc_cgen_emit_sigs, table carried on CgenBuf).

    String concatenation. a + b lowers to __flowc_str_concat(a, b), a static inline helper in the preamble that mallocs and copies. Results are never freed — process-lifetime strings, the same discipline as the compiler's other arenas. Detection is syntactic and conservative: a literal, a + chain already containing one, a cast to string, or a call declared -> string. Two string values with neither a literal nor a call between them still emit + and are rejected by cc. Loud failure, never wrong output. That limit is written down in cgen.flow and compiler/README.md.

    Fixtures that run

    Fixture Exit Covers
    stage_a_infer_struct 42 struct-returning call, cast, struct literal in one module
    bundle_infer_main + bundle_infer_lib 42 callee in a sibling module — type comes from the signature table
    stage_a_strcat 42 three concatenations read back through strcmp / strlen

    roundtrip.sh also greps the emitted C: it fails if p is typed int32_t, if "hello, " + appears, or if the let over a concat is not const char*.

    The exit condition is now a gate

    compiler/scripts/selfcompile_audit.sh, run by roundtrip.sh:

    PASS ast                3763 bytes of C, 0 diagnostics
    PASS cgen              34134 bytes of C, 0 diagnostics
    PASS claim_address     14472 bytes of C, 0 diagnostics
    ...
    PASS typecheck         26492 bytes of C, 0 diagnostics
    ALL PASS selfcompile_audit (all of compiler/src compiles under flowc)
    

    And compiler/scripts/self_host_full.sh bundles all of compiler/src into one ~195 KB translation unit. That binary is a complete flowc: bare, it runs the front-end self-tests and prints flowc: PASS; with FLOWC_IN/FLOWC_OUT it emits C. It recompiles compiler/src, and so does its child — gen1.c == gen2.c == gen3.c, gen2.o == gen3.o.

    Remaining distance

    flowc compiles the subset flowc is written in. It does not compile all of Flow. Still out: generics, effects, the DSLs, jsgen/fmt lowering of AST_MATCH, and everything src/flow/*.py supports beyond Stage-A. String concatenation between two values with no literal or call in the expression is not detected. Those belong in a Stage-B issue rather than here.

    Gates on every commit: roundtrip.sh ALL PASS (109 PASS lines, byte-identical self.o == g2.o, gen3 token compare, three whole-compiler generations); FLOWC_BUNDLE=1 typecheck of main.flow green; ./flow run compiler/src/main.flow self-tests pass; ./flow test --strict --tier2 516/516; emit_basics.sh 10/10.

  10. added 2 commits that reference this issue on Aug 9, 2026
    9e27d25
    aa21c02
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

    enhancementself-hosting

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions