ocaml: phase 4 'assert EXPR' (+3 tests, 487 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s

Tokenizer already classified 'assert' as a keyword; this commit wires
it through:
  parser  : parse-prefix dispatches like 'not' — advance, recur, wrap
            as (:assert EXPR).
  eval    : evaluate operand; nil on truthy, host-error 'Assert_failure'
            on false. Caught cleanly by existing try/with.

  assert true; 42                          = 42
  let x = 5 in assert (x = 5); x + 1       = 6
  try (assert false; 0) with _ -> 99       = 99
This commit is contained in:
2026-05-09 00:32:35 +00:00
parent bd2cd8aad1
commit 14b52cfaa7
4 changed files with 26 additions and 0 deletions

View File

@@ -430,6 +430,11 @@
(else (error (str "ocaml-eval: unbound variable " name))))))
((= tag "neg") (- 0 (ocaml-eval (nth ast 1) env)))
((= tag "not") (not (ocaml-eval (nth ast 1) env)))
((= tag "assert")
(let ((v (ocaml-eval (nth ast 1) env)))
(cond
((= v false) (error "Assert_failure"))
(else nil))))
((= tag "deref")
(let ((cell (ocaml-eval (nth ast 1) env)))
(nth cell 0)))