123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834(* 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.
*)openCommonmoduleA=Ast_jsmoduleC=Cst_jsmoduleG=Ast_generic(* for the operators *)(*****************************************************************************)(* Prelude *)(*****************************************************************************)(* Cst_js to Ast_js.
*
* See also transpile_js.ml for important helper functions.
*)(*****************************************************************************)(* Types *)(*****************************************************************************)(* used only to tag Id to avoid repetition in the code highlighter
* less: factorize code with graph_code_js?
*)typeenv={(* I handle block scope by not using
* a ref of mutable here! Just build a new list and passed it down.
*)locals:(string*Ast_js.resolved_name(* Local or Param *))list;(* 'var's have a function scope.
* alt: lift Var up in a ast_js_build.ml transforming phase
*)vars:(string,bool)Hashtbl.t;}letempty_env()={locals=[];vars=Hashtbl.create0;}exceptionTodoConstructofstring*Parse_info.t(* The string is usually "advanced es6" or "Typescript" *)exceptionUnhandledConstructofstring*Parse_info.t(*****************************************************************************)(* Helpers *)(*****************************************************************************)letoptfenvx=matchxwith|None->None|Somex->Some(fenvx)letfst3(x,_,_)=xletnoop=A.Block[]letnot_resolved()=refA.NotResolvedexceptionFoundofParse_info.tletfirst_tok_of_itemx=lethooks={Visitor_js.default_visitorwithVisitor_js.kinfo=(fun(_k,_)i->raise(Foundi));}inbeginletvout=Visitor_js.mk_visitorhooksintryvout(C.Itemx);failwith"first_to_of_item: could not find a token";withFoundtok->tokendlets_of_nn=Ast_js.str_of_namen(*
let is_local env n =
let s = s_of_n n in
List.mem_assoc s env.locals || Hashtbl.mem env.vars s
*)(* copy-paste of Graph_code_js.add_locals mostly *)letadd_localsenvvs=letlocals=vs|>Common.map_filter(funv->lets=s_of_nv.A.v_nameinmatchv.A.v_kindwith|A.Let|A.Const->Some(s,A.Local)|A.Var->Hashtbl.replaceenv.varsstrue;None)in{envwithlocals=locals@env.locals}letadd_paramsenvps=letparams=ps|>List.map(funp->lets=s_of_np.A.p_nameins,A.Param)in{envwithlocals=params@env.locals}(* we would like to remove leading ./ and possibly add
* some node_modules/xxx/index.js but we can not do that here.
* See graph_code_js and module_path_js for filename resolving.
*)letpath_to_file(path,tok)=path,tok(*****************************************************************************)(* Entry point *)(*****************************************************************************)letrecprogramxs=letenv=empty_env()inmodule_itemsenvxsandmodule_itemsenvxs=xs|>List.map(module_itemenv)|>List.flattenandmodule_itemenv=function|C.Itx->itemNoneenvx|>List.map(funres->matchreswith|A.VarDeclvar->A.Vvar|_->lettok=first_tok_of_itemxinA.S(tok,res))|C.Import(_,x,_)->importenvx|>List.map(funx->A.Mx)|C.Export(tok,x)->exportenvtokxandimportenv=function|C.ImportEffect((file,tok))->iffile=~".*\\.css$"then[A.ImportCss(file,tok)]else[A.ImportEffect(file,tok)]|C.ImportFrom((default_opt,names_opt),(_,path))->letfile=path_to_filepathin(matchdefault_optwith|Somen->[A.Import((A.default_entity,sndn),nameenvn,file)]|None->[])@(matchnames_optwith|None->[]|Someni->(matchniwith|C.ImportNamespace(_star,_as,n1)->letn1=nameenvn1in[A.ModuleAlias(n1,file)]|C.ImportNamesxs->xs|>C.unparen|>C.uncomma|>List.map(fun(n1,n2opt)->letn1=nameenvn1inletn2=matchn2optwith|None->n1|Some(_,n2)->nameenvn2inA.Import(n1,n2,file))|C.ImportTypes(_tok,_xs)->(* ignore for now *)[]))andexportenvtok=function|C.ExportDefaultExpr(tok,e,_)->lete=exprenveinletn=A.default_entity,tokinletv={A.v_name=n;v_kind=A.Const;v_init=e;v_resolved=not_resolved()}in[A.Vv;A.M(A.Export(n))]|C.ExportDeclx->letxs=itemNoneenvxinxs|>List.map(function|A.VarDeclv->[A.Vv;A.M(A.Export(v.A.v_name))]|_->raise(UnhandledConstruct("exporting a stmt",tok)))|>List.flatten|C.ExportDefaultDecl(tok,x)->(* this is ok to have anonymous entities here *)letxs=item(Sometok)envxinxs|>List.map(function|A.VarDeclv->[A.Vv;A.M(A.Export(v.A.v_name))]|_->raise(UnhandledConstruct("exporting a stmt",tok)))|>List.flatten|C.ExportNames(xs,_)->xs|>C.unparen|>C.uncomma|>List.map(fun(n1,n2opt)->letn1=nameenvn1inmatchn2optwith|None->[A.M(A.Export(n1))]|Some(_,n2)->letn2=nameenvn2inletid=A.Id(n1,not_resolved())inletv={A.v_name=n2;v_kind=A.Const;v_init=id;v_resolved=not_resolved()}in[A.Vv;A.M(A.Exportn2)])|>List.flatten|C.ReExportNames(xs,(_from,path),_)->xs|>C.unbrace|>C.uncomma|>List.map(fun(n1,n2opt)->letn1=nameenvn1inlettmpname=("!tmp_"^fstn1,sndn1)inletfile=path_to_filepathinletimport=A.Import(n1,tmpname,file)inletid=A.Id(tmpname,not_resolved())inmatchn2optwith|None->letv={A.v_name=n1;v_kind=A.Const;v_init=id;v_resolved=not_resolved()}in[A.Mimport;A.Vv;A.M(A.Exportn1)]|Some(_,n2)->letn2=nameenvn2inletv={A.v_name=n2;v_kind=A.Const;v_init=id;v_resolved=not_resolved()}in[A.Mimport;A.Vv;A.M(A.Exportn2)])|>List.flatten|C.ReExportNamespace(_,_,_)->raise(UnhandledConstruct("reexporting namespace",tok))anditemdefault_optenv=function|C.Stx->stmtenvx|C.FunDeclx->letfun_=func_declenvxin(matchx.C.f_kind,default_optwith|C.F_func(_,Somex),None->letn=nameenvxin[A.VarDecl{A.v_name=n;v_kind=A.Const;v_init=A.Fun(fun_,None);v_resolved=not_resolved()}]|C.F_func(_,None),Sometok->letn=A.default_entity,tokin[A.VarDecl{A.v_name=n;v_kind=A.Const;v_init=A.Fun(fun_,None);v_resolved=not_resolved()}]|C.F_func(_,Somex),Sometok->letn1=A.default_entity,tokinletn2=nameenvxin[A.VarDecl{A.v_name=n1;v_kind=A.Const;v_init=A.Fun(fun_,Somen2);v_resolved=not_resolved()}]|C.F_func(_,None),None->raise(UnhandledConstruct("weird: anonymous func decl",fst3x.C.f_params))|_->raise(UnhandledConstruct("weird func decl",fst3x.C.f_params)))|C.ClassDeclx->letclass_=class_declenvxin(matchx.C.c_name,default_optwith|Somex,None->letn=nameenvxin[A.VarDecl{A.v_name=n;v_kind=A.Const;v_init=A.Class(class_,None);v_resolved=not_resolved()}]|None,Sometok->letn=A.default_entity,tokin[A.VarDecl{A.v_name=n;v_kind=A.Const;v_init=A.Class(class_,None);v_resolved=not_resolved()}]|Somex,Sometok->letn1=A.default_entity,tokinletn2=nameenvxin[A.VarDecl{A.v_name=n1;v_kind=A.Const;v_init=A.Class(class_,Somen2);v_resolved=not_resolved()}]|None,None->raise(UnhandledConstruct("weird: anonymous class decl",x.C.c_tok)))|C.InterfaceDeclx->raise(UnhandledConstruct("Typescript",x.C.i_tok))|C.ItemTodotok->raise(TodoConstruct("ItemTodo",tok))(* ------------------------------------------------------------------------- *)(* Names *)(* ------------------------------------------------------------------------- *)andname_envx=xandlabel_envx=xandproperty_nameenv=function|C.PN_Idx->A.PN(nameenvx)|C.PN_Stringx->A.PN(nameenvx)|C.PN_Numx->A.PN(nameenvx)|C.PN_Computedx->A.PN_Computed(x|>C.unparen|>exprenv)(* ------------------------------------------------------------------------- *)(* Statement *)(* ------------------------------------------------------------------------- *)andstmtenv=function|C.VarsDecl((vkind,_),bindings,_)->bindings|>C.uncomma|>List.map(funx->letvars=var_bindingenvvkindxinvars|>List.map(funvar->A.VarDeclvar))|>List.flatten|C.Blockx->[stmt1_item_listenv(C.unparenx)]|C.Nop_->[]|C.ExprStmt(e,_)->lete=exprenvein(matchewith|A.String("use strict",tok)->[A.ExprStmt(A.Apply(A.IdSpecial(A.UseStrict,tok),[]))]|_->[A.ExprStmte])|C.If(_,e,then_,elseopt)->lete=e|>C.unparen|>exprenvinletthen_=stmt1envthen_inletelse_=matchelseoptwith|None->noop|Some(_,st)->stmt1envstin[A.If(e,then_,else_)]|C.Do(_,st,_,e,_)->letst=stmt1envstinlete=e|>C.unparen|>exprenvin[A.Do(st,e)]|C.While(_,e,st)->lete=e|>C.unparen|>exprenvinletst=stmt1envstin[A.While(e,st)]|C.For(_,_,lhs_vars_opt,_,e2opt,_,e3opt,_,st)->lete1=matchlhs_vars_optwith|Some(C.LHS1e)->Right(exprenve)|Some(C.ForVars(((vkind,_),vbindings)))->Left(vbindings|>C.uncomma|>List.map(funx->(var_bindingenvvkindx))|>List.flatten)|None->Right(A.Nop)inlete2=expr_optenve2optinlete3=expr_optenve3optinletst=stmt1envstin[A.For(A.ForClassic(e1,e2,e3),st)]|C.ForIn(_,_,lhs_var,_,e2,_,st)->lete1=matchlhs_varwith|C.LHS2e->Right(exprenve)|C.ForVar((vkind,tok),binding)->letvars=var_bindingenvvkindbindingin(matchvarswith|[var]->Leftvar|_->raise(TodoConstruct("For in with (pattern) vars?",tok)))inlete2=exprenve2inletst=stmt1envstin[A.For(A.ForIn(e1,e2),st)]|C.ForOf(_,_,lhs_var,tokof,e2,_,st)->(tryTranspile_js.forof(lhs_var,tokof,e2,st)(exprenv,stmtenv,var_bindingenv)withFailures->raise(TodoConstruct(spf"ForOf:%s"s,tokof)))|C.Switch(_,e,xs)->lete=e|>C.unparen|>exprenvinletxs=xs|>C.unparen|>List.map(case_clauseenv)in[A.Switch(e,xs)]|C.Continue(_,lopt,_)->[A.Continue(optlabelenvlopt)]|C.Break(_,lopt,_)->[A.Break(optlabelenvlopt)]|C.Return(_,eopt,_)->[A.Return(expr_optenveopt)]|C.With(tok,_e,_st)->raise(TodoConstruct("with",tok))|C.Labeled(lbl,_,st)->letlbl=labelenvlblinletst=stmt1envstin[A.Label(lbl,st)]|C.Throw(_,e,_)->lete=exprenvein[A.Throwe]|C.Try(_,st,catchopt,finally_opt)->letst=stmt1envstinletcatchopt=opt(funenv(_,arg,st)->letarg=nameenv(C.unparenarg)inletst=stmt1envstin(arg,st))envcatchoptinletfinally_opt=opt(funenv(_,st)->stmt1envst)envfinally_optin[A.Try(st,catchopt,finally_opt)]andstmt_of_stmtsxs=matchxswith|[]->A.Block[]|[x]->x|xs->A.Blockxsandstmt1envst=stmtenvst|>stmt_of_stmtsandcase_clauseenv=function|C.Default(_,_,xs)->A.Default(stmt1_item_listenvxs)|C.Case(_,e,_,xs)->lete=exprenveinA.Case(e,stmt1_item_listenvxs)andstmt_item_listenvitems=letrecauxaccenv=function|[]->List.revacc|>List.flatten|x::xs->letys=itemNoneenvxinletenv=letlocals=ys|>Common.map_filter(funx->matchxwith|A.VarDeclv->Somev|_->None)inadd_localsenvlocalsinaux(ys::acc)envxsinaux[]envitemsandstmt1_item_listenvitems=stmt_item_listenvitems|>stmt_of_stmts(* ------------------------------------------------------------------------- *)(* Expression *)(* ------------------------------------------------------------------------- *)andexprenv=function|C.Lx->literalenvx|C.V(s,tok)->letresolved=try(List.assocsenv.locals)withNot_found->ifHashtbl.memenv.varssthenA.LocalelseA.NotResolvedin(matchresolvedwith|A.Local|A.Param->A.Id((s,tok),refresolved)|A.NotResolved|A.Global_->(matchswith|"eval"->A.IdSpecial(A.Eval,tok)|"undefined"->A.IdSpecial(A.Undefined,tok)(* commonJS *)|"require"->A.IdSpecial(A.Require,tok)|"exports"->A.IdSpecial(A.Exports,tok)|"module"->A.IdSpecial(A.Module,tok)(* AMD *)|"define"->A.IdSpecial(A.Define,tok)(* reflection *)|"arguments"->A.IdSpecial(A.Arguments,tok)|_->A.Id((s,tok),refresolved)))|C.Thistok->A.IdSpecial(A.This,tok)|C.Supertok->A.IdSpecial(A.Super,tok)|C.U((op,tok),e)->letspecial=unopenvopinlete=exprenveinA.Apply(A.IdSpecial(special,tok),[e])|C.B(e1,op,e2)->lete1=exprenve1inlete2=exprenve2inbinopenvope1e2|C.Period(e,_,n)->lete=exprenveinA.ObjAccess(e,A.PN(nameenvn))|C.Bracket(e,e2)->(* let e = expr env e in
A.ObjAccess (e, A.PN_Computed (expr env (paren e2)))
*)lete=exprenveinlete2=exprenv(C.unparene2)inA.ArrAccess(e,e2)|C.Objectxs->A.Obj(xs|>C.unparen|>C.uncomma|>List.map(propertyenv))|C.Array(tok,xs,_)->(* A.Obj (array_obj env 0 tok xs) *)A.Arr(array_arrenvtokxs)|C.Apply(e,es)->lete=exprenveinletes=List.map(exprenv)(es|>C.unparen|>C.uncomma)inA.Apply(e,es)|C.Conditional(e1,_,e2,_,e3)->lete1=exprenve1inlete2=exprenve2inlete3=exprenve3inA.Conditional(e1,e2,e3)|C.Assign(e1,(op,tok),e2)->lete1=exprenve1inlete2=exprenve2inletspecialop=A.IdSpecial(A.ArithOpop,tok)in(matchopwith|C.A_eq->A.Assign(e1,e2)(* less: should use intermediate? can unsugar like this? *)|C.A_add->A.Assign(e1,A.Apply(specialG.Plus,[e1;e2]))|C.A_sub->A.Assign(e1,A.Apply(specialG.Minus,[e1;e2]))|C.A_mul->A.Assign(e1,A.Apply(specialG.Mult,[e1;e2]))|C.A_div->A.Assign(e1,A.Apply(specialG.Div,[e1;e2]))|C.A_mod->A.Assign(e1,A.Apply(specialG.Mod,[e1;e2]))|C.A_lsl->A.Assign(e1,A.Apply(specialG.LSL,[e1;e2]))|C.A_lsr->A.Assign(e1,A.Apply(specialG.LSR,[e1;e2]))|C.A_asr->A.Assign(e1,A.Apply(specialG.ASR,[e1;e2]))|C.A_and->A.Assign(e1,A.Apply(specialG.BitAnd,[e1;e2]))|C.A_or->A.Assign(e1,A.Apply(specialG.BitOr,[e1;e2]))|C.A_xor->A.Assign(e1,A.Apply(specialG.BitXor,[e1;e2])))|C.Seq(e1,tok,e2)->lete1=exprenve1inlete2=exprenve2inA.Apply(A.IdSpecial(A.Seq,tok),[e1;e2])|C.Functionx->letfun_=func_declenvxin(matchx.C.f_kindwith|C.F_func(_,None)->A.Fun(fun_,None)|C.F_func(_,Somen)->A.Fun(fun_,Some(nameenvn))|_->raise(UnhandledConstruct("weird lambda",fst3x.C.f_params)))|C.Classx->letclass_=class_declenvxin(matchx.C.c_namewith|None->A.Class(class_,None)|Somen->A.Class(class_,Some(nameenvn)))|C.Arrowx->A.Fun(arrow_funcenvx,None)|C.Yield(tok,star,eopt)->letspecial=ifstar=NonethenA.YieldelseA.YieldStarinlete=expr_optenveoptinA.Apply(A.IdSpecial(special,tok),[e])|C.Await(tok,e)->lete=exprenveinA.Apply(A.IdSpecial(A.Await,tok),[e])|C.NewTarget(tok,_,_)->A.Apply(A.IdSpecial(A.NewTarget,tok),[])|C.Encaps(name_opt,tok,xs,_)->letspecial=A.Encapsname_optinletxs=List.map(encapsenv)xsinA.Apply(A.IdSpecial(special,tok),xs)|C.XhpHtmlx->Transpile_js.xhp(exprenv)x|C.Parenx->exprenv(C.unparenx)|C.Ellipsesx->A.Ellipsesxandexpr_optenv=function|None->A.Nop|Somee->exprenveandliteral_env=function|C.Boolx->A.Boolx|C.Numx->A.Numx|C.Stringx->A.Stringx|C.Regexpx->A.Regexpx|C.Nulltok->A.IdSpecial(A.Null,tok)andunop_env=function|C.U_new->A.New|C.U_delete->A.Delete|C.U_typeof->A.Typeof|C.U_void->A.Void|C.U_pre_increment->A.IncrDecr(G.Incr,G.Prefix)|C.U_pre_decrement->A.IncrDecr(G.Decr,G.Prefix)|C.U_post_increment->A.IncrDecr(G.Incr,G.Postfix)|C.U_post_decrement->A.IncrDecr(G.Decr,G.Postfix)|C.U_plus->A.ArithOpG.Plus|C.U_minus->A.ArithOpG.Minus|C.U_not->A.ArithOpG.Not|C.U_bitnot->A.ArithOpG.BitNot|C.U_spread->A.Spreadandbinop_env(op,tok)e1e2=letres=matchopwith|C.B_instanceof->LeftA.Instanceof|C.B_in->LeftA.In|C.B_add->Left(A.ArithOpG.Plus)|C.B_sub->Left(A.ArithOpG.Minus)|C.B_mul->Left(A.ArithOpG.Mult)|C.B_div->Left(A.ArithOpG.Div)|C.B_mod->Left(A.ArithOpG.Mod)|C.B_expo->Left(A.ArithOpG.Pow)|C.B_lt->Left(A.ArithOpG.Lt)|C.B_gt->Left(A.ArithOpG.Gt)|C.B_lsr->Left(A.ArithOpG.LSR)|C.B_asr->Left(A.ArithOpG.ASR)|C.B_lsl->Left(A.ArithOpG.LSL)|C.B_bitand->Left(A.ArithOpG.BitAnd)|C.B_bitor->Left(A.ArithOpG.BitOr)|C.B_bitxor->Left(A.ArithOpG.BitXor)|C.B_and->Left(A.ArithOpG.And)|C.B_or->Left(A.ArithOpG.Or)|C.B_equal->Left(A.ArithOpG.Eq)|C.B_physequal->Left(A.ArithOpG.PhysEq)|C.B_le->Left(A.ArithOpG.LtE)|C.B_ge->Left(A.ArithOpG.GtE)|C.B_notequal->Left(A.ArithOpG.NotEq)|C.B_physnotequal->Left(A.ArithOpG.NotPhysEq)inmatchreswith|Leftspecial->A.Apply(A.IdSpecial(special,tok),[e1;e2])|Rightx->xandencapsenv=function|C.EncapsStringx->A.Stringx|C.EncapsExpr(_,e,_)->exprenve(* ------------------------------------------------------------------------- *)(* Entities *)(* ------------------------------------------------------------------------- *)andvar_bindingenvvkind=function|C.VarClassicx->[variable_declarationenvvkindx]|C.VarPatternx->(tryTranspile_js.var_pattern(exprenv,nameenv,property_nameenv)xwithFailures->raise(TodoConstruct(spf"VarPattern:%s"s,(C.Patternx.C.vpat)|>Lib_parsing_js.ii_of_any|>List.hd)))andvariable_declarationenvvkindx=letn=nameenvx.C.v_nameinletinit=init_optenvx.C.v_initinletvkind=var_kindenvvkindin{A.v_name=n;v_init=init;v_kind=vkind;v_resolved=not_resolved()}andinit_optenvini=matchiniwith(* less Undefined? *)|None->A.Nop|Some(_,e)->exprenveandvar_kind_env=function|C.Var->A.Var|C.Const->A.Const|C.Let->A.Letandfunc_declenvx=(* bugfix: each function has its own vars *)letenv={envwithvars=Hashtbl.copyenv.vars}inletprops=func_propsenvx.C.f_kindx.C.f_propertiesinletparams_and_vars=x.C.f_params|>C.unparen|>C.uncomma|>Common.index_list_0|>List.map(fun(p,idx)->parameter_bindingenvidxp)inletparams=params_and_vars|>List.mapfstinletvars=params_and_vars|>List.mapsnd|>List.flatteninletenv=add_paramsenvparamsinletxs=stmt_item_listenv(x.C.f_body|>C.unparen)inletbody=stmt_of_stmts(vars@xs)in{A.f_props=props;f_params=params;f_body=body}andfunc_props_envkindprops=(matchkindwith|C.F_func_->[]|C.F_method_->[]|C.F_get_->[A.Get]|C.F_set_->[A.Set])@(props|>List.map(function|C.Generator_->A.Generator|C.Async_->A.Async))andparameter_bindingenvidx=function|C.ParamClassicp->parameterenvp,[]|C.ParamPatternx->lettok=(C.Patternx.C.ppat)|>Lib_parsing_js.ii_of_any|>List.hdinletintermediate=spf"!arg%d!"idx,tokinletpat=x.C.ppatin(tryletvars=Transpile_js.compile_pattern(exprenv,nameenv,property_nameenv)intermediatepatinletp={C.p_name=intermediate;C.p_type=x.C.ppat_type;C.p_dots=None;C.p_default=matchx.C.ppat_defaultwith|None->None;|Some(tok,e)->Some(C.DSome(tok,e))}inletp=parameterenvpinp,vars|>List.map(funx->A.VarDeclx)withFailures->raise(TodoConstruct(spf"ParamPattern:%s"s,tok)))andparameterenvp=letname=nameenvp.C.p_nameinletd=optdefaultenvp.C.p_defaultinletdots=p.C.p_dots<>Nonein{A.p_name=name;p_default=d;p_dots=dots}anddefaultenv=function(* less: use Undefined? *)|C.DNone_->A.Nop|C.DSome(_,e)->exprenveandarrow_funcenvx=(* todo: they can have some too, but not in CST for now *)letprops=[]inletbindings=matchx.C.a_paramswith|C.ASingleParamx->[x]|C.AParamsxs->xs|>C.unparen|>C.uncommainletparams_and_vars=bindings|>Common.index_list_0|>List.map(fun(p,idx)->parameter_bindingenvidxp)inletparams=params_and_vars|>List.mapfstinletvars=params_and_vars|>List.mapsnd|>List.flatteninletenv=add_paramsenvparamsinletxs=matchx.C.a_bodywith(* Javascript has implicit returns for arrows like that *)|C.AExpre->[A.Return(exprenve)]|C.ABodyxs->stmt_item_listenv(xs|>C.unparen)inletbody=stmt_of_stmts(vars@xs)in{A.f_props=props;f_params=params;f_body=body}andpropertyenv=function|C.P_field(pname,_,e)->letpname=property_nameenvpnameinlete=exprenveinletprops=[]inA.Field(pname,props,e)|C.P_methodx->method_env[]x|C.P_shorthandn->letn=nameenvninA.Field(A.PNn,[],A.Id(n,not_resolved()))|C.P_spread(_,e)->lete=exprenveinA.FieldSpreadeand_array_objenvidxtokxs=matchxswith|[]->[]|x::xs->(matchxwith|Righttok->_array_objenv(idx+1)tokxs|Lefte->letn=A.PN(string_of_intidx,tok)inlete=exprenveinletelt=A.Field(n,[],e)inelt::_array_objenvidxtokxs)andarray_arrenvtokxs=matchxswith|[]->[]|[Right_]->[]|[Lefte]->[exprenve]|(Lefte)::(Righttok)::xs->lete=exprenveine::array_arrenvtokxs|(Right_)::xs->lete=A.Nopine::array_arrenvtokxs|(Left_)::(Left_)::_->raise(TodoConstruct("array_arr, 2 left? impossible?",tok))andclass_declenvx=letextends=opt(funenv(_,typ)->nominal_typeenvtyp)envx.C.c_extendsinletxs=x.C.c_body|>C.unparen|>List.map(class_elementenv)|>List.flattenin{A.c_extends=extends;c_body=xs}andnominal_typeenv(e,_)=exprenveandclass_elementenv=function|C.C_field(fld,_)->letpn=property_nameenvfld.C.fld_nameinletprops=[]in(* TODO fld.fld_static *)lete=init_optenvfld.C.fld_initin[A.Field(pn,props,e)]|C.C_method(static_opt,x)->letprops=matchstatic_optwith|None->[]|Some_->[A.Static]in[method_envpropsx]|C.C_extrasemicolon_->[]andmethod_envpropsx=letfun_=func_declenvxinletpname,fprops=matchx.C.f_kindwith|C.F_methodpn->property_nameenvpn,[]|C.F_get(_,pn)->property_nameenvpn,[A.Get]|C.F_set(_,pn)->property_nameenvpn,[A.Set]|C.F_func_->raise(UnhandledConstruct("weird method decl: unexpected F_func",fst3x.C.f_params))inletfun_={fun_withA.f_props=fprops@fun_.A.f_props}inA.Field(pname,props,A.Fun(fun_,None))(* ------------------------------------------------------------------------- *)(* Misc *)(* ------------------------------------------------------------------------- *)letanyx=letenv=empty_env()inmatchxwith|C.Exprx->A.Expr(exprenvx)|C.Stmtx->A.Stmt(stmt1envx)|C.Pattern_x->raiseTodo|C.Item_x->raiseTodo|C.Program_x->raiseTodo