Skip to content

flowc: lower match patterns, ui layout, pipeline forks, declarative sort, [T; N], ?, vectors, module and unit (#996) - #1012

Merged
godofecht merged 11 commits into
mainfrom
selfhost/lower-patterns
Sep 29, 2026
Merged

godofecht merged 11 commits into
mainfrom
selfhost/lower-patterns

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Part of #996 and #678. flowc now lowers the parse-only constructs outside proofs, effects and flow blocks. Each one follows the Python host's lowering.

Based on #1001 and #1003, which are merged in because both are still open. Review after them, or read from a02dfdc2.

Constructs

construct Python reference tracked files unblocked
match or-patterns, guards, nested and literal struct fields, variant patterns on an enum value c_generator._gen_match 5 (tests/core/test_match_{nested,advanced,enum_exhaustive}, test_coverage_match_deep, the new fixture)
ui_layout / ui_row / ui_column / ui_stack / ui_grid, _ui_state, ui_layout_bind _gen_layout 10 (tests/core/test_ui_layout*, examples/ui/layout_hello, 4 demos)
pipeline fork src |> R { a = f }, anonymous fork, |> choose sel { ... }, the _ pipe slot fork_records.py, _insert_pipe_arg 7 (examples/basics/pipeline_{fork,fork_inferred,choose,placeholder}, book/06_pipeline_result, flow_way/sensor_calibration)
|> sort, sort by keys, sortBy, order, with entropy, unique, descending _gen_sort_expr, _sort_cmp_fragment, _scalar_cmp 1 (examples/basics/declarative_sort; its sortBy [desc .score, asc .name] used to sort by the first field only)
[T; N] array<T, N> 2 (benchmarks/aosoa, cross_harness arrays)
expr? TryExpr 1 (tests/core/test_production_features, with module)
<a, b, c> and vecN<T> VectorLiteral, _c_type 2 (tests/simd/*)
module Name { ... } flatten_module_declarations shared with ? above
unit Name [= ...] f64 at run time 3 (tests/core/test_units_basic, test_coverage_units, doc preamble vision-units)
distinct type already a plain alias on both hosts 0

Flow stage parameters x |> Stage { k: v } still refuse: they are part of flow blocks, which this PR does not lower (75 files, src/flow/flow_blocks.py).

Tracked files that compile on flowc

Bundle emit plus cc -c, over every tracked .flow file the Python parser accepts, on this tree:

The 36 include cgen.flow, main.flow, driver.flow, resolve.flow and examples/systems/tiny_pointers.flow, which need the 1 MiB source buffers now that cgen.flow is past 256 KiB.

Parity gate

compiler/scripts/parity_lowering.sh holds 66 tracked files (every file above plus every match, unit and distinct user) to the Python host: same accept/reject, the Python diagnostic for rejects, and the same exit code and stdout for programs that link. Goldens are recorded from ff99639; --python ff99639e repeats it live.

golden:          files=66 accept/reject=47 run=38 diagnostics=2 known-divergent=19 fail=0
python@ff99639e: files=66 accept/reject=47 run=38 diagnostics=2 known-divergent=19 fail=0

The 19 known divergences are outside this work and listed with reasons in compiler/parity_lowering/divergent.txt:

The gate is not wired into CI in this PR. It needs only cc and runs in about a minute.

Python removed

None. fork_records.py, the sort plans and _gen_match / _gen_layout still serve the MLIR backend, the Python C path that flow-examples CI runs with FLOW_HOST=python, and tests/integration/test_pipeline_examples.py. They can go once those move off the Python host. The ratchet passes and did not grow.

Other changes

Verification

  • Bootstrap C was regenerated last, in its own commit. The Python-host emit equals the flowc self-emit and the fixed point holds.
  • bootstrap_from_c.sh --verify, self_host_full.sh, roundtrip.sh, parse_coverage.sh --check, parity_field_dsl.sh and parity_lowering.sh pass.
  • Bootstrap suite (tests/lang): 147 pass, 12 fail. The same 12 fail without this PR (flow blocks, effects, external headers).

Follow-ups

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
godofecht force-pushed the selfhost/lower-patterns branch from 994e8e3 to e6c84ca Compare September 29, 2026 07:52
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
…s lowered

test_parser_python_forms asserted that handle blocks, or-patterns, effect
rows and fork blocks stay parse-only. #1012 lowers or-patterns and forks
and #1014 lowers handle and effect rows, so the test exited 7 (6 with
both). It now checks that each form parses to its node and is lowered, and
exits 0 on flowc and the Python host alike. The parity_lowering golden
follows.
Match arms with or-patterns, guards, nested struct patterns with literal
fields, and variant patterns on an enum value now lower in Stage-A the
way c_generator._gen_match does: an if/else chain whose condition ORs
the alternatives' checks, a guard that sees the first alternative's
bindings through a statement expression, and bindings typed with
__typeof__ from the matched field.

The or-pattern fixture that roundtrip.sh used as a negative case is now
a positive case, stage_a_match_ext (exit 42).

Unblocks 5 tracked files (tests/core/test_match_{nested,advanced,
enum_exhaustive}.flow, test_coverage_match_deep.flow and the fixture);
each builds and runs with the same output and exit code as the Python
host.
cgen.flow is about to pass 256 KiB, the old per-module source buffer.
Bundle mode, the single-file emit path and the Stage-A driver now read
up to 1 MiB per module.
ui_layout / ui_row / ui_column / ui_stack / ui_grid blocks lower the way
c_generator._gen_layout does: <word>_begin(args), the body, then
<word>_end(first arg). Unless the first argument is a pointer or array
variable, the implicit _ui_state pointer goes first. A module that uses
it gets the guarded global. ui_layout_bind(p) and an undefined
i32_to_f32(v) lower inline as the Python host's intrinsics do.

tests/core/test_ui_layout{,_std,_positions,_snapshot}.flow and
examples/ui/layout_hello.flow now build on flowc with the same run
output and exit code as the Python host (test_ui_layout and
test_ui_layout_std segfault identically on both hosts).
src |> Record { a = f, ... }, the anonymous src |> { a = f, ... } and
src |> choose sel { A => f, ... } now lower the way
src/flow/fork_records.py desugars them. Each block is one GNU statement
expression that binds the source once; a fork builds its record (an
inline struct typed from the branches for the anonymous form) and a
choose assigns the selected arm through the general match lowering.
Branches parse as pipelines over a placeholder source, and a bare _
argument now marks the pipe slot (x |> f(1, _)) as in _insert_pipe_arg.
The typecheck rejects an uninferrable anonymous field or choose with
the Python messages. Flow stage parameters stay with flow blocks.

examples/basics/pipeline_{fork,fork_inferred,choose}.flow now build on
flowc with the same output and exit code as the Python host.
Every |> sort, |> sortBy and |> order is now a declarative sort, as the
Python parser's _at_declarative_sort claims them. It sorts the
fixed-size array in place with a stable bottom-up merge sort, keys
compared lexicographically with per-key and global direction, and
unique compacts the prefix, as c_generator._gen_sort_expr and
_sort_cmp_fragment do. Compares follow _scalar_cmp: strcmp for strings,
IEEE totalOrder for floats, < and > otherwise. Entropy and speed
policies parse and change nothing, as in the Python host (the plan
choice there changes speed only).

examples/basics/declarative_sort.flow now builds on flowc; before, its
sortBy [desc .score, asc .name] sorted by the first field only and
printed a different order. It and the tests/lang sort tests match the
Python host's output.
The Python type form [T; N] is array<T, N>. The parser now marks its
AST_TYPE with AST_TYPE_FIXED_ARRAY, and cgen asks flowc_cgen_ty_is_array
wherever it matched the array name span, so the nameless node emits as
T name[N] and is no longer mistaken for the [T] span sugar.

benchmarks/cross_harness/runtime/arrays/arrays.flow builds and runs with
the Python host's exit code; benchmarks/aosoa.flow compiles and links.
…#996)

- expr? binds the Result once, returns it unless is_ok and yields
  .value, as c_generator's TryExpr does (pending defers run first).
- <a, b, c> is a GCC/Clang vector literal whose lane type comes from
  the first element, and vecN<T> is T __attribute__((vector_size)),
  both as in the Python host. return <a, b> now parses as a value.
- module Name { ... } declarations join the top level, as
  flatten_module_declarations does.

tests/core/test_production_features.flow and tests/simd/test_{simple_simd,
simd_vector}.flow now build on flowc with the Python host's output and
exit code.
A top-level unit Name [= ...] is an f64 at run time, as in the Python
host, so it now lowers to typedef double Name, emitted before structs
whose fields may use it. flowc does not check dimensions yet (#1008).

tests/core/test_units_basic.flow and test_coverage_units.flow now build
on flowc and match the Python host's output and exit code.
compiler/scripts/parity_lowering.sh builds 66 tracked files that use
the newly lowered constructs, match, unit or distinct declarations on
flowc, runs the ones that link, and holds accept/reject, the rejection
diagnostic, the exit code and the stdout to the Python host. Goldens in
compiler/parity_lowering/expected.tsv come from ff99639; --python <rev>
repeats the comparison live and --write-golden rewrites them.

Result on flowc and live against python@ff99639e: 66 files, 47 same
accept/reject, 38 same exit code and output, 2 same diagnostics, 19
known divergences outside this work (listed with reasons in
divergent.txt; #1006, #1007), 0 failures.

New: tests/lang/test_lowered_forms.flow exercises each construct and
passes on both hosts; two negative fixtures pin the fork and choose
inference messages.
cgen.flow now passes 256 KiB, so roundtrip.sh's g2_emit of cgen through
the C host driver ran out of source buffer. The output buffer grows to
4 MiB to match.
@godofecht
godofecht force-pushed the selfhost/lower-patterns branch from e6c84ca to e2bd4cb Compare September 29, 2026 08:48
@godofecht
godofecht merged commit b380c42 into main Sep 29, 2026
10 checks passed
godofecht added a commit that referenced this pull request Sep 29, 2026
…s lowered

test_parser_python_forms asserted that handle blocks, or-patterns, effect
rows and fork blocks stay parse-only. #1012 lowers or-patterns and forks
and #1014 lowers handle and effect rows, so the test exited 7 (6 with
both). It now checks that each form parses to its node and is lowered, and
exits 0 on flowc and the Python host alike. The parity_lowering golden
follows.
godofecht added a commit that referenced this pull request Sep 29, 2026
Effect declarations, capabilities, handle blocks, effect rows and E.op calls lower in flowc (effects.flow) with the Python host's diagnostics, gated by parity_effects.sh. Merged over #1012: AST_TYPE_CAPABILITY is 3 (FIXED_ARRAY is 2), effect rows and capability types join flowc_ast_parse_only_lowered, and the parser contract test expects handle, or-patterns, rows and forks lowered. CI passed on 0510995, whose tree (4902590) is identical to this rebased head. Bootstrap C regenerated (fixed point, bootstrap_from_c --verify, self_host_full, parity_effects and parity_lowering pass).
@godofecht
godofecht deleted the selfhost/lower-patterns 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.

1 participant