ocaml: phase 6 Hashtbl (+6 tests, 332 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 32s

Backing store is a one-element list cell holding a SX dict; keys
coerced to strings via str so int/string keys work uniformly. API:
create, add, replace, find, find_opt, mem, length.

_hashtbl_create / _hashtbl_add / _hashtbl_replace / _hashtbl_find_opt /
_hashtbl_mem / _hashtbl_length primitives wired in eval.sx; OCaml-side
Hashtbl module wraps them in lib/ocaml/runtime.sx.
This commit is contained in:
2026-05-08 12:57:22 +00:00
parent 6d7197182e
commit 812aa75d43
4 changed files with 66 additions and 1 deletions

View File

@@ -66,7 +66,30 @@
;; Print: prints to host stdout via println.
(list "print_string" (fn (s) (begin (print s) nil)))
(list "print_endline" (fn (s) (begin (println s) nil)))
(list "print_int" (fn (i) (begin (print (str i)) nil))))))
(list "print_int" (fn (i) (begin (print (str i)) nil)))
;; Hashtbl primitives (one-element list cell holding a dict).
;; Keys are coerced to strings via `str` so any value type works
;; as a key (matches Hashtbl's polymorphic-key semantics).
(list "_hashtbl_create" (fn (n) (list {})))
(list "_hashtbl_add"
(fn (t) (fn (k) (fn (v)
(begin
(set-nth! t 0 (merge (nth t 0) (dict (str k) v)))
nil)))))
(list "_hashtbl_replace"
(fn (t) (fn (k) (fn (v)
(begin
(set-nth! t 0 (merge (nth t 0) (dict (str k) v)))
nil)))))
(list "_hashtbl_find_opt"
(fn (t) (fn (k)
(cond
((has-key? (nth t 0) (str k)) (list "Some" (get (nth t 0) (str k))))
(else (list "None"))))))
(list "_hashtbl_mem"
(fn (t) (fn (k) (has-key? (nth t 0) (str k)))))
(list "_hashtbl_length"
(fn (t) (len (keys (nth t 0))))))))
(define ocaml-env-lookup
(fn (env name)