123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331(* Yoann Padioleau
*
* Copyright (C) 2019 r2c
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation, with the
* special exception on linking described in file license.txt.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file
* license.txt for more details.
*)openCommonopenAst_mlmoduleG=Ast_generic(*****************************************************************************)(* Prelude *)(*****************************************************************************)(* Ast_ml to Ast_generic.
*
* See ast_generic.ml for more information.
*)(*****************************************************************************)(* Helpers *)(*****************************************************************************)letid=funx->xletoption=Common.map_optletlist=List.mapletstring=idletbool=idletint=idleterror=Ast_generic.errorletfake_info()=Parse_info.fake_info"FAKE"(*****************************************************************************)(* Entry point *)(*****************************************************************************)letinfox=xlettokv=infovletwrap=fun_of_a(v1,v2)->letv1=_of_av1andv2=infov2in(v1,v2)letrecidentv=wrapstringvandname(v1,v2)=letv1=qualifierv1andv2=identv2inv2,{G.empty_name_infowithG.name_qualifier=Somev1}andqualifierv=listidentvandtype_=function|TyNamev1->letv1=namev1inG.TyApply(v1,[])|TyVarv1->letv1=identv1inG.TyVarv1|TyFunction((v1,v2))->letv1=type_v1andv2=type_v2inG.TyFun([v1],v2)|TyApp((v1,v2))->letv1=listtype_v1andv2=namev2inG.TyApply(v2,v1|>List.map(funt->G.TypeArgt))|TyTuplev1->letv1=listtype_v1inG.TyTuplev1andexpr=function|Lv1->letv1=literalv1inG.Lv1|Namev1->letv1=namev1inG.Name(v1,G.empty_id_info())|Constructor((v1,v2))->letv1=namev1andv2=optionexprv2inG.Constructor(v1,Common.opt_to_listv2)|Tuplev1->letv1=listexprv1inG.Tuplev1|Listv1->letv1=listexprv1inG.Container(G.List,v1)|Sequencev1->letv1=listexprv1inG.Seqv1|Prefix((v1,v2))->letv1=wrapstringv1andv2=exprv2inletn=v1,G.empty_name_infoinG.Call(G.Name(n,G.empty_id_info()),[G.Argv2])|Infix((v1,v2,v3))->letn=v2,G.empty_name_infoinletv1=exprv1andv3=exprv3inG.Call(G.Name(n,G.empty_id_info()),[G.Argv1;G.Argv3])|Call((v1,v2))->letv1=exprv1andv2=listargumentv2inG.Call(v1,v2)|RefAccess((v1,v2))->let_v1=tokv1andv2=exprv2inG.DeRef(v2)|RefAssign((v1,v2,v3))->letv1=exprv1and_v2=tokv2andv3=exprv3inG.Assign(G.DeRefv1,v3)|FieldAccess((v1,v2))->letv1=exprv1in(matchv2with|[],id->letid=identidinG.ObjAccess(v1,id)|_->letv2=namev2inG.OtherExpr(G.OE_FieldAccessQualified,[G.Ev1;G.Nv2]))|FieldAssign((v1,v2,v3))->letv1=exprv1andv3=exprv3in(matchv2with|[],id->letid=identidinG.Assign(G.ObjAccess(v1,id),v3)|_->letv2=namev2inG.Assign(G.OtherExpr(G.OE_FieldAccessQualified,[G.Ev1;G.Nv2]),v3))|Record((v1,v2))->letv1=optionexprv1andv2=list(fun(v1,v2)->letv2=exprv2in(matchv1with|[],id->letid=identidinletent=G.basic_entityid[]inG.FieldVar(ent,{G.vinit=Somev2;vtype=None})|_->letv1=namev1inlete=G.OtherExpr(G.OE_FieldAccessQualified,[G.Nv1;G.Ev2])inletst=G.ExprStmteinG.FieldStmt(st)))v2inletobj=G.Recordv2in(matchv1with|None->obj|Somee->G.OtherExpr(G.OE_RecordWith,[G.Ee;G.Eobj]))|New((v1,v2))->letv1=tokv1andv2=namev2inG.Call(G.IdSpecial(G.New,v1),[G.Arg(G.Name(v2,G.empty_id_info()))])|ObjAccess((v1,v2))->letv1=exprv1andv2=identv2inG.ObjAccess(v1,v2)|LetIn((v1,v2,v3))->let_v1=listlet_bindingv1and_v2=exprv2and_v3=rec_optv3inraiseTodo|Fun((v1,v2))->letv1=listparameterv1andv2=exprv2inletdef={G.fparams=v1;frettype=None;fbody=G.ExprStmtv2}inG.Lambdadef|Nop->G.Nop|If((v1,v2,v3))->letv1=exprv1andv2=exprv2andv3=exprv3inG.Conditional(v1,v2,v3)|Match((v1,v2))->letv1=exprv1andv2=listmatch_casev2inG.MatchPattern(v1,v2)|Try((v1,v2))->letv1=exprv1andv2=listmatch_casev2inletcatches=v2|>List.map(fun(pat,e)->pat,G.ExprStmte)inletst=G.Try(G.ExprStmtv1,catches,None)inG.OtherExpr(G.OE_StmtExpr,[G.Sst])|While((v1,v2))->letv1=exprv1andv2=exprv2inletst=G.While(v1,G.ExprStmtv2)inG.OtherExpr(G.OE_StmtExpr,[G.Sst])|For((v1,v2,v3,v4,v5))->letv1=identv1andv2=exprv2and(tok,nextop,condop)=for_directionv3andv4=exprv4andv5=exprv5inletent=G.basic_entityv1[]inletvar={G.vinit=Somev2;vtype=None}inletn=G.Name((v1,G.empty_name_info),G.empty_id_info())inletnext=(G.AssignOp(n,(nextop,tok),G.L(G.Int("1",tok))))inletcond=G.Call(G.IdSpecial(G.ArithOpcondop,tok),[G.Argn;G.Argv4])inletheader=G.ForClassic([G.ForInitVar(ent,var)],cond,next)inletst=G.For(header,G.ExprStmtv5)inG.OtherExpr(G.OE_StmtExpr,[G.Sst])andliteral=function|Intv1->letv1=wrapstringv1inG.Intv1|Floatv1->letv1=wrapstringv1inG.Floatv1|Charv1->letv1=wrapstringv1inG.Charv1|Stringv1->letv1=wrapstringv1inG.Stringv1andargument=function|Argv1->letv1=exprv1inG.Argv1|ArgKwd((v1,v2))->letv1=identv1andv2=exprv2inG.ArgKwd(v1,v2)|ArgQuestion((v1,v2))->letv1=identv1andv2=exprv2inG.ArgOther(G.OA_ArgQuestion,[G.Idv1;G.Ev2])andmatch_case(v1,(v2,v3))=letv1=patternv1andv2=exprv2andv3=optionexprv3in(matchv3with|None->v1,v2|Somex->G.PatWhen(v1,x),v2)andfor_direction=function|Tov1->letv1=tokv1inv1,G.Plus,G.LtE|Downtov1->letv1=tokv1inv1,G.Minus,G.GtEandrec_optv=optiontokvandpattern=function|PatVarv1->letv1=identv1inG.PatVar(v1,G.empty_id_info())|PatLiteralv1->letv1=literalv1inG.PatLiteralv1|PatConstructor((v1,v2))->letv1=namev1andv2=optionpatternv2inG.PatConstructor(v1,Common.opt_to_listv2)|PatConsInfix((v1,v2,v3))->letv1=patternv1andv2=tokv2andv3=patternv3inletn=("::",v2),G.empty_name_infoinG.PatConstructor(n,[v1;v3])|PatTuplev1->letv1=listpatternv1inG.PatTuplev1|PatListv1->letv1=listpatternv1inG.PatListv1|PatUnderscorev1->letv1=tokv1inG.PatUnderscorev1|PatRecordv1->letv1=list(fun(v1,v2)->letv1=namev1andv2=patternv2inv1,v2)v1inG.PatRecordv1|PatAs((v1,v2))->letv1=patternv1andv2=identv2inG.PatAs(v1,(v2,G.empty_id_info()))|PatDisj((v1,v2))->letv1=patternv1andv2=patternv2inG.PatDisj(v1,v2)|PatTyped((v1,v2))->letv1=patternv1andv2=type_v2inG.PatTyped(v1,v2)andlet_binding=function|LetClassicv1->let_v1=let_defv1inraiseTodo|LetPattern((v1,v2))->letv1=patternv1andv2=exprv2inG.LetPattern(v1,v2)andlet_def{lname=lname;lparams=lparams;lbody=lbody}=let_v1=identlnameinlet_v2=listparameterlparamsinlet_v3=exprlbodyin()andparameterv=G.ParamPattern(patternv)andtype_declaration{tname=tname;tparams=tparams;tbody=tbody}=letv1=identtnameinletv2=listtype_parametertparamsinletv3=type_def_kindtbodyinletentity={(G.basic_entityv1[])withG.tparams=v2}inletdef={G.tbody=v3}inentity,defandtype_parameterv=identv,[]andtype_def_kind=function|AbstractType->G.OtherTypeKind(G.OTKO_AbstractType,[])|CoreTypev1->letv1=type_v1inG.AliasTypev1|AlgebricTypev1->letv1=list(fun(v1,v2)->letv1=identv1andv2=listtype_v2inG.OrConstructor(v1,v2))v1inG.OrTypev1|RecordTypev1->letv1=list(fun(v1,v2,v3)->letv1=identv1andv2=type_v2andv3=optiontokv3inletent=G.basic_entityv1(matchv3withSome_->[G.Mutable]|None->[])inG.FieldVar(ent,{G.vinit=None;vtype=Somev2}))v1inG.AndTypev1andmodule_declaration{mname=mname;mbody=mbody}=let_v1=identmnameinlet_v2=module_exprmbodyin()andmodule_expr=function|ModuleNamev1->let_v1=namev1in()|ModuleStructv1->let_v1=listitemv1in()anditem=function|Typev1->let_v1=listtype_declarationv1in()|Exception((v1,v2))->let_v1=identv1and_v2=listtype_v2in()|External((v1,v2,v3))->let_v1=identv1and_v2=type_v2and_v3=list(wrapstring)v3in()|Openv1->let_v1=namev1in()|Val((v1,v2))->let_v1=identv1and_v2=type_v2in()|Let((v1,v2))->let_v1=rec_optv1and_v2=listlet_bindingv2in()|Modulev1->let_v1=module_declarationv1in()andprogramxs=List.mapitemxs