12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788(* 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.findsthenTargetint.oneelseTargetint.zeroinJ.ENum(J.Num.of_targetinti)|Countcount->incrcount;super#expressionx)|"CONFIG",[J.Arg(J.EStr(Utf8"effects"))]->(matchflagswith|Replace->lets=Build_info.string_of_effects_backend(Config.effects())inJ.EStr(Utf8_string.of_string_exns)|Countcount->incrcount;super#expressionx)|"CONFIG",[J.Arg(J.EStr(Utf8s))]->failwith("unsupported CONFIG parameter "^s)|"BLOCK",J.Arg(J.ENumtag)::(_::_asargs)whenList.for_allargs~f:(function|J.Arg_->true|J.ArgSpread_->false)->lettag=Targetint.to_int_exn(J.Num.to_targetinttag)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=Targetint.to_int_exn(J.Num.to_targetintn)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