123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183(* This file is part of asak.
*
* Copyright (C) 2019 IRIF / OCaml Software Foundation.
*
* asak is distributed under the terms of the MIT license. See the
* included LICENSE file for details. *)openTypedtreeopenMonad_error.ErrSletfilter_mapfxs=letauxxacc=matchfxwith|None->acc|Somex->x::accinList.fold_rightauxxs[]letinit_path()=#ifOCAML_VERSION>=(4,09,0)Compmisc.init_path()#elseCompmisc.init_pathtrue#endifletparsetree_of_stringstr=tryletwithout_directives=String.concat";;"@@List.filter(funx->letx=String.trimxinString.lengthx>0&&x.[0]!='#')@@Str.split(Str.regexp_string";;")strinret(Parse.implementation(Lexing.from_stringwithout_directives))with|Lexer.Error_|Syntaxerr.Error_->fail"parse error"letinit_env()=letold_modules=!Clflags.open_modulesininit_path();letenv=Compmisc.initial_env()inClflags.open_modules:=old_modules;envletextract_typedtree=#ifOCAML_VERSION>=(4,08,0)fun(s,_,_,_)->s#elsefun(s,_,_)->s#endiflettype_with_initlst=tryret@@extract_typedtree@@Typemod.type_structure(init_env())lst#ifOCAML_VERSION<(4,12,0)Location.none#endifwithTypetexp.Error_|Typecore.Error_->fail"type error"letsimplify_lambdalambda=#ifOCAML_VERSION>=(4,09,0)Simplif.simplify_lambdalambda#elseSimplif.simplify_lambda""lambda#endiflettransl_expexpr=#ifOCAML_VERSION>=(4,12,0)Translcore.transl_exp~scopes:Debuginfo.Scoped_location.empty_scopesexpr#elifOCAML_VERSION>=(4,11,0)Translcore.transl_exp~scopes:[]expr#elseTranslcore.transl_expexpr#endifletlambda_of_expression?nameexpr=Lambda_normalization.normalize_local_variables?name@@Lambda_normalization.inline_all@@simplify_lambda@@transl_expexprletget_name_of_patpat=matchpat.pat_descwith|Tpat_var(id,_)->Someid|Tpat_alias(_,id,_)->Someid|_->Noneletget_namefx=matchget_name_of_patx.vb_patwith|Someidwhen(Ident.nameid=f)->Someid|_->Nonelethas_namefx=matchget_namefxwith|Some_->true|None->falseletlist_find_mapf=letauxaccx=matchaccwith|None->fx|_->accinList.fold_leftauxNoneletget_specific_lambda_of_typedtreenamestructure=letpred_bindingx=matchget_namenamexwith|Somename->Some(name,x.vb_expr)|None->Noneinletpredx=matchx.str_descwith|Tstr_value(_,xs)->list_find_mappred_bindingxs|_->Noneinmatchlist_find_mappredstructure.str_itemswith|None->fail"get_specific_lambda_of_typedtree: function not found"|Some(name,item)->ret@@lambda_of_expression~nameitemletfind_let_in_parsetree_itemsf=letopenParsetreeinletpred_bindingx=matchx.pvb_pat.ppat_descwith|Ppat_varv->Asttypes.(v.txt)=f|_->falseinletpredx=matchx.pstr_descwith|Pstr_value(_,xs)->List.existspred_bindingxs|_->falseinList.find_optpredletrecread_module_expr~prefixm=matchm.mod_descwith|Tmod_structurestructure->read_structure_with_loc~prefixstructure#ifOCAML_VERSION>=(4,10,0)|Tmod_functor(_,m)->#else|Tmod_functor(_,_,_,m)->#endifread_module_expr~prefixm|_->[]andread_value_binding~prefixx=matchget_name_of_patx.vb_patwith|Somename->letname_s=prefix^"."^(Ident.namename)inSome((name_s,x.vb_pat.pat_loc),lambda_of_expression~namex.vb_expr)|None->Noneandread_item_desc~prefixx=letread_module_exprm=letmid=#ifOCAML_VERSION>=(4,10,0)Option.value~default:""(Option.mapIdent.namem.mb_id)#elseIdent.namem.mb_id#endifinletprefix=prefix^"."^midinread_module_expr~prefixm.mb_exprinmatchx.str_descwith|Tstr_value(_,xs)->filter_map(read_value_binding~prefix)xs|Tstr_modulem->read_module_exprm|Tstr_recmodulexs->List.flatten@@List.mapread_module_exprxs|_->[]andread_structure_with_loc?prefixstructure=letprefix=matchprefixwith|None->""|Someprefix->prefixinList.flatten@@List.map(funx->read_item_desc~prefixx)structure.str_itemsletread_structure?prefixstructure=List.map(fun((x,_),y)->x,y)(read_structure_with_loc?prefixstructure)letread_stringstr=lett=parsetree_of_stringstr>>=type_with_initinmatchruntwith|Errore->failwithe|Okt->read_structuret