ocaml: phase 6 Hashtbl.iter / Hashtbl.fold (+2 tests, 494 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s

New host primitive _hashtbl_to_list returns the entries as a list of
OCaml tuples — ('tuple' k v) form, matching the AST representation
that the pattern-match VM (:ptuple) expects. Without that exact
shape, '(k, v) :: rest' patterns fail to match.

Hashtbl.iter / Hashtbl.fold in runtime walk that list with the user
fn. This closes a long-standing gap: previously Hashtbl was opaque
once values were written (we could only find_opt one key at a time).

  let t = Hashtbl.create 4 in
  Hashtbl.add t "a" 1; Hashtbl.add t "b" 2; Hashtbl.add t "c" 3;
  Hashtbl.fold (fun _ v acc -> acc + v) t 0   = 6
This commit is contained in:
2026-05-09 00:53:32 +00:00
parent 1b38f89055
commit 207dfc60ad
4 changed files with 51 additions and 1 deletions

View File

@@ -115,7 +115,23 @@
(list "_hashtbl_mem"
(fn (t) (fn (k) (has-key? (nth t 0) (str k)))))
(list "_hashtbl_length"
(fn (t) (len (keys (nth t 0))))))))
(fn (t) (len (keys (nth t 0)))))
;; _hashtbl_to_list: returns [(k, v); ...] as a list of pairs.
;; Keys are returned as the stringified form used internally.
(list "_hashtbl_to_list"
(fn (t)
(let ((d (nth t 0)) (out (list)))
(begin
(define ks (keys d))
(define loop
(fn (xs)
(when (not (= xs (list)))
(begin
(append! out
(list "tuple" (first xs) (get d (first xs))))
(loop (rest xs))))))
(loop ks)
out)))))))
(define ocaml-env-lookup
(fn (env name)