123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384(* Dump an OCaml value into a printable string.
* By Richard W.M. Jones (rich@annexia.org).
* dumper.ml 1.2 2005/02/06 12:38:21 rich Exp
*)openPrintfopenObjletrecdumpr=ifis_intrthenstring_of_int(magicr:int)else((* Block. *)letrecget_fieldsacc=function|0->acc|n->letn=n-1inget_fields(fieldrn::acc)ninletrecis_listr=ifis_intrthen(if(magicr:int)=0thentrue(* [] *)elsefalse)else(lets=sizerandt=tagrinift=0&&s=2thenis_list(fieldr1)(* h :: t *)elsefalse)inletrecget_listr=ifis_intrthen[]elseleth=fieldr0andt=get_list(fieldr1)inh::tinletopaquename=(* XXX In future, print the address of value 'r'. Not possible in
* pure OCaml at the moment.
*)"<"^name^">"inlets=sizerandt=tagrin(* From the tag, determine the type of block. *)ifis_listrthen((* List. *)letfields=get_listrin"["^String.concat"; "(List.mapdumpfields)^"]")elseift=0then((* Tuple, array, record. *)letfields=get_fields[]sin"("^String.concat", "(List.mapdumpfields)^")")(* Note that [lazy_tag .. forward_tag] are < no_scan_tag. Not
* clear if very large constructed values could have the same
* tag. XXX *)elseift=lazy_tagthenopaque"lazy"elseift=closure_tagthenopaque"closure"elseift=object_tagthen((* Object. *)letfields=get_fields[]sinletclasz,id,slots=matchfieldswithh::h'::t->h,h',t|_->assertfalsein(* No information on decoding the class (first field). So just print
* out the ID and the slots.
*)"Object #"^dumpid^" ("^String.concat", "(List.mapdumpslots)^")")elseift=infix_tagthenopaque"infix"elseift=forward_tagthenopaque"forward"elseift<no_scan_tagthen((* Constructed value. *)letfields=get_fields[]sin"Tag"^string_of_intt^" ("^String.concat", "(List.mapdumpfields)^")")elseift=string_tagthen("\""^String.escaped(magicr:string)^"\"")elseift=double_tagthen(string_of_float(magicr:float))elseift=abstract_tagthenopaque"abstract"elseift=custom_tagthenopaque"custom"elsefailwith("dump: impossible tag ("^string_of_intt^")"))letdumpv=dump(reprv)