ocaml: phase 3 pattern matching + constructors (+18 tests, 183 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 56s

Constructor app: (:app (:con NAME) arg) -> (NAME …args). Tuple args
flatten so Pair(a,b) -> ("Pair" a b), matching the parser's pattern
flatten. Standalone (:con NAME) -> (NAME) nullary.

Pattern matcher: :pwild, :pvar, :plit, :pcon (head + arity), :pcons
(decompose), :plist (length match), :ptuple (after tuple tag). Match
walks clauses until first success; runtime error on exhaustion.
Recursive list functions (len, sum, fact) work end-to-end.
This commit is contained in:
2026-05-08 08:02:56 +00:00
parent 4dca583ee3
commit 9b833a9442
3 changed files with 237 additions and 7 deletions

View File

@@ -430,6 +430,57 @@ cat > "$TMPFILE" << 'EPOCHS'
(epoch 495)
(eval "(ocaml-run \"let f x = x * 2 in 5 |> f\")")
;; ── Phase 3: ADTs + match (eval) ───────────────────────────────
;; Constructors
(epoch 500)
(eval "(ocaml-run \"None\")")
(epoch 501)
(eval "(ocaml-run \"Some 42\")")
(epoch 502)
(eval "(ocaml-run \"Some (1, 2)\")")
;; Match — option
(epoch 510)
(eval "(ocaml-run \"match Some 5 with | None -> 0 | Some y -> y\")")
(epoch 511)
(eval "(ocaml-run \"match None with | None -> 0 | Some y -> y\")")
;; Match — literals
(epoch 520)
(eval "(ocaml-run \"match 3 with | 1 -> 100 | 2 -> 200 | _ -> 999\")")
(epoch 521)
(eval "(ocaml-run \"match true with | true -> 1 | false -> 0\")")
(epoch 522)
(eval "(ocaml-run \"match \\\"hi\\\" with | \\\"hi\\\" -> 1 | _ -> 0\")")
;; Match — tuples
(epoch 530)
(eval "(ocaml-run \"match (1, 2) with | (a, b) -> a + b\")")
(epoch 531)
(eval "(ocaml-run \"match (1, 2, 3) with | (a, b, c) -> a * b * c\")")
;; Match — list cons / nil
(epoch 540)
(eval "(ocaml-run \"match [1; 2; 3] with | [] -> 0 | h :: _ -> h\")")
(epoch 541)
(eval "(ocaml-run \"match [] with | [] -> 0 | h :: _ -> h\")")
(epoch 542)
(eval "(ocaml-run \"match [1; 2; 3] with | [a; b; c] -> a + b + c | _ -> 0\")")
(epoch 543)
(eval "(ocaml-run \"let rec len lst = match lst with | [] -> 0 | _ :: t -> 1 + len t in len [1; 2; 3; 4; 5]\")")
(epoch 544)
(eval "(ocaml-run \"let rec sum lst = match lst with | [] -> 0 | h :: t -> h + sum t in sum [1; 2; 3; 4; 5]\")")
;; Match — wildcard + var
(epoch 550)
(eval "(ocaml-run \"match 99 with | _ -> 1\")")
(epoch 551)
(eval "(ocaml-run \"match 99 with | x -> x + 1\")")
;; Constructors with tuple args
(epoch 560)
(eval "(ocaml-run \"match Pair (1, 2) with | Pair (a, b) -> a * b\")")
EPOCHS
OUTPUT=$(timeout 60 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
@@ -683,6 +734,39 @@ check 492 "run-prog inc + double" '10'
# pipe
check 495 "eval x |> f" '10'
# ── Phase 3: ADTs + match (eval) ────────────────────────────────
# constructors
check 500 "eval None" '("None")'
check 501 "eval Some 42" '("Some" 42)'
check 502 "eval Pair tuple-arg" '("Some" 1 2)'
# option match
check 510 "match Some 5 -> 5" '5'
check 511 "match None -> 0" '0'
# literal match
check 520 "match 3 -> _ -> 999" '999'
check 521 "match bool true" '1'
check 522 "match string lit" '1'
# tuple match
check 530 "match (1,2)" '3'
check 531 "match (1,2,3)" '6'
# list match
check 540 "match list cons head" '1'
check 541 "match empty list" '0'
check 542 "match list literal pat" '6'
check 543 "match recursive len" '5'
check 544 "match recursive sum" '15'
# wildcard + var
check 550 "match _ -> 1" '1'
check 551 "match x -> x+1" '100'
# ctor with tuple arg
check 560 "Pair(a,b) → a*b" '2'
TOTAL=$((PASS + FAIL))
if [ $FAIL -eq 0 ]; then
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"