OCaml bootstrapper Phase 2: HTML renderer, SX server, Python bridge

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-15 23:28:48 +00:00
parent 16fa813d6d
commit 313f7d6be1
18 changed files with 2541 additions and 191 deletions

View File

@@ -35,6 +35,7 @@ let sx_str args =
(** Convert a value to a list. *)
let sx_to_list = function
| List l -> l
| ListRef r -> !r
| Nil -> []
| v -> raise (Eval_error ("Expected list, got " ^ type_of v))
@@ -60,6 +61,7 @@ let sx_apply f args_list =
let sx_append_b lst item =
match lst with
| List items -> List (items @ [item])
| ListRef r -> r := !r @ [item]; lst (* mutate in place, return same ref *)
| _ -> raise (Eval_error ("append!: expected list, got " ^ type_of lst))
(** Mutable dict-set — set key in dict, return value. *)
@@ -74,7 +76,8 @@ let get_val container key =
match container, key with
| Dict d, String k -> dict_get d k
| Dict d, Keyword k -> dict_get d k
| List l, Number n -> (try List.nth l (int_of_float n) with _ -> Nil)
| (List l | ListRef { contents = l }), Number n ->
(try List.nth l (int_of_float n) with _ -> Nil)
| _ -> raise (Eval_error ("get: unsupported " ^ type_of container ^ " / " ^ type_of key))
(** Register get as a primitive override — transpiled code calls (get d k). *)
@@ -82,7 +85,12 @@ let () =
Sx_primitives.register "get" (fun args ->
match args with
| [c; k] -> get_val c k
| _ -> raise (Eval_error "get: 2 args"))
| [c; k; default] ->
(try
let v = get_val c k in
if v = Nil then default else v
with _ -> default)
| _ -> raise (Eval_error "get: 2-3 args"))
(* ====================================================================== *)
@@ -195,8 +203,9 @@ let sx_emit a b = prim_call "emit!" [a; b]
let sx_emitted a = prim_call "emitted" [a]
let sx_context a b = prim_call "context" [a; b]
(* Trampoline — evaluate thunks iteratively *)
let trampoline v = v (* CEK machine doesn't use tree-walk thunks *)
(* Trampoline — forward-declared in sx_ref.ml, delegates to CEK eval_expr *)
(* This is a stub; the real trampoline is wired up in sx_ref.ml after eval_expr is defined *)
let trampoline v = v
(* Value-returning type predicates — the transpiled code passes these through
sx_truthy, so they need to return Bool, not OCaml bool. *)