Step 5 piece 6: migrate 23 .sx files to define-library/import
Wraps all core .sx files in R7RS define-library with explicit export lists, plus (import ...) at end for backward-compatible global re-export. Libraries registered: (sx bytecode) — 83 opcode constants (sx render) — 15 tag registries + render helpers (sx signals) — 23 reactive signal primitives (sx r7rs) — 21 R7RS aliases (sx compiler) — 42 compiler functions (sx vm) — 32 VM functions (sx freeze) — 9 freeze/thaw functions (sx content) — 6 content store functions (sx callcc) — 1 call/cc wrapper (sx highlight) — 13 syntax highlighting functions (sx stdlib) — 47 stdlib functions (sx swap) — 13 swap algebra functions (sx render-trace) — 8 render trace functions (sx harness) — 21 test harness functions (sx canonical) — 12 canonical serialization functions (web adapter-html) — 13 HTML renderer functions (web adapter-sx) — 13 SX wire format functions (web engine) — 33 hypermedia engine functions (web request-handler) — 4 request handling functions (web page-helpers) — 12 page helper functions (web router) — 36 routing functions (web deps) — 19 dependency analysis functions (web orchestration) — 59 page orchestration functions Key changes: - define-library now inherits parent env (env-extend env instead of env-extend make-env) so library bodies can access platform primitives - sx_server.ml: added resolve_library_path + load_library_file for import resolution (maps library specs to file paths) - cek_run_with_io: handles "import" locally instead of sending to Python bridge 2608/2608 tests passing. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -280,10 +280,44 @@ let flush_batched_io result_str =
|
||||
!final
|
||||
end
|
||||
|
||||
(** Resolve a library spec to a file path.
|
||||
(sx render) → spec/render.sx, (sx bytecode) → lib/bytecode.sx, etc.
|
||||
Returns Some path or None if unknown. *)
|
||||
let _lib_base = ref "lib"
|
||||
let _spec_base = ref "spec"
|
||||
let _web_base = ref "web"
|
||||
|
||||
let resolve_library_path lib_spec =
|
||||
let parts = match lib_spec with List l | ListRef { contents = l } -> l | _ -> [] in
|
||||
match List.map (fun v -> match v with Symbol s -> s | String s -> s | _ -> "") parts with
|
||||
| ["sx"; name] ->
|
||||
(* Check spec/ first, then lib/ *)
|
||||
let spec_path = Filename.concat !_spec_base (name ^ ".sx") in
|
||||
let lib_path = Filename.concat !_lib_base (name ^ ".sx") in
|
||||
if Sys.file_exists spec_path then Some spec_path
|
||||
else if Sys.file_exists lib_path then Some lib_path
|
||||
else None
|
||||
| ["web"; name] ->
|
||||
let path = Filename.concat !_web_base (name ^ ".sx") in
|
||||
if Sys.file_exists path then Some path else None
|
||||
| [prefix; name] ->
|
||||
(* Generic: try prefix/name.sx *)
|
||||
let path = Filename.concat prefix (name ^ ".sx") in
|
||||
if Sys.file_exists path then Some path else None
|
||||
| _ -> None
|
||||
|
||||
(** Load a library file — parse and evaluate all expressions in the global env.
|
||||
The file should contain a define-library form that registers itself. *)
|
||||
let _import_env : env option ref = ref None
|
||||
|
||||
let load_library_file path =
|
||||
let env = match !_import_env with Some e -> e | None -> Sx_types.make_env () in
|
||||
let exprs = Sx_parser.parse_file path in
|
||||
List.iter (fun expr -> ignore (Sx_ref.eval_expr expr (Env env))) exprs
|
||||
|
||||
(** IO-aware CEK run — handles suspension by dispatching IO requests.
|
||||
When the CEK machine suspends with a perform, this function sends
|
||||
the IO request to the Python bridge, resumes with the response,
|
||||
and repeats until evaluation completes. *)
|
||||
Import requests are handled locally (load .sx file).
|
||||
Other IO requests are sent to the Python bridge. *)
|
||||
let cek_run_with_io state =
|
||||
let s = ref state in
|
||||
let is_terminal s = match Sx_ref.cek_terminal_p s with Bool true -> true | _ -> false in
|
||||
@@ -295,16 +329,27 @@ let cek_run_with_io state =
|
||||
if is_suspended !s then begin
|
||||
let request = Sx_runtime.get_val !s (String "request") in
|
||||
let op = match Sx_runtime.get_val request (String "op") with String s -> s | _ -> "" in
|
||||
(* Extract args based on operation type *)
|
||||
let args = match op with
|
||||
let response = match op with
|
||||
| "import" ->
|
||||
let lib = Sx_runtime.get_val request (String "library") in
|
||||
[String "import"; lib]
|
||||
(* Resolve library locally — load the .sx file *)
|
||||
let lib_spec = Sx_runtime.get_val request (String "library") in
|
||||
let key = Sx_ref.library_name_key lib_spec in
|
||||
if Sx_types.sx_truthy (Sx_ref.library_loaded_p key) then
|
||||
(* Already loaded — just resume *)
|
||||
Nil
|
||||
else begin
|
||||
(match resolve_library_path lib_spec with
|
||||
| Some path -> load_library_file path
|
||||
| None ->
|
||||
Printf.eprintf "[import] WARNING: no file for library %s\n%!"
|
||||
(Sx_runtime.value_to_str lib_spec));
|
||||
Nil
|
||||
end
|
||||
| _ ->
|
||||
let a = Sx_runtime.get_val request (String "args") in
|
||||
(match a with List l -> l | _ -> [a])
|
||||
let args = let a = Sx_runtime.get_val request (String "args") in
|
||||
(match a with List l -> l | _ -> [a]) in
|
||||
io_request op args
|
||||
in
|
||||
let response = io_request op args in
|
||||
s := Sx_ref.cek_resume !s response;
|
||||
loop ()
|
||||
end else
|
||||
|
||||
@@ -503,7 +503,7 @@ and step_eval_list expr env kont =
|
||||
|
||||
(* step-sf-define-library *)
|
||||
and step_sf_define_library args env kont =
|
||||
(let lib_spec = (first (args)) in let decls = (rest (args)) in (let lib_env = (env_extend ((make_env ()))) in let exports = ref ((List [])) in let body_forms = ref ((List [])) in (let () = ignore ((List.iter (fun decl -> ignore ((if sx_truthy ((let _and = (list_p (decl)) in if not (sx_truthy _and) then _and else (let _and = (Bool (not (sx_truthy ((empty_p (decl)))))) in if not (sx_truthy _and) then _and else (symbol_p ((first (decl))))))) then (let kind = (symbol_name ((first (decl)))) in (if sx_truthy ((prim_call "=" [kind; (String "export")])) then (exports := (prim_call "append" [!exports; (List (List.map (fun s -> (if sx_truthy ((symbol_p (s))) then (symbol_name (s)) else (String (sx_str [s])))) (sx_to_list (rest (decl)))))]); Nil) else (if sx_truthy ((prim_call "=" [kind; (String "begin")])) then (body_forms := (prim_call "append" [!body_forms; (rest (decl))]); Nil) else Nil))) else Nil))) (sx_to_list decls); Nil)) in (let () = ignore ((List.iter (fun form -> ignore ((eval_expr (form) (lib_env)))) (sx_to_list !body_forms); Nil)) in (let export_dict = (Dict (Hashtbl.create 0)) in (let () = ignore ((List.iter (fun name -> ignore ((if sx_truthy ((env_has (lib_env) (name))) then (sx_dict_set_b export_dict name (env_get (lib_env) (name))) else Nil))) (sx_to_list !exports); Nil)) in (let () = ignore ((register_library (lib_spec) (export_dict))) in (make_cek_value (Nil) (env) (kont)))))))))
|
||||
(let lib_spec = (first (args)) in let decls = (rest (args)) in (let lib_env = (env_extend (env)) in let exports = ref ((List [])) in let body_forms = ref ((List [])) in (let () = ignore ((List.iter (fun decl -> ignore ((if sx_truthy ((let _and = (list_p (decl)) in if not (sx_truthy _and) then _and else (let _and = (Bool (not (sx_truthy ((empty_p (decl)))))) in if not (sx_truthy _and) then _and else (symbol_p ((first (decl))))))) then (let kind = (symbol_name ((first (decl)))) in (if sx_truthy ((prim_call "=" [kind; (String "export")])) then (exports := (prim_call "append" [!exports; (List (List.map (fun s -> (if sx_truthy ((symbol_p (s))) then (symbol_name (s)) else (String (sx_str [s])))) (sx_to_list (rest (decl)))))]); Nil) else (if sx_truthy ((prim_call "=" [kind; (String "begin")])) then (body_forms := (prim_call "append" [!body_forms; (rest (decl))]); Nil) else Nil))) else Nil))) (sx_to_list decls); Nil)) in (let () = ignore ((List.iter (fun form -> ignore ((eval_expr (form) (lib_env)))) (sx_to_list !body_forms); Nil)) in (let export_dict = (Dict (Hashtbl.create 0)) in (let () = ignore ((List.iter (fun name -> ignore ((if sx_truthy ((env_has (lib_env) (name))) then (sx_dict_set_b export_dict name (env_get (lib_env) (name))) else Nil))) (sx_to_list !exports); Nil)) in (let () = ignore ((register_library (lib_spec) (export_dict))) in (make_cek_value (Nil) (env) (kont)))))))))
|
||||
|
||||
(* bind-import-set *)
|
||||
and bind_import_set import_set env =
|
||||
|
||||
Reference in New Issue
Block a user