Skip to content

selfhost: dynamics DSL and flow-block lowering on flowc, Python expanders retired (#681) - #999

Merged
godofecht merged 18 commits into
mainfrom
port/dynamics-dsl-flowc
Sep 29, 2026
Merged

godofecht merged 18 commits into
mainfrom
port/dynamics-dsl-flowc

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Fixes #681

What

The dynamics DSL and flow blocks (evolves, when ... reaches, every, solver, always/never, recognize, connect, nested flows, |> flow stages) now expand inside flowc. Programs that use them compile on the flowc host with no Python.

  • compiler/src/flow_blocks.flow (new, about 5,600 lines) is src/flow/flow_blocks.py plus the flow grammar from parser.py. It follows the Python lexer's token rules and column arithmetic, parses the flow body and the expressions in it, runs every validation in the original order, and lowers each flow to the same struct and Name_new / _init / _derivs / _step / _default_dt / _outputs / _check functions. It writes them as Flow source in place of the block: one declaration per line, ; between statements, padded to the block's line count so later lines keep their numbers.
  • compiler/src/dynamics_dsl.flow is now the whole of dynamics_dsl.py with the _dynamics_dsl_fixes.py corrections applied. The output is byte-identical to the Python expander.
  • flowc runs Field DSL, then dynamics DSL, then flow blocks on every source read (flowc_expand_stages_in_place in resolve.flow, used by main.flow, driver.flow and all bundle passes). FLOWC_EXPAND_ONLY takes field, dynamics, flow, or 1 for all.
  • cgen: a binary operand of a unary operator gets its own parentheses (!(a < b) was emitted as !a < b), and flow_panic is an intrinsic, accepted by the typechecker.
  • Python: dynamics_dsl.py and flow_blocks.py are thin bridges to flowc, and _dynamics_dsl_fixes.py is deleted. Parser.parse asks flowc for the lowered source and keeps each FlowDecl and its recognition manifest on the lowered struct for tooling. Net Python in src/flow: 143 lines added, 3,565 removed.

This branch builds on #956 (Field DSL port, still open) and carries its commits minus its bootstrap regeneration. Merge #956 first; the regenerated C here is from the combined tree.

Parity

compiler/scripts/parity_flow_blocks.sh --python 7527ab37:

fixtures: pass=107 fail=0
programs: pass=4 fail=0
corpus: untouched=2181 lowered=117 diagnosed=78
live python@7527ab37: same trees=2312 same errors=94 skipped=77 fail=0
e2e: bouncing_ball_evolves built by ./flow compile on flowc, exit 0, no python call
parity_flow_blocks: PASS

The Python lowering produced AST, so the gate compares parsed results. The Python parser at the given revision lowers the original source and parses flowc's expansion, and the two declaration lists must be equal (positions and has_self aside). Diagnostics must match the Python text: message, line, column and hint. 107 fixtures (21 expansions, 86 errors) carry goldens recorded from the Python. Two run fixtures carry the Python host's program output, and tests/lang/test_time_blocks.flow and test_hybrid_events.flow now pass on flowc. Separately, 36 of the 38 flow examples that build on both hosts print the same output. rc_rl_rlc differs because of #993, and chemostat is identical but slower than the 10 s limit at -O1.

compiler/scripts/parity_dynamics_dsl.sh --python 7527ab37:

fixtures: pass=94 fail=0
passthrough: files=2382 (dsl files expanded: 9)
live python@7527ab37: files=2485 fail=0
e2e: ga_dsys_syntax.flow exit 0 on flowc, no python call
parity_dynamics_dsl: PASS

The dynamics e2e sets FLOWC_TYPECHECK=0 because of #988.

parity_field_dsl.sh still passes (21 fixtures, 2007 files untouched); it now asks for the field stage alone.

Bootstrap

Regenerated last, in its own commit, per AGENTS.md from the Python host emit. Fixed point OK. bootstrap_from_c.sh --verify, self_host_full.sh (three generations byte-identical) and roundtrip.sh pass. Bootstrap suite (tests/lang, FLOWC_IN/FLOWC_OUT, cc -Itests/lang): 121 pass, 17 fail, against 119/19 on main. The two new passes are the flow-block tests and nothing regressed. Python unit suite: 1798 passed, 4 skipped.

Not changed

docs/project/python-in-flow.md, docs/vision/north-star.md and docs/AI_FLOW_HANDBOOK.md still name the Python modules. The commit hook rejects those files for dashes that were already there, so they are left for a separate pass.

Gaps found: #988 (bundle typecheck and unexported stdlib functions), #992 (println of an f64 struct field), #993 (print takes a type from another function's parameter).

…wc source read

FLOWC_EXPAND_ONLY now takes the stages to run (field, dynamics, flow, or
1 for all). dynamics_dsl and flow_blocks are new leaf frontend modules;
their expanders are stubs until the ports land.
compiler/src/flow_blocks.flow is src/flow/flow_blocks.py plus the flow
grammar from parser.py: the Python lexer's token rules and column
arithmetic, the flow body grammar, the expression grammar the flow items
use, pipeline stages, every validation in its original order, and the
lowering to Name_new/_init/_derivs/_step/_default_dt/_outputs/_check.

The lowering is emitted as Flow source in place of the block, one
declaration per line with ';' between statements, padded to the block's
line count so later lines keep their numbers.

tests/lang/test_time_blocks.flow and test_hybrid_events.flow now build
and pass on flowc.
compiler/src/dynamics_dsl.flow is now the whole of expand_dynamics_dsl
from src/flow/dynamics_dsl.py, with the two corrections from
_dynamics_dsl_fixes.py applied (raw dsys validation, setup injected on its
own line). Detection, represent extraction, every dsys / horizon / sense /
ga evolve / closed / analyze / wfc field / couple / guide form, dynamics { }
blocks, code generation and injection. Output is byte-identical to the
Python, including its quirks and its ValueError, IndexError and KeyError
text. Floats are formatted from the exact binary value (%.17g and repr),
so no variadic C call is involved.
compiler/scripts/parity_dynamics_dsl.sh holds flowc to 94 fixtures whose
goldens were recorded from the Python expander at 7527ab37 (27 expansions,
67 errors, including the nine tracked programs that use the DSL), checks
that every other tracked .flow passes through untouched, diffs live against
the Python at any revision with --python, and builds and runs
examples/dynamics/ga_dsys_syntax.flow on flowc with python stubbed out.
The end-to-end run skips flowc's bundle typecheck until #988 is fixed.
compiler/scripts/parity_flow_blocks.sh parses the Python lowering and
flowc's expansion with the Python parser at a git revision and requires
equal declaration trees, and equal diagnostics (message, line, column,
hint). 107 fixtures (21 expansions, 86 errors) carry goldens recorded
from the Python at 7527ab37, two run fixtures carry the Python host's
program output, and bouncing_ball_evolves builds with ./flow compile on
flowc with python stubbed out.

The flowc typechecker accepts flow_panic, the intrinsic an always/never
block lowers to.
dynamics_dsl.py and flow_blocks.py now call flowc's FLOWC_EXPAND_ONLY
mode (dynamics, flow) through the runner field_dsl.py already had, and
_dynamics_dsl_fixes.py is gone. Parser.parse lowers flow blocks by asking
flowc for the expanded source, then attaches each FlowDecl and its
recognition manifest to the lowered struct for tooling. field_dsl.py asks
for the field stage alone, now that FLOWC_EXPAND_ONLY=1 runs every stage.

The dynamics tests assert on the expansion instead of the removed Python
program objects; the byte-level check is parity_dynamics_dsl.sh.

flow-driver bundles programs that use the dynamics DSL, which adds
stdlib imports. parser.py comments lose their em dashes (the prose hook
lints whole staged files).
…fter parse

The word 'flow' followed by a name and a brace inside a comment sent the
compiler's own modules through flowc. The cheap check now looks at line
starts, as the other DSL checks do, and a flow block it misses is lowered
by flowc once the parse sees a FlowDecl.
FLOWC_EXPAND_ONLY=1 now runs every stage, so flow blocks and the dynamics
DSL counted as passthrough failures.
The run_*.out goldens were matched by the *.out ignore rule, so a clean
checkout had none and parity_flow_blocks.sh failed both run fixtures.
Recorded with --write-golden 7527ab37 and unignored.
The Python parser now hands flow-block expansion to flowc. On a clean
runner the first such fuzz input built flowc_bootstrap with cc inside the
5 second per-input limit and was reported as a hang (repro 'flow Osc {').
With flowc built first the input is a clean parse error.
Every roundtrip step transpiles main.flow on the Python host. With the
dynamics DSL and flow-block modules a step went from about 17s to 23s and
the whole run from 15 to over 20 minutes, so the 20 minute timeout killed
it twice during compile_module driver.
#971 changed a comment in docs/vision/examples/pendulum.flow from
verify_examples.py to verify_examples.sh. The ex_ fixture must match its
source byte for byte.
@godofecht
godofecht force-pushed the port/dynamics-dsl-flowc branch from e44dec7 to da0cc45 Compare September 29, 2026 07:10
godofecht added a commit that referenced this pull request Sep 29, 2026
Rebased onto main containing #1003, #999, #998 and #989. Carries #1001's
parse-only mode, located parse errors (flowc_parser_print_error, which the
driver, emit and bundle paths now use), theorem/assume/therefore/claim
references, flow blocks, handle, ui layout and pipeline blocks, let without
initializer, contextual unit, distinct, effect rows, or-patterns, guards,
nested patterns, [T; N], capability types, ?, vector literals, soft names,
sort-by keys, entropy policies and module blocks, the parse coverage script
and its CI floor, and the Python-grammar parser contract test. The
#1001/#1003 reconciliation follows the one in #1012.
@godofecht
godofecht merged commit eec7463 into main Sep 29, 2026
26 checks passed
godofecht added a commit that referenced this pull request Sep 29, 2026
Rebased onto main containing #1003, #999, #998 and #989. Carries #1001's
parse-only mode, located parse errors (flowc_parser_print_error, which the
driver, emit and bundle paths now use), theorem/assume/therefore/claim
references, flow blocks, handle, ui layout and pipeline blocks, let without
initializer, contextual unit, distinct, effect rows, or-patterns, guards,
nested patterns, [T; N], capability types, ?, vector literals, soft names,
sort-by keys, entropy policies and module blocks, the parse coverage script
and its CI floor, and the Python-grammar parser contract test. The
#1001/#1003 reconciliation follows the one in #1012.
godofecht added a commit that referenced this pull request Sep 29, 2026
Rebased onto main containing #1003, #999, #998 and #989. Carries #1001's
parse-only mode, located parse errors (flowc_parser_print_error, which the
driver, emit and bundle paths now use), theorem/assume/therefore/claim
references, flow blocks, handle, ui layout and pipeline blocks, let without
initializer, contextual unit, distinct, effect rows, or-patterns, guards,
nested patterns, [T; N], capability types, ?, vector literals, soft names,
sort-by keys, entropy policies and module blocks, the parse coverage script
and its CI floor, and the Python-grammar parser contract test. The
#1001/#1003 reconciliation follows the one in #1012.
godofecht added a commit that referenced this pull request Sep 29, 2026
With #999 on main, flowc expands dyn. blocks, so the server indexes
dynamics files the way it compiles them.
godofecht added a commit that referenced this pull request Sep 29, 2026
With #999 on main, flowc expands dyn. blocks, so the server indexes
dynamics files the way it compiles them.
godofecht added a commit that referenced this pull request Sep 29, 2026
With #999 on main, flowc expands dyn. blocks, so the server indexes
dynamics files the way it compiles them.
godofecht added a commit that referenced this pull request Sep 29, 2026
With #999 on main, flowc expands dyn. blocks, so the server indexes
dynamics files the way it compiles them.
godofecht added a commit that referenced this pull request Sep 29, 2026
With #999 on main, flowc expands dyn. blocks, so the server indexes
dynamics files the way it compiles them.
@godofecht
godofecht deleted the port/dynamics-dsl-flowc branch October 1, 2026 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Self-Hosting Phase 2] Port Hybrid Dynamics DSL and Time Blocks to Flow

1 participant