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

@@ -728,6 +728,20 @@ cat > "$TMPFILE" << 'EPOCHS'
(epoch 1105)
(eval "(ocaml-run-program \"let r = { x = 1; y = 2 };; r.x + r.y\")")
;; ── as / when in match ─────────────────────────────────────────
(epoch 1200)
(eval "(ocaml-run \"match Some 5 with | Some x as p -> x | None -> 0\")")
(epoch 1201)
(eval "(ocaml-run \"match 5 with | n when n > 0 -> 1 | n when n < 0 -> -1 | _ -> 0\")")
(epoch 1202)
(eval "(ocaml-run \"match (-3) with | n when n > 0 -> 1 | n when n < 0 -> -1 | _ -> 0\")")
(epoch 1203)
(eval "(ocaml-run \"match 0 with | n when n > 0 -> 1 | n when n < 0 -> -1 | _ -> 0\")")
(epoch 1204)
(eval "(ocaml-run \"match (Some 7) with | None -> 0 | Some x when x > 5 -> x * 10 | Some x -> x\")")
(epoch 1205)
(eval "(ocaml-run \"match (Some 3) with | None -> 0 | Some x when x > 5 -> x * 10 | Some x -> x\")")
EPOCHS
OUTPUT=$(timeout 180 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
@@ -1153,6 +1167,14 @@ check 1103 "record string field" '"Bob"'
check 1104 "record int field" '30'
check 1105 "top-level record decl" '3'
# ── as / when in match ──────────────────────────────────────────
check 1200 "Some x as p" '5'
check 1201 "when sign +" '1'
check 1202 "when sign -" '-1'
check 1203 "when sign 0" '0'
check 1204 "when guard fires" '70'
check 1205 "when guard skips" '3'
TOTAL=$((PASS + FAIL))
if [ $FAIL -eq 0 ]; then
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"