summaryrefslogtreecommitdiff
path: root/ocaml/test/test_op9.ml
blob: dc34c8a506886a28650fe9236c21c12cdd54e21f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(* Test opcode 9 (invoke) *)

open Nock_lib

let () =
  (* Build a simple core: [[formula] payload] *)
  (* Formula at slot 2: [0 3] (returns payload) *)
  (* Payload at slot 3: 42 *)
  let core = Noun.cell
    (Noun.cell (Noun.atom 0) (Noun.atom 3))
    (Noun.atom 42)
  in

  (* Test: *[core [9 2 [0 1]]] *)
  (* This should:
     1. Evaluate [0 1] on core → core
     2. Extract slot 2 from core → [0 3]
     3. Evaluate [0 3] on core → 42
  *)

  let subject = core in
  let formula = Noun.cell
    (Noun.atom 9)
    (Noun.cell
      (Noun.atom 2)
      (Noun.cell (Noun.atom 0) (Noun.atom 1)))
  in

  Printf.printf "Subject (core): [[0 3] 42]\n";
  Printf.printf "Subject mug: 0x%08lx\n" (Noun.mug subject);
  Printf.printf "Formula: [9 2 [0 1]]\n";
  Printf.printf "Formula mug: 0x%08lx\n\n" (Noun.mug formula);

  Printf.printf "This should compute:\n";
  Printf.printf "  1. Evaluate [0 1] on core → core\n";
  Printf.printf "  2. Extract slot 2 from result → [0 3]\n";
  Printf.printf "  3. Evaluate [0 3] on core → 42\n\n";

  let result = Nock.nock_on subject formula in

  Printf.printf "Result mug: 0x%08lx\n" (Noun.mug result);
  Printf.printf "Result: %s\n"
    (match result with
     | Noun.Atom { z; _ } -> Z.to_string z
     | Noun.Cell _ -> "cell")