From: Richard J. <ri...@an...> - 2005-02-09 10:23:59
|
On Wed, Feb 09, 2005 at 12:09:02AM +0200, Anatoly Zaretsky wrote: > Hi! > > I was playing with this nice tool and I want to suggest some additions to it. > > First, I think pointers (out-of-heap ones too) can be (portably I > hope) dumped like this: > > let fieldval x = > let ni = 0n in > Obj.set_field (Obj.repr ni) 1 x; > ni;; > > let hex_of_field x = Printf.sprintf "%0*nx" (Nativeint.size / 4) (fieldval x);; > > Then, custom blocks contain pointer to their ops structure in the > first field, which can be used to determine their type. This way > "standard" custom values can be recognized and printed: > > let with_obj f x = f (Obj.obj x) > let cust_ops x = Obj.field x 0 > let (custom_printers : (Obj.t, Obj.t -> string) Hashtbl.t) = > Hashtbl.create 5 (* printer functions hashed with ops struct pointer > values *) > let pr_pair (v : 'a) (f : 'a -> string) = cust_ops (Obj.repr v), with_obj f > > List.iter (fun (v, f) -> Hashtbl.add custom_printers v f) > [ > pr_pair Int32.zero (Printf.sprintf "%ldl"); > pr_pair Int64.zero (Printf.sprintf "%LdL"); > pr_pair Nativeint.zero (Printf.sprintf "%ndn") > ] > > let add_printer v f = > let v', f' = pr_pair v f in > Hashtbl.add custom_printers v' f' > and print_custom x = Hashtbl.find custom_printers (cust_ops x) x > > For example: > # print_custom (Obj.repr 765n);; > - : string = "765n" > # print_custom (Obj.repr (-765l));; > - : string = "-765l" > # print_custom (Obj.repr (-7659876655443438L));; > - : string = "-7659876655443438L" > > It seems that get_list guarded with is_list is redundant - one can write > if is_list r then ( (* List. *) > let fields = obj r in (* or (obj r : t list) for purity ;) *) in > ... > ) > instead. > > Similarly for arrays: > else if t = 0 || t = double_array_tag then ( (* Tuple, array, record. *) > let fields = Array.to_list (obj r) in (* float arrays and > records work fine! *) > > For constructed values similar trick can be used. > > Hope this will be helpful. > > P.S. And thanks for the great tutorial on OCaml! I learnt OCaml this > summer mostly from it (with INRIA manual and O'Reilly book of course > ;) ). For-loops in the cerebellum are the best :) > > -- > Best regards, > Tolik -- Richard Jones, CTO Merjis Ltd. Merjis - web marketing and technology - http://merjis.com Team Notepad - intranets and extranets for business - http://team-notepad.com |