ocaml: phase 2 try/with + raise (+6 tests, 204 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 57s

Parser: try EXPR with | pat -> handler | ... -> (:try EXPR CLAUSES).
Eval delegates to SX guard with else matching the raised value against
clause patterns; re-raises on no-match. raise/failwith/invalid_arg
shipped as builtins. failwith "msg" raises ("Failure" msg) so
| Failure msg -> ... patterns match.
This commit is contained in:
2026-05-08 08:20:11 +00:00
parent 937342bbf0
commit 6a1f63f0d1
4 changed files with 82 additions and 2 deletions

View File

@@ -573,6 +573,28 @@
(begin (advance-tok!) (one) (loop)))))
(loop)
(cons :match (cons scrut (list cases)))))))))
(define parse-try
(fn ()
(let ((expr (parse-expr-no-seq)))
(begin
(consume! "keyword" "with")
(when (at-op? "|") (advance-tok!))
(let ((cases (list)))
(begin
(define one
(fn ()
(let ((p (parse-pattern)))
(begin
(consume! "op" "->")
(let ((body (parse-expr)))
(append! cases (list :case p body)))))))
(one)
(define loop
(fn ()
(when (at-op? "|")
(begin (advance-tok!) (one) (loop)))))
(loop)
(list :try expr cases)))))))
(define parse-function
(fn ()
;; `function | pat -> body | …` ≡ fun x -> match x with | pat -> body
@@ -634,6 +656,7 @@
((at-kw? "function") (begin (advance-tok!) (parse-function)))
((at-kw? "for") (begin (advance-tok!) (parse-for)))
((at-kw? "while") (begin (advance-tok!) (parse-while)))
((at-kw? "try") (begin (advance-tok!) (parse-try)))
(else (parse-tuple)))))
(set!
parse-expr