(** Test the fully iterative Nock interpreter - no stack overflow! *) open Nock_lib let () = Printf.printf "Testing iterative Nock interpreter (no stack limit needed)...\n\n"; (* Test 1: Simple increment *) Printf.printf "[1] Testing simple increment: *[42 [4 0 1]]\n"; let result = Nock_iter.nock_on (Noun.atom 42) (Noun.cell (Noun.atom 4) (Noun.cell (Noun.atom 0) (Noun.atom 1))) in Printf.printf " Result: %s\n" (Z.to_string (match result with Noun.Atom { z; _ } -> z | _ -> Z.zero)); Printf.printf " Expected: 43\n\n"; (* Test 2: Distribution *) Printf.printf "[2] Testing distribution: *[42 [[4 0 1] [4 0 1]]]\n"; let inc_fol = Noun.cell (Noun.atom 4) (Noun.cell (Noun.atom 0) (Noun.atom 1)) in let dist = Noun.cell inc_fol inc_fol in let result = Nock_iter.nock_on (Noun.atom 42) dist in Printf.printf " Result: [%s %s]\n" (Z.to_string (match Noun.head result with Noun.Atom { z; _ } -> z | _ -> Z.zero)) (Z.to_string (match Noun.tail result with Noun.Atom { z; _ } -> z | _ -> Z.zero)); Printf.printf " Expected: [43 43]\n\n"; (* Test 3: Small pill to verify no recursion issues *) Printf.printf "[3] Loading ivory pill (this would stack overflow with recursive version)...\n"; Eio_main.run (fun env -> let fs = Eio.Stdenv.fs env in let bytes = Eio.Path.(load (fs / "ivory.pill")) |> Bytes.of_string in Printf.printf " Pill size: %d bytes\n" (Bytes.length bytes); let pill = Serial.cue bytes in Printf.printf " Cued successfully\n"; match pill with | Noun.Cell { h = _tag; t = core; _ } -> Printf.printf " Structure: [tag core]\n"; Printf.printf " Core mug: 0x%08lx\n" (Noun.mug core); Printf.printf "\n Running lifecycle formula with ITERATIVE interpreter...\n"; Printf.printf " (No OCAMLRUNPARAM needed!)\n"; let formula = Noun.cell (Noun.atom 2) (Noun.cell (Noun.cell (Noun.atom 0) (Noun.atom 3)) (Noun.cell (Noun.atom 0) (Noun.atom 2))) in let start = Unix.gettimeofday () in let kernel = Nock_iter.nock_on core formula in let elapsed = Unix.gettimeofday () -. start in Printf.printf " āœ“ SUCCESS in %.2fs\n" elapsed; Printf.printf " Kernel mug: 0x%08lx\n" (Noun.mug kernel); Printf.printf "\nāœ… Iterative Nock interpreter works perfectly!\n"; Printf.printf " No stack overflow, no OCAMLRUNPARAM needed.\n"; | _ -> Printf.printf " Unexpected pill structure\n" )