123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181openOcaml_commonopenParsetreeletprint_positionoutxlexbuf=letopenLexinginletpos=lexbuf.lex_curr_pinPrintf.fprintfoutx"%s:%d:%d : %s"pos.pos_fnamepos.pos_lnum(pos.pos_cnum-pos.pos_bol+1)(Lexing.lexemelexbuf)letprepare_errorerr=letopenSyntaxerrinmatcherrwith|Unclosed(opening_loc,opening,closing_loc,closing)->Location.errorf~loc:closing_loc~sub:[Location.msg~loc:opening_loc"This '%s' might be unmatched"opening;]"Syntax error: '%s' expected"closing|Expecting(loc,nonterm)->Location.errorf~loc"Syntax error: %s expected."nonterm|Not_expecting(loc,nonterm)->Location.errorf~loc"Syntax error: %s not expected."nonterm|Applicative_pathloc->Location.errorf~loc"Syntax error: applicative paths of the form F(X).t are not supported \
when the option -no-app-func is set."|Variable_in_scope(loc,var)->Location.errorf~loc"In this scoped type, variable %a is reserved for the local type %s."Pprintast.tyvarvarvar|Otherloc->Location.errorf~loc"Syntax error"|Ill_formed_ast(loc,s)->Location.errorf~loc"broken invariant in parsetree: %s"s|Invalid_package_type(loc,s)->Location.errorf~loc"invalid package type: %s"sletparse_compilo_stringst=letopenLexinginletlexbuf=Lexing.from_stringstinlexbuf.lex_curr_p<-{lexbuf.lex_curr_pwithpos_fname=""};trylettree=Parse.interfacelexbufintreewith|Syntaxerr.Errore->lete2=prepare_erroreinLocation.print_reportFormat.std_formattere2;exit1|Parsing.Parse_error->Printf.fprintfstderr"%a: syntax error in %s \n"print_positionlexbufst;exit1(*
let rec print_core_desc f = function
| Ptyp_any -> Format.pp_print_text f "any"
| Ptyp_var v -> Format.fprintf f "[var %s]" v
| Ptyp_constr (a,_) -> Format.fprintf f "[constr %a]" Pprintast.longident a.txt
| Ptyp_tuple al -> Format.pp_print_list ~pp_sep:(fun f () ->Format.pp_print_text f "*") print_core f al
| Ptyp_arrow (_,t1,t2) -> Format.fprintf f "[%a -> %a]" print_core t1 print_core t2
| _ -> failwith "not yet implemented print_core"
and print_core f a = print_core_desc f a.ptyp_desc
let print_constr f c =
Format.fprintf f "%s" c.pcd_name.txt
let print_type_dec f pd =
Format.fprintf f "type %s =" pd.ptype_name.txt;
match pd.ptype_kind with
Ptype_abstract -> Format.fprintf f "abstract"
| Ptype_variant cl -> Format.pp_print_list ~pp_sep:(fun f () ->Format.pp_print_text f "|") print_constr f cl
| _ -> failwith "not yet implemented ptype kind"
let print_signature_item_desc f = function
Psig_value v -> Format.fprintf f "[Val name:'%s' core:'%a']" v.pval_name.Asttypes.txt print_core v.pval_type
| Psig_type (_,vl) -> Format.pp_print_list print_type_dec f vl
| _ -> failwith "not yet implemented"
let print_signature =
List.iter (fun s ->
Format.printf "%a@." print_signature_item_desc s.psig_desc)*)letrecto_compo_typet=letopenTypeinmatcht.ptyp_descwith|Ptyp_varv->Abstractv|Ptyp_constr(a,args)->Name(Longident.lasta.txt,List.mapto_compo_typeargs)|Ptyp_tupleal->Prod(List.mapto_compo_typeal)|Ptyp_arrow_->letargs,res=uncurrytinFun(args,res)|_->failwith"Not yet supported"anduncurryt=matcht.ptyp_descwith|Ptyp_arrow(_,t1,t2)->lettl,f=uncurryt2in(to_compo_typet1::tl,f)|_->([],to_compo_typet)letbuild_constrconslist=List.map(funx->letat2=matchList.filter(funy->y.attr_name.txt="weight")x.pcd_attributeswith|y::_->(matchy.attr_payloadwith|PStrtstr->lettstr2=Pprintast.string_of_structuretstrinlettstr3=String.subtstr22(String.lengthtstr2-2)inSome(float_of_stringtstr3)|_->None)|_->Noneinmatchx.pcd_argswith|Pcstr_tuple[]->(x.pcd_name.txt,None,at2)|Pcstr_tupleargs->(x.pcd_name.txt,Some(Type.Prod(List.mapto_compo_typeargs)),at2)|_->failwith"Not implemented, and I do not understand Parsetree doc")conslistletbuild_type_defvl=letparam_list=List.map(fun(t,_)->matcht.ptyp_descwith|Ptyp_vars->s|_->failwith"Not yet implemented type params shape")vl.ptype_paramsinmatchvl.ptype_kindwith|Ptype_variantconslist->((vl.ptype_name.txt,param_list),build_constrconslist)|_->failwith"Not yet supported type kind"letto_val_and_def=List.fold_left(fun(def,value)t->matcht.psig_descwith|Psig_valuev->letargs,res=uncurryv.pval_typein(def,{Type.name=v.pval_name.txt;intypes=args;outtype=res}::value)|Psig_type(_,vl)->letvl2=List.mapbuild_type_defvlin(vl2@def,value)|_->failwith"Not yet supported")([],[])letparse_strings=letv=parse_compilo_stringsinletdef,vl=to_val_and_defvinmatchvlwitht::_->(def,t)|_->failwith"No function defined"letparse_typedefs=letv=parse_compilo_stringsinletdef,_=to_val_and_defvinmatchdefwith|t::_->t|_->failwith"Require exactly one type definition"(*
open Type_lib
let _ =
let v = parse_compilo_string Sys.argv.(1) in
let (def,vl) = to_val_and_def v in
List.iter Recursive_type_gen.evaluate def;
List.iter (fun x -> print_endline (Type.string_of_sum x)) def;
List.iter (Type.print_func stdout) vl
*)