ocaml: phase 1+3 'when' guard in 'function | pat -> body' (+3 tests, 464 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
parse-function now consumes optional 'when GUARD-EXPR' before -> and emits (:case-when PAT GUARD BODY) — same handling as match clauses. function-style sign extraction now works: (function | n when n > 0 -> 1 | n when n < 0 -> -1 | _ -> 0)
This commit is contained in:
@@ -787,7 +787,7 @@
|
|||||||
(list :try expr cases)))))))
|
(list :try expr cases)))))))
|
||||||
(define parse-function
|
(define parse-function
|
||||||
(fn ()
|
(fn ()
|
||||||
;; `function | pat -> body | …` ≡ fun x -> match x with | pat -> body
|
;; `function | pat [when GUARD] -> body | …`
|
||||||
(let ()
|
(let ()
|
||||||
(begin
|
(begin
|
||||||
(when (at-op? "|") (advance-tok!))
|
(when (at-op? "|") (advance-tok!))
|
||||||
@@ -795,11 +795,18 @@
|
|||||||
(begin
|
(begin
|
||||||
(define one
|
(define one
|
||||||
(fn ()
|
(fn ()
|
||||||
(let ((p (parse-pattern)))
|
(let ((p (parse-pattern)) (guard nil))
|
||||||
(begin
|
(begin
|
||||||
|
(when (at-kw? "when")
|
||||||
|
(begin (advance-tok!)
|
||||||
|
(set! guard (parse-expr-no-seq))))
|
||||||
(consume! "op" "->")
|
(consume! "op" "->")
|
||||||
(let ((body (parse-expr)))
|
(let ((body (parse-expr)))
|
||||||
(append! cases (list :case p body)))))))
|
(cond
|
||||||
|
((= guard nil)
|
||||||
|
(append! cases (list :case p body)))
|
||||||
|
(else
|
||||||
|
(append! cases (list :case-when p guard body)))))))))
|
||||||
(one)
|
(one)
|
||||||
(define loop
|
(define loop
|
||||||
(fn ()
|
(fn ()
|
||||||
|
|||||||
@@ -1140,6 +1140,14 @@ cat > "$TMPFILE" << 'EPOCHS'
|
|||||||
(epoch 4306)
|
(epoch 4306)
|
||||||
(eval "(ocaml-run \"Char.is_lower \\\"a\\\"\")")
|
(eval "(ocaml-run \"Char.is_lower \\\"a\\\"\")")
|
||||||
|
|
||||||
|
;; ── function with `when` guard ────────────────────────────────
|
||||||
|
(epoch 4400)
|
||||||
|
(eval "(ocaml-run \"(function | n when n > 0 -> 1 | _ -> 0) 5\")")
|
||||||
|
(epoch 4401)
|
||||||
|
(eval "(ocaml-run \"(function | n when n > 0 -> 1 | _ -> 0) (-3)\")")
|
||||||
|
(epoch 4402)
|
||||||
|
(eval "(ocaml-run \"(function | n when n > 0 -> 1 | n when n < 0 -> -1 | _ -> 0) 0\")")
|
||||||
|
|
||||||
EPOCHS
|
EPOCHS
|
||||||
|
|
||||||
OUTPUT=$(timeout 180 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
|
OUTPUT=$(timeout 180 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
|
||||||
@@ -1805,6 +1813,11 @@ check 4304 "Char.is_whitespace ' '" 'true'
|
|||||||
check 4305 "Char.is_upper A" 'true'
|
check 4305 "Char.is_upper A" 'true'
|
||||||
check 4306 "Char.is_lower a" 'true'
|
check 4306 "Char.is_lower a" 'true'
|
||||||
|
|
||||||
|
# ── function with `when` guard ─────────────────────────────────
|
||||||
|
check 4400 "function when 5" '1'
|
||||||
|
check 4401 "function when -3" '0'
|
||||||
|
check 4402 "function sign 0" '0'
|
||||||
|
|
||||||
TOTAL=$((PASS + FAIL))
|
TOTAL=$((PASS + FAIL))
|
||||||
if [ $FAIL -eq 0 ]; then
|
if [ $FAIL -eq 0 ]; then
|
||||||
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"
|
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"
|
||||||
|
|||||||
@@ -407,6 +407,9 @@ _Newest first._
|
|||||||
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
|
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
|
||||||
'a tree`) with insert + in-order traversal. Tests parametric ADT,
|
'a tree`) with insert + in-order traversal. Tests parametric ADT,
|
||||||
recursive match, List.append, List.fold_left.
|
recursive match, List.append, List.fold_left.
|
||||||
|
- 2026-05-08 Phase 1+3 — `function | pat when GUARD -> body | …`
|
||||||
|
guard support (+3 tests, 464 total). `parse-function` mirrors the
|
||||||
|
match-clause when-handling.
|
||||||
- 2026-05-08 Phase 5.1 — anagrams.ml baseline (18/18 pass). Counts
|
- 2026-05-08 Phase 5.1 — anagrams.ml baseline (18/18 pass). Counts
|
||||||
anagram-equivalence groups via Hashtbl + List.sort + String.get +
|
anagram-equivalence groups via Hashtbl + List.sort + String.get +
|
||||||
for-loop. `["eat";"tea";"tan";"ate";"nat";"bat"]` → 3 groups.
|
for-loop. `["eat";"tea";"tan";"ate";"nat";"bat"]` → 3 groups.
|
||||||
|
|||||||
Reference in New Issue
Block a user