123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778(* Js_of_ocaml compiler
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2019 Ty Overby, Jane Street Group LLC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program 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
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)open!Stdlibtypem=|Replace|Countofintrefclassmacro_mapper~flags=object(m)inheritJs_traverse.mapassupermethodexpressionx=letmoduleJ=Javascriptinmatchxwith|J.ECall(J.EVar(J.S{name=Utf8name;_}),(ANormal|ANullish),args,_)->(matchname,argswith|"FLAG",[J.Arg(J.EStr(Utf8s))]->(matchflagswith|Replace->leti=ifConfig.Flag.findsthen1lelse0linJ.ENum(J.Num.of_int32i)|Countcount->incrcount;super#expressionx)|"BLOCK",J.Arg(J.ENumtag)::(_::_asargs)whenList.for_allargs~f:(function|J.Arg_->true|J.ArgSpread_->false)->lettag=Int32.to_int(J.Num.to_int32tag)inletargs=List.mapargs~f:(function|J.Arge->J.Element(m#expressione)|J.ArgSpread_->assertfalse)inMlvalue.Block.make~tag~args|"TAG",[J.Arge]->Mlvalue.Block.tag(m#expressione)|"LENGTH",[J.Arge]->Mlvalue.Array.length(m#expressione)|"FIELD",[J.Arge;J.Arg(J.ENumn)]->letidx=Int32.to_int(J.Num.to_int32n)inMlvalue.Block.field(m#expressione)idx|"FIELD",[_;J.Arg(J.EUn(J.Neg,_))]->failwith"Negative field indexes are not allowed"|"ISBLOCK",[J.Arge]->Mlvalue.is_block(m#expressione)|(("BLOCK"|"TAG"|"LENGTH"|"FIELD"|"ISBLOCK"|"FLAG")asname),_->failwith(Format.sprintf"macro %s called with inappropriate arguments"name)|_->super#expressionx)|_->super#expressionxendletf~flagsjs=letcount=ref0inletflags=matchflagswith|true->Replace|false->Countcountinlettrav=newmacro_mapper~flagsinletjs=trav#programjsinjs,!count>0