ocaml: phase 3 'as' alias + 'when' guard in match (+6 tests, 295 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 50s

Pattern parser top wraps cons-pat with 'as ident' -> (:pas PAT NAME).
Match clause parser consumes optional 'when GUARD-EXPR' before -> and
emits (:case-when PAT GUARD BODY) instead of :case.

Eval: :pas matches inner pattern then binds the alias name; case-when
checks the guard after a successful match and falls through to the next
clause if the guard is false.

Or-patterns deferred — ambiguous with clause separator without
parens-only support.
This commit is contained in:
2026-05-08 12:28:07 +00:00
parent 7fb65cd26a
commit 851e0585cf
4 changed files with 84 additions and 11 deletions

View File

@@ -136,9 +136,11 @@ SX CEK evaluator (both JS and OCaml hosts)
bindings.)_
- [~] **Patterns:** constructor (nullary + with args, incl. flattened tuple
args `Pair (a, b)``(:pcon "Pair" PA PB)`), literal (int/string/char/
bool/unit), variable, wildcard `_`, tuple, list cons `::`, list literal.
_(Pending: record patterns, `as` binding, or-pattern `P1 | P2`, `when`
guard.)_
bool/unit), variable, wildcard `_`, tuple, list cons `::`, list
literal, `as` binding (`pat as name`). Match clauses support `when`
guard via `(:case-when PAT GUARD BODY)`. _(Pending: record patterns,
or-pattern `P1 | P2` — ambiguous with clause separator without
lookahead.)_
- [ ] OCaml is **not** indentation-sensitive — no layout algorithm needed.
- [ ] Tests in `lib/ocaml/tests/parse.sx` — 50+ round-trip parse tests.
@@ -355,6 +357,14 @@ the "mother tongue" closure: OCaml → SX → OCaml. This means:
_Newest first._
- 2026-05-08 Phase 3 — `as` aliases + `when` guards in match (+6 tests,
295 total). Parser: pattern parser wraps with `as ident` → `(:pas
PAT NAME)`. Match's `one` consumes optional `when GUARD-EXPR` → emits
`(:case-when PAT GUARD BODY)` instead of `:case`. Eval `:pas` matches
inner pattern then also binds the alias name; `case-when` checks the
guard after a successful match and falls through if false. Or-pat
`(P1 | P2)` deferred — ambiguous with clause separator without
parens-only support.
- 2026-05-08 Phase 1+2 — record literals `{ x = 1; y = 2 }` and
functional update `{ r with x = 99 }`. Parser produces `(:record (F E)
...)` and `(:record-update BASE-EXPR (F E) ...)`. Eval builds a dict