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

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:
2026-05-08 20:26:28 +00:00
parent b92a98fb45
commit 029c1783f4
3 changed files with 26 additions and 3 deletions

View File

@@ -787,7 +787,7 @@
(list :try expr cases)))))))
(define parse-function
(fn ()
;; `function | pat -> body | …` ≡ fun x -> match x with | pat -> body
;; `function | pat [when GUARD] -> body | …`
(let ()
(begin
(when (at-op? "|") (advance-tok!))
@@ -795,11 +795,18 @@
(begin
(define one
(fn ()
(let ((p (parse-pattern)))
(let ((p (parse-pattern)) (guard nil))
(begin
(when (at-kw? "when")
(begin (advance-tok!)
(set! guard (parse-expr-no-seq))))
(consume! "op" "->")
(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)
(define loop
(fn ()