adamc@25: (* Copyright (c) 2008, Adam Chlipala adamc@25: * All rights reserved. adamc@25: * adamc@25: * Redistribution and use in source and binary forms, with or without adamc@25: * modification, are permitted provided that the following conditions are met: adamc@25: * adamc@25: * - Redistributions of source code must retain the above copyright notice, adamc@25: * this list of conditions and the following disclaimer. adamc@25: * - Redistributions in binary form must reproduce the above copyright notice, adamc@25: * this list of conditions and the following disclaimer in the documentation adamc@25: * and/or other materials provided with the distribution. adamc@25: * - The names of contributors may not be used to endorse or promote products adamc@25: * derived from this software without specific prior written permission. adamc@25: * adamc@25: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" adamc@25: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE adamc@25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE adamc@25: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE adamc@25: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR adamc@25: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF adamc@25: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS adamc@25: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN adamc@25: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) adamc@25: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE adamc@25: * POSSIBILITY OF SUCH DAMAGE. adamc@25: *) adamc@25: adamc@25: structure Monoize :> MONOIZE = struct adamc@25: adamc@25: structure E = ErrorMsg adamc@25: structure Env = CoreEnv adamc@25: adamc@25: structure L = Core adamc@25: structure L' = Mono adamc@25: adamc@25: val dummyTyp = (L'.TNamed 0, E.dummySpan) adamc@25: adamc@25: fun monoName env (all as (c, loc)) = adamc@25: let adamc@25: fun poly () = adamc@25: (E.errorAt loc "Unsupported name constructor"; adamc@25: Print.eprefaces' [("Constructor", CorePrint.p_con env all)]; adamc@25: "") adamc@25: in adamc@25: case c of adamc@25: L.CName s => s adamc@25: | _ => poly () adamc@25: end adamc@25: adamc@25: fun monoType env (all as (c, loc)) = adamc@25: let adamc@25: fun poly () = adamc@25: (E.errorAt loc "Unsupported type constructor"; adamc@25: Print.eprefaces' [("Constructor", CorePrint.p_con env all)]; adamc@25: dummyTyp) adamc@25: in adamc@25: case c of adamc@25: L.TFun (c1, c2) => (L'.TFun (monoType env c1, monoType env c2), loc) adamc@25: | L.TCFun _ => poly () adamc@25: | L.TRecord (L.CRecord ((L.KType, _), xcs), _) => adamc@25: (L'.TRecord (map (fn (x, t) => (monoName env x, monoType env t)) xcs), loc) adamc@25: | L.TRecord _ => poly () adamc@25: adamc@94: | L.CApp ((L.CFfi ("Basis", "xml"), _), _) => (L'.TFfi ("Basis", "string"), loc) adamc@94: adamc@25: | L.CRel _ => poly () adamc@25: | L.CNamed n => (L'.TNamed n, loc) adamc@51: | L.CFfi mx => (L'.TFfi mx, loc) adamc@25: | L.CApp _ => poly () adamc@25: | L.CAbs _ => poly () adamc@25: adamc@25: | L.CName _ => poly () adamc@25: adamc@25: | L.CRecord _ => poly () adamc@25: | L.CConcat _ => poly () adamc@69: | L.CFold _ => poly () adamc@87: | L.CUnit => poly () adamc@25: end adamc@25: adamc@25: val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan) adamc@25: adamc@120: fun fooifyExp name env = adamc@120: let adamc@120: fun fooify (e, tAll as (t, loc)) = adamc@120: case #1 e of adamc@120: L'.EClosure (fnam, [(L'.ERecord [], _)]) => adamc@120: let adamc@120: val (_, _, _, s) = Env.lookupENamed env fnam adamc@120: in adamc@120: (L'.EPrim (Prim.String s), loc) adamc@120: end adamc@120: | L'.EClosure (fnam, args) => adamc@120: let adamc@120: val (_, ft, _, s) = Env.lookupENamed env fnam adamc@120: val ft = monoType env ft adamc@111: adamc@120: fun attrify (args, ft, e) = adamc@120: case (args, ft) of adamc@120: ([], _) => e adamc@120: | (arg :: args, (L'.TFun (t, ft), _)) => adamc@120: (L'.EStrcat (e, adamc@120: (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc), adamc@120: fooify (arg, t)), loc)), loc) adamc@120: | _ => (E.errorAt loc "Type mismatch encoding attribute"; adamc@120: e) adamc@120: in adamc@120: attrify (args, ft, (L'.EPrim (Prim.String s), loc)) adamc@120: end adamc@120: | _ => adamc@120: case t of adamc@120: L'.TFfi ("Basis", "string") => (L'.EFfiApp ("Basis", name ^ "ifyString", [e]), loc) adamc@120: | L'.TFfi ("Basis", "int") => (L'.EFfiApp ("Basis", name ^ "ifyInt", [e]), loc) adamc@120: | L'.TFfi ("Basis", "float") => (L'.EFfiApp ("Basis", name ^ "ifyFloat", [e]), loc) adamc@120: | L'.TRecord [] => (L'.EPrim (Prim.String ""), loc) adamc@111: adamc@120: | _ => (E.errorAt loc "Don't know how to encode attribute type"; adamc@120: Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)]; adamc@120: dummyExp) adamc@120: in adamc@120: fooify adamc@120: end adamc@120: adamc@120: val attrifyExp = fooifyExp "attr" adamc@120: val urlifyExp = fooifyExp "url" adamc@105: adamc@25: fun monoExp env (all as (e, loc)) = adamc@25: let adamc@25: fun poly () = adamc@25: (E.errorAt loc "Unsupported expression"; adamc@25: Print.eprefaces' [("Expression", CorePrint.p_exp env all)]; adamc@25: dummyExp) adamc@25: in adamc@25: case e of adamc@25: L.EPrim p => (L'.EPrim p, loc) adamc@25: | L.ERel n => (L'.ERel n, loc) adamc@25: | L.ENamed n => (L'.ENamed n, loc) adamc@51: | L.EFfi mx => (L'.EFfi mx, loc) adamc@51: | L.EFfiApp (m, x, es) => (L'.EFfiApp (m, x, map (monoExp env) es), loc) adamc@94: adamc@94: | L.EApp ((L.ECApp ((L.EFfi ("Basis", "cdata"), _), adamc@94: _), _), se) => monoExp env se adamc@95: | L.EApp ( adamc@95: (L.EApp ( adamc@95: (L.ECApp ( adamc@95: (L.ECApp ( adamc@95: (L.ECApp ( adamc@95: (L.EFfi ("Basis", "join"), adamc@95: _), _), _), adamc@95: _), _), adamc@95: _), _), adamc@95: xml1), _), adamc@95: xml2) => (L'.EStrcat (monoExp env xml1, monoExp env xml2), loc) adamc@95: adamc@95: | L.EApp ( adamc@95: (L.EApp ( adamc@104: (L.EApp ( adamc@95: (L.ECApp ( adamc@104: (L.ECApp ( adamc@104: (L.ECApp ( adamc@104: (L.ECApp ( adamc@104: (L.EFfi ("Basis", "tag"), adamc@104: _), _), _), _), _), _), _), _), _), adamc@104: attrs), _), adamc@95: tag), _), adamc@95: xml) => adamc@95: let adamc@95: fun getTag (e, _) = adamc@95: case e of adamc@95: L.EFfi ("Basis", tag) => tag adamc@95: | _ => (E.errorAt loc "Non-constant XML tag"; adamc@95: Print.eprefaces' [("Expression", CorePrint.p_exp env tag)]; adamc@95: "") adamc@95: adamc@95: val tag = getTag tag adamc@95: adamc@104: val attrs = monoExp env attrs adamc@104: adamc@104: val tagStart = adamc@104: case #1 attrs of adamc@104: L'.ERecord xes => adamc@104: let adamc@104: fun lowercaseFirst "" = "" adamc@104: | lowercaseFirst s = str (Char.toLower (String.sub (s, 0))) ^ String.extract (s, 1, NONE) adamc@104: adamc@104: val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc) adamc@104: in adamc@105: foldl (fn ((x, e, t), s) => adamc@104: let adamc@104: val xp = " " ^ lowercaseFirst x ^ "=\"" adamc@120: adamc@120: val fooify = adamc@120: case x of adamc@120: "Link" => urlifyExp adamc@120: | _ => attrifyExp adamc@104: in adamc@104: (L'.EStrcat (s, adamc@104: (L'.EStrcat ((L'.EPrim (Prim.String xp), loc), adamc@120: (L'.EStrcat (fooify env (e, t), adamc@104: (L'.EPrim (Prim.String "\""), loc)), adamc@104: loc)), adamc@104: loc)), loc) adamc@104: end) adamc@104: s xes adamc@104: end adamc@104: | _ => raise Fail "Attributes!" adamc@104: adamc@95: fun normal () = adamc@104: (L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc), adamc@95: (L'.EStrcat (monoExp env xml, adamc@95: (L'.EPrim (Prim.String (String.concat [""])), loc)), loc)), adamc@95: loc) adamc@104: adamc@104: adamc@95: in adamc@95: case xml of adamc@95: (L.EApp ((L.ECApp ((L.EFfi ("Basis", "cdata"), _), adamc@95: _), _), (L.EPrim (Prim.String s), _)), _) => adamc@95: if CharVector.all Char.isSpace s then adamc@104: (L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc) adamc@95: else adamc@95: normal () adamc@95: | _ => normal () adamc@95: end adamc@94: adamc@25: | L.EApp (e1, e2) => (L'.EApp (monoExp env e1, monoExp env e2), loc) adamc@26: | L.EAbs (x, dom, ran, e) => adamc@26: (L'.EAbs (x, monoType env dom, monoType env ran, monoExp (Env.pushERel env x dom) e), loc) adamc@25: | L.ECApp _ => poly () adamc@25: | L.ECAbs _ => poly () adamc@25: adamc@29: | L.ERecord xes => (L'.ERecord (map (fn (x, e, t) => (monoName env x, monoExp env e, monoType env t)) xes), loc) adamc@25: | L.EField (e, x, _) => (L'.EField (monoExp env e, monoName env x), loc) adamc@73: | L.EFold _ => poly () adamc@102: | L.EWrite e => (L'.EWrite (monoExp env e), loc) adamc@110: adamc@111: | L.EClosure (n, es) => (L'.EClosure (n, map (monoExp env) es), loc) adamc@25: end adamc@25: adamc@25: fun monoDecl env (all as (d, loc)) = adamc@25: let adamc@25: fun poly () = adamc@25: (E.errorAt loc "Unsupported declaration"; adamc@25: Print.eprefaces' [("Declaration", CorePrint.p_decl env all)]; adamc@25: NONE) adamc@25: in adamc@25: case d of adamc@25: L.DCon _ => NONE adamc@109: | L.DVal (x, n, t, e, s) => SOME (Env.pushENamed env x n t (SOME e) s, adamc@109: (L'.DVal (x, n, monoType env t, monoExp env e, s), loc)) adamc@115: | L.DExport n => adamc@115: let adamc@120: val (_, t, _, s) = Env.lookupENamed env n adamc@120: adamc@120: fun unwind (t, _) = adamc@120: case t of adamc@120: L.TFun (dom, ran) => dom :: unwind ran adamc@120: | _ => [] adamc@120: adamc@120: val ts = map (monoType env) (unwind t) adamc@115: in adamc@120: SOME (env, (L'.DExport (s, n, ts), loc)) adamc@115: end adamc@25: end adamc@25: adamc@25: fun monoize env ds = adamc@25: let adamc@25: val (_, ds) = List.foldl (fn (d, (env, ds)) => adamc@25: case monoDecl env d of adamc@25: NONE => (env, ds) adamc@25: | SOME (env, d) => (env, d :: ds)) (env, []) ds adamc@25: in adamc@25: rev ds adamc@25: end adamc@25: adamc@25: end