diff options
Diffstat (limited to 'ocaml/lib/noun.ml')
-rw-r--r-- | ocaml/lib/noun.ml | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/ocaml/lib/noun.ml b/ocaml/lib/noun.ml index c59ec80..9be65b7 100644 --- a/ocaml/lib/noun.ml +++ b/ocaml/lib/noun.ml @@ -40,16 +40,39 @@ let tail = function - 2 is head, 3 is tail - For n > 1: if even, go left; if odd, go right *) +let slot_debug = ref false + let rec slot n noun = - if Z.equal n Z.one then + let debug = !slot_debug && (Z.equal n (Z.of_int 2) || Z.equal n (Z.of_int 3)) in + if debug then + Printf.eprintf "[SLOT-DEBUG] slot(%s, noun)\n%!" (Z.to_string n); + + if Z.equal n Z.one then begin + if debug then Printf.eprintf "[SLOT-DEBUG] slot(%s) = identity\n%!" (Z.to_string n); noun + end else if Z.equal n Z.zero then raise Exit - else + else begin let bit = Z.testbit n 0 in (* Check if odd *) let parent = Z.shift_right n 1 in + if debug then + Printf.eprintf "[SLOT-DEBUG] slot(%s): bit=%b parent=%s\n%!" + (Z.to_string n) bit (Z.to_string parent); let sub = slot parent noun in - if bit then tail sub else head sub + let result = if bit then tail sub else head sub in + if debug then begin + Printf.eprintf "[SLOT-DEBUG] slot(%s): taking %s, result is %s\n%!" + (Z.to_string n) (if bit then "tail" else "head") + (if is_cell result then + let h = head result in + if is_atom h then + (match h with Atom z -> "cell[" ^ Z.to_string z ^ " ...]" | _ -> "cell[? ...]") + else "cell[cell ...]" + else "atom") + end; + result + end (** Equality test for nouns *) let rec equal a b = |