From 23a57eed38d560f0c2d84cc7803fafc74dbcc1dd Mon Sep 17 00:00:00 2001 From: polwex Date: Tue, 7 Oct 2025 02:22:58 +0700 Subject: working on solid pill in ocaml --- ocaml/test/test_two_stage_boot.ml | 250 +++++++++++++++++++------------------- ocaml/test_cue_solid.ml | 17 +++ 2 files changed, 142 insertions(+), 125 deletions(-) create mode 100644 ocaml/test_cue_solid.ml diff --git a/ocaml/test/test_two_stage_boot.ml b/ocaml/test/test_two_stage_boot.ml index 258e882..c18ea58 100644 --- a/ocaml/test/test_two_stage_boot.ml +++ b/ocaml/test/test_two_stage_boot.ml @@ -142,151 +142,151 @@ let stage1_ivory_boot env = None (* Stage 2: Boot solid pill events *) -(* let stage2_solid_boot env _ivory_kernel = *) - (* Printf.printf "\n╔═══════════════════════════════════════╗\n"; *) - (* Printf.printf "║ STAGE 2: Solid Pill Events ║\n"; *) - (* Printf.printf "╚═══════════════════════════════════════╝\n\n"; *) +let stage2_solid_boot env _ivory_kernel = + Printf.printf "\n╔═══════════════════════════════════════╗\n"; + Printf.printf "║ STAGE 2: Solid Pill Events ║\n"; + Printf.printf "╚═══════════════════════════════════════╝\n\n"; (* Load solid pill *) - (* Printf.printf "[1] Loading solid.pill...\n%!"; *) - (* let fs = Eio.Stdenv.fs env in *) - (* let pill_bytes = Eio.Path.(load (fs / "solid.pill")) |> Bytes.of_string in *) - (* Printf.printf " Size: %d bytes (%.1f MB)\n%!" *) - (* (Bytes.length pill_bytes) *) - (* (float_of_int (Bytes.length pill_bytes) /. 1024.0 /. 1024.0); *) - - (* Printf.printf "[2] Cuing solid pill...\n%!"; *) - (* let start = Unix.gettimeofday () in *) - (* let pill = Serial.cue pill_bytes in *) - (* let elapsed = Unix.gettimeofday () -. start in *) - (* Printf.printf " ✓ Cued in %.2fs\n\n%!" elapsed; *) + Printf.printf "[1] Loading solid.pill...\n%!"; + let fs = Eio.Stdenv.fs env in + let pill_bytes = Eio.Path.(load (fs / "solid.pill")) |> Bytes.of_string in + Printf.printf " Size: %d bytes (%.1f MB)\n%!" + (Bytes.length pill_bytes) + (float_of_int (Bytes.length pill_bytes) /. 1024.0 /. 1024.0); + + Printf.printf "[2] Cuing solid pill...\n%!"; + let start = Unix.gettimeofday () in + let pill = Serial.cue pill_bytes in + let elapsed = Unix.gettimeofday () -. start in + Printf.printf " ✓ Cued in %.2fs\n\n%!" elapsed; (* Parse structure: [%pill %solid [bot mod use]] *) - (* Printf.printf "[3] Parsing solid pill structure...\n%!"; *) - (* match pill with *) - (* | Noun.Cell (_tag, rest) -> *) - (* begin match rest with *) - (* | Noun.Cell (_typ, rest2) -> *) - (* Printf.printf " Tag: pill\n"; *) - (* Printf.printf " Type: solid\n"; *) - - (* begin match rest2 with *) - (* | Noun.Cell (bot, rest3) -> *) + Printf.printf "[3] Parsing solid pill structure...\n%!"; + match pill with + | Noun.Cell { h = _tag; t = rest; _ } -> + begin match rest with + | Noun.Cell { h = _typ; t = rest2; _ } -> + Printf.printf " Tag: pill\n"; + Printf.printf " Type: solid\n"; + + begin match rest2 with + | Noun.Cell { h = bot; t = rest3; _ } -> (* Count bot events *) - (* let rec count_list acc n = *) - (* match n with *) - (* | Noun.Atom _ -> acc *) - (* | Noun.Cell (_, rest) -> count_list (acc + 1) rest *) - (* in *) - (* let bot_count = count_list 0 bot in *) - (* Printf.printf " Bot events: %d\n" bot_count; *) - - (* begin match rest3 with *) - (* | Noun.Cell (mod_, rest4) -> *) - (* let mod_count = count_list 0 mod_ in *) - (* Printf.printf " Mod events: %d\n" mod_count; *) - - (* begin match rest4 with *) - (* | Noun.Cell (use, _) -> *) - (* let use_count = count_list 0 use in *) - (* Printf.printf " Use events: %d\n" use_count; *) - - (* let total = bot_count + mod_count + use_count in *) - (* Printf.printf " Total: %d events\n\n" total; *) + let rec count_list acc n = + match n with + | Noun.Atom _ -> acc + | Noun.Cell { h = _; t = rest; _ } -> count_list (acc + 1) rest + in + let bot_count = count_list 0 bot in + Printf.printf " Bot events: %d\n" bot_count; + + begin match rest3 with + | Noun.Cell { h = mod_; t = rest4; _ } -> + let mod_count = count_list 0 mod_ in + Printf.printf " Mod events: %d\n" mod_count; + + begin match rest4 with + | Noun.Cell { h = use; t = _; _ } -> + let use_count = count_list 0 use in + Printf.printf " Use events: %d\n" use_count; + + let total = bot_count + mod_count + use_count in + Printf.printf " Total: %d events\n\n" total; (* Concatenate all events into a single list *) - (* Printf.printf "[4] Concatenating all events...\n%!"; *) - (* let rec append_lists l1 l2 = *) - (* match l1 with *) - (* | Noun.Atom _ -> l2 *) - (* | Noun.Cell (h, t) -> Noun.cell h (append_lists t l2) *) - (* in *) - (* let all_events = append_lists bot (append_lists mod_ use) in *) - (* Printf.printf " ✓ Event list built\n\n"; *) + Printf.printf "[4] Concatenating all events...\n%!"; + let rec append_lists l1 l2 = + match l1 with + | Noun.Atom _ -> l2 + | Noun.Cell { h; t; _ } -> Noun.cell h (append_lists t l2) + in + let all_events = append_lists bot (append_lists mod_ use) in + Printf.printf " ✓ Event list built\n\n"; (* Now run u3v_boot on all events *) - (* Printf.printf "[5] Running u3v_boot() on %d events...\n%!" total; *) - (* Printf.printf " This will call u3v_life() with the event list\n%!"; *) + Printf.printf "[5] Running u3v_boot() on %d events...\n%!" total; + Printf.printf " This will call u3v_life() with the event list\n%!"; - (* begin try *) - (* let start = Unix.gettimeofday () in *) + begin try + let start = Unix.gettimeofday () in (* Call the lifecycle formula on the event list *) - (* Printf.printf " Running [2 [0 3] [0 2]] on event list...\n%!"; *) - (* let kernel = Boot.life all_events in *) + Printf.printf " Running [2 [0 3] [0 2]] on event list...\n%!"; + let kernel = Boot.life all_events in - (* let elapsed = Unix.gettimeofday () -. start in *) - (* Printf.printf " ✓ SUCCESS! Kernel updated in %.4fs\n\n" elapsed; *) + let elapsed = Unix.gettimeofday () -. start in + Printf.printf " ✓ SUCCESS! Kernel updated in %.4fs\n\n" elapsed; (* Verify kernel *) - (* Printf.printf "[6] Verifying updated kernel...\n%!"; *) - (* begin try *) - (* let _poke = Noun.slot (Z.of_int 23) kernel in *) - (* Printf.printf " ✓ Has poke gate at slot 23\n\n"; *) - - (* Printf.printf "╔═══════════════════════════════════════╗\n"; *) - (* Printf.printf "║ 🎉🎉🎉 FULL BOOT SUCCESS! 🎉🎉🎉 ║\n"; *) - (* Printf.printf "╚═══════════════════════════════════════╝\n\n"; *) - - (* Printf.printf "Boot sequence complete:\n"; *) - (* Printf.printf " 1. Stage 1: Ivory pill with null → Initial kernel\n"; *) - (* Printf.printf " 2. Stage 2: Solid pill %d events → Updated kernel\n" total; *) - (* Printf.printf " 3. Kernel is ready to receive pokes!\n\n"; *) - - (* true *) - - (* with _ -> *) - (* Printf.printf " ✗ No slot 23 in updated kernel\n\n"; *) - (* false *) - (* end *) - - (* with *) - (* | Noun.Exit -> *) - (* Printf.printf " ✗ FAILED: Nock Exit during lifecycle\n\n"; *) - (* false *) - - (* | e -> *) - (* Printf.printf " ✗ FAILED: %s\n\n" (Printexc.to_string e); *) - (* false *) - (* end *) - - (* | Noun.Atom _ -> *) - (* Printf.printf " ✗ rest4 is atom (expected use)\n"; *) - (* false *) - (* end *) - - (* | Noun.Atom _ -> *) - (* Printf.printf " ✗ rest3 is atom (expected [mod use])\n"; *) - (* false *) - (* end *) - - (* | Noun.Atom _ -> *) - (* Printf.printf " ✗ rest2 is atom (expected [bot mod use])\n"; *) - (* false *) - (* end *) - - (* | Noun.Atom _ -> *) - (* Printf.printf " ✗ rest is atom (expected [type ...])\n"; *) - (* false *) - (* end *) - - (* | Noun.Atom _ -> *) - (* Printf.printf " ✗ Pill is atom (expected cell)\n"; *) - (* false *) + Printf.printf "[6] Verifying updated kernel...\n%!"; + begin try + let _poke = Noun.slot (Z.of_int 23) kernel in + Printf.printf " ✓ Has poke gate at slot 23\n\n"; + + Printf.printf "╔═══════════════════════════════════════╗\n"; + Printf.printf "║ 🎉🎉🎉 FULL BOOT SUCCESS! 🎉🎉🎉 ║\n"; + Printf.printf "╚═══════════════════════════════════════╝\n\n"; + + Printf.printf "Boot sequence complete:\n"; + Printf.printf " 1. Stage 1: Ivory pill with null → Initial kernel\n"; + Printf.printf " 2. Stage 2: Solid pill %d events → Updated kernel\n" total; + Printf.printf " 3. Kernel is ready to receive pokes!\n\n"; + + true + + with _ -> + Printf.printf " ✗ No slot 23 in updated kernel\n\n"; + false + end + + with + | Noun.Exit -> + Printf.printf " ✗ FAILED: Nock Exit during lifecycle\n\n"; + false + + | e -> + Printf.printf " ✗ FAILED: %s\n\n" (Printexc.to_string e); + false + end + + | Noun.Atom _ -> + Printf.printf " ✗ rest4 is atom (expected use)\n"; + false + end + + | Noun.Atom _ -> + Printf.printf " ✗ rest3 is atom (expected [mod use])\n"; + false + end + + | Noun.Atom _ -> + Printf.printf " ✗ rest2 is atom (expected [bot mod use])\n"; + false + end + + | Noun.Atom _ -> + Printf.printf " ✗ rest is atom (expected [type ...])\n"; + false + end + + | Noun.Atom _ -> + Printf.printf " ✗ Pill is atom (expected cell)\n"; + false (* Main test *) let main env = (* Stage 1: Ivory *) - let _success = stage1_ivory_boot env in () - (* match stage1_ivory_boot env with *) - (* | Some ivory_kernel -> *) + (* let _success = stage1_ivory_boot env in () *) + match stage1_ivory_boot env with + | Some ivory_kernel -> (* Stage 2: Solid *) - (* let _success = stage2_solid_boot env ivory_kernel in *) - (* () *) + let _success = stage2_solid_boot env ivory_kernel in + () - (* | None -> *) - (* Printf.printf "╔═══════════════════════════════════════╗\n"; *) - (* Printf.printf "║ ✗ STAGE 1 FAILED - Cannot continue ║\n"; *) - (* Printf.printf "╚═══════════════════════════════════════╝\n\n" *) + | None -> + Printf.printf "╔═══════════════════════════════════════╗\n"; + Printf.printf "║ ✗ STAGE 1 FAILED - Cannot continue ║\n"; + Printf.printf "╚═══════════════════════════════════════╝\n\n" let () = Eio_main.run main diff --git a/ocaml/test_cue_solid.ml b/ocaml/test_cue_solid.ml new file mode 100644 index 0000000..95be2da --- /dev/null +++ b/ocaml/test_cue_solid.ml @@ -0,0 +1,17 @@ +open Nock_lib + +let () = + let bytes = Eio.Path.(load (Eio.Stdenv.fs (Eio_main.run (fun env -> env)) / "solid.pill")) |> Bytes.of_string in + Printf.printf "File size: %d bytes\n" (Bytes.length bytes); + + let noun = Serial.cue bytes in + Printf.printf "Cued successfully\n"; + Printf.printf "Is atom: %b\n" (Noun.is_atom noun); + Printf.printf "Is cell: %b\n" (Noun.is_cell noun); + + if Noun.is_cell noun then begin + let h = Noun.head noun in + let t = Noun.tail noun in + Printf.printf "Head is atom: %b\n" (Noun.is_atom h); + Printf.printf "Tail is atom: %b\n" (Noun.is_atom t) + end -- cgit v1.2.3