Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file helpers.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145(*****************************************************************************)(* Open Source License *)(* Copyright (c) 2021 Valentin Chaboche *)(* Copyright (c) 2021 Nomadic Labs, <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openPpxlibmoduleP=Ast_helpers.Patternletmk_loc?locx=matchlocwith|None->{txt=x;loc=Location.none}|Someloc->{txt=x;loc}letoptf=trySome(f())with_->NonemoduleInfo=structtypet={stri_name:string;stri_attr:attributeoption;stri_loc:Location.t;}letcreate_info?(name="")?attr?(loc=Location.none)()={stri_name=name;stri_attr=attr;stri_loc=loc}letupdate_namenameinfo={infowithstri_name=name}letget_namex=x.stri_nameletget_attributex=x.stri_attrletget_locx=x.stri_locendmodulePairs=struct(* Split a list in two list of size (length list) / 2
example:
split_even_list [1; 2; 3; 4] => [1; 2] [3; 4]
The reason we split only even list is because separated list are meant
to be nested into pairs.
A odd list would be translated to something like:
[x; y; z] => (pair x (pair y z))
In our case, the split is only applied on [y;z] *)letsplit_even_listlist=letn=List.lengthlistinassert(nmod2=0);letmiddle=n/2inleti=ref0inList.partition(fun_->i:=!i+1;!i<=middle)listtype'anested_pairs=|Pairof'anested_pairs*'anested_pairs|Doubleof'a*'a|Simpleof'aletrecnest_generatorsgens=matchgenswith|[]->letloc=Location.noneinSimple[%exprPbt.Gens.unit]|[x]->Simplex|[x;y]->Double(x,y)|genswhenList.lengthgensmod2=0->let(l1,l2)=split_even_listgensinPair(nest_generatorsl1,nest_generatorsl2)|x::xs->Pair(Simplex,nest_generatorsxs)letrecnested_pairs_to_exprloc=function|Simpleexpr->expr|Pair(x,y)->[%exprQCheck.pair[%enested_pairs_to_exprlocx][%enested_pairs_to_exprlocy]]|Double(x,y)->[%exprQCheck.pair[%ex][%ey]]letrecnested_pairs_to_list=function|Simplex->[x]|Double(x,y)->[x;y]|Pair(x,y)->letleft=nested_pairs_to_listxinletright=nested_pairs_to_listyinleft@rightletnames_from_gensfgens=letid=ref0inletcreate_fresh_namei=letx=!iini:=!i+1;f@@string_of_intxin(* Replace_by_id replace the generators pattern by identifiers refering to the
function pattern *)letrecreplace_by_id=function|Pair(x,y)->letx=replace_by_idxinlety=replace_by_idyinPair(x,y)|Double_->letx=create_fresh_nameidinlety=create_fresh_nameidinDouble(x,y)|Simple_->Simple(create_fresh_nameid)inreplace_by_idgensletpattern_from_genslocfgens=letreccreate_patternloc=function|Pair(x,y)->[%pat?([%pcreate_patternlocx],[%pcreate_patternlocy])]|Double(x,y)->letarg_x=P.ppat_var~locxinletarg_y=P.ppat_var~locyinPpat_tuple[arg_x;arg_y]|>P.pattern~loc|Simplex->P.ppat_var~locxinletargs=names_from_gensfgensin(create_patternlocargs,args)end