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@168: val dummyTyp = (L'.TDatatype (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@143: | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) => adamc@143: (L'.TFfi ("Basis", "string"), loc) adamc@183: | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) => adamc@183: (L'.TFfi ("Basis", "string"), loc) adamc@94: adamc@25: | L.CRel _ => poly () adamc@168: | L.CNamed n => adamc@168: let adamc@168: val (_, xncs) = Env.lookupDatatype env n adamc@168: adamc@168: val xncs = map (fn (x, n, to) => (x, n, Option.map (monoType env) to)) xncs adamc@168: in adamc@168: (L'.TDatatype (n, xncs), loc) adamc@168: end 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@179: structure IM = IntBinaryMap adamc@179: adamc@179: datatype foo_kind = adamc@179: Attr adamc@179: | Url adamc@179: adamc@179: fun fk2s fk = adamc@179: case fk of adamc@179: Attr => "attr" adamc@179: | Url => "url" adamc@179: adamc@179: structure Fm :> sig adamc@179: type t adamc@179: adamc@179: val empty : int -> t adamc@179: adamc@179: val lookup : t -> foo_kind -> int -> (int -> t -> L'.decl * t) -> t * int adamc@179: val enter : t -> t adamc@179: val decls : t -> L'.decl list adamc@179: end = struct adamc@179: adamc@179: structure M = BinaryMapFn(struct adamc@179: type ord_key = foo_kind adamc@179: fun compare x = adamc@179: case x of adamc@179: (Attr, Attr) => EQUAL adamc@179: | (Attr, _) => LESS adamc@179: | (_, Attr) => GREATER adamc@179: adamc@179: | (Url, Url) => EQUAL adamc@179: end) adamc@179: adamc@179: type t = { adamc@179: count : int, adamc@179: map : int IM.map M.map, adamc@179: decls : L'.decl list adamc@179: } adamc@179: adamc@179: fun empty count = { adamc@179: count = count, adamc@179: map = M.empty, adamc@179: decls = [] adamc@179: } adamc@179: adamc@179: fun enter ({count, map, ...} : t) = {count = count, map = map, decls = []} adamc@179: fun decls ({decls, ...} : t) = decls adamc@179: adamc@179: fun lookup (t as {count, map, decls}) k n thunk = adamc@120: let adamc@179: val im = Option.getOpt (M.find (map, k), IM.empty) adamc@179: in adamc@179: case IM.find (im, n) of adamc@179: NONE => adamc@179: let adamc@179: val n' = count adamc@179: val (d, {count, map, decls}) = thunk count {count = count + 1, adamc@179: map = M.insert (map, k, IM.insert (im, n, n')), adamc@179: decls = decls} adamc@179: in adamc@179: ({count = count, adamc@179: map = map, adamc@179: decls = d :: decls}, n') adamc@179: end adamc@179: | SOME n' => (t, n') adamc@179: end adamc@179: adamc@179: end adamc@185: adamc@185: adamc@185: fun capitalize s = adamc@185: if s = "" then adamc@185: s adamc@185: else adamc@185: str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE) adamc@179: adamc@179: fun fooifyExp fk env = adamc@179: let adamc@179: fun fooify fm (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@183: ((L'.EPrim (Prim.String ("/" ^ s)), loc), fm) 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@179: fun attrify (args, ft, e, fm) = adamc@120: case (args, ft) of adamc@179: ([], _) => (e, fm) adamc@120: | (arg :: args, (L'.TFun (t, ft), _)) => adamc@179: let adamc@179: val (arg', fm) = fooify fm (arg, t) adamc@179: in adamc@179: attrify (args, ft, adamc@179: (L'.EStrcat (e, adamc@179: (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc), adamc@179: arg'), loc)), loc), adamc@179: fm) adamc@179: end adamc@120: | _ => (E.errorAt loc "Type mismatch encoding attribute"; adamc@179: (e, fm)) adamc@120: in adamc@183: attrify (args, ft, (L'.EPrim (Prim.String ("/" ^ s)), loc), fm) adamc@120: end adamc@120: | _ => adamc@120: case t of adamc@185: L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [e]), loc), fm) adamc@179: | L'.TRecord [] => ((L'.EPrim (Prim.String ""), loc), fm) adamc@111: adamc@179: | L'.TDatatype (i, _) => adamc@179: let adamc@179: fun makeDecl n fm = adamc@179: let adamc@179: val (x, xncs) = Env.lookupDatatype env i adamc@179: adamc@179: val (branches, fm) = adamc@179: ListUtil.foldlMap adamc@179: (fn ((x, n, to), fm) => adamc@179: case to of adamc@179: NONE => adamc@179: (((L'.PCon (L'.PConVar n, NONE), loc), adamc@179: (L'.EPrim (Prim.String x), loc)), adamc@179: fm) adamc@179: | SOME t => adamc@179: let adamc@182: val t = monoType env t adamc@182: val (arg, fm) = fooify fm ((L'.ERel 0, loc), t) adamc@179: in adamc@182: (((L'.PCon (L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc), adamc@179: (L'.EStrcat ((L'.EPrim (Prim.String (x ^ "/")), loc), adamc@179: arg), loc)), adamc@179: fm) adamc@179: end) adamc@179: fm xncs adamc@179: adamc@179: val dom = tAll adamc@179: val ran = (L'.TFfi ("Basis", "string"), loc) adamc@179: in adamc@179: ((L'.DValRec [(fk2s fk ^ "ify_" ^ x, adamc@179: n, adamc@179: (L'.TFun (dom, ran), loc), adamc@179: (L'.EAbs ("x", adamc@179: dom, adamc@179: ran, adamc@179: (L'.ECase ((L'.ERel 0, loc), adamc@179: branches, adamc@182: {disc = dom, adamc@182: result = ran}), loc)), loc), adamc@179: "")], loc), adamc@179: fm) adamc@179: end adamc@179: adamc@179: val (fm, n) = Fm.lookup fm fk i makeDecl adamc@179: in adamc@179: ((L'.EApp ((L'.ENamed n, loc), e), loc), fm) adamc@179: end adamc@164: 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@179: (dummyExp, fm)) adamc@120: in adamc@120: fooify adamc@120: end adamc@120: adamc@179: val attrifyExp = fooifyExp Attr adamc@179: val urlifyExp = fooifyExp Url adamc@105: adamc@143: datatype 'a failable_search = adamc@143: Found of 'a adamc@143: | NotFound adamc@143: | Error adamc@143: adamc@153: structure St :> sig adamc@153: type t adamc@153: adamc@153: val empty : t adamc@153: adamc@153: val radioGroup : t -> string option adamc@153: val setRadioGroup : t * string -> t adamc@153: end = struct adamc@153: adamc@153: type t = { adamc@153: radioGroup : string option adamc@153: } adamc@153: adamc@153: val empty = {radioGroup = NONE} adamc@153: adamc@153: fun radioGroup (t : t) = #radioGroup t adamc@153: adamc@153: fun setRadioGroup (t : t, x) = {radioGroup = SOME x} adamc@153: adamc@153: end adamc@153: adamc@178: fun monoPatCon pc = adamc@178: case pc of adamc@178: L.PConVar n => L'.PConVar n adamc@178: | L.PConFfi mx => L'.PConFfi mx adamc@178: adamc@182: fun monoPat env (p, loc) = adamc@178: case p of adamc@178: L.PWild => (L'.PWild, loc) adamc@182: | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc) adamc@178: | L.PPrim p => (L'.PPrim p, loc) adamc@182: | L.PCon (pc, po) => (L'.PCon (monoPatCon pc, Option.map (monoPat env) po), loc) adamc@182: | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc) adamc@178: adamc@179: fun monoExp (env, st, fm) (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@179: (dummyExp, fm)) adamc@25: in adamc@25: case e of adamc@179: L.EPrim p => ((L'.EPrim p, loc), fm) adamc@179: | L.ERel n => ((L'.ERel n, loc), fm) adamc@179: | L.ENamed n => ((L'.ENamed n, loc), fm) adamc@185: | L.ECon (pc, eo) => adamc@179: let adamc@179: val (eo, fm) = adamc@179: case eo of adamc@179: NONE => (NONE, fm) adamc@179: | SOME e => adamc@179: let adamc@179: val (e, fm) = monoExp (env, st, fm) e adamc@179: in adamc@179: (SOME e, fm) adamc@179: end adamc@179: in adamc@185: ((L'.ECon (monoPatCon pc, eo), loc), fm) adamc@179: end adamc@179: | L.EFfi mx => ((L'.EFfi mx, loc), fm) adamc@179: | L.EFfiApp (m, x, es) => adamc@179: let adamc@179: val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es adamc@179: in adamc@179: ((L'.EFfiApp (m, x, es), loc), fm) adamc@179: end adamc@94: adamc@139: | L.EApp ( adamc@139: (L.ECApp ( adamc@141: (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _), adamc@139: _), _), adamc@179: se) => adamc@179: let adamc@179: val (se, fm) = monoExp (env, st, fm) se adamc@179: in adamc@179: ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm) adamc@179: end adamc@179: adamc@95: | L.EApp ( adamc@95: (L.EApp ( adamc@95: (L.ECApp ( adamc@95: (L.ECApp ( adamc@95: (L.ECApp ( adamc@139: (L.ECApp ( adamc@140: (L.EFfi ("Basis", "join"), adamc@139: _), _), _), adamc@139: _), _), adamc@95: _), _), adamc@95: _), _), adamc@95: xml1), _), adamc@179: xml2) => adamc@179: let adamc@179: val (xml1, fm) = monoExp (env, st, fm) xml1 adamc@179: val (xml2, fm) = monoExp (env, st, fm) xml2 adamc@179: in adamc@179: ((L'.EStrcat (xml1, xml2), loc), fm) adamc@179: end 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@139: (L.ECApp ( adamc@139: (L.ECApp ( adamc@139: (L.ECApp ( adamc@139: (L.ECApp ( adamc@139: (L.EFfi ("Basis", "tag"), adamc@139: _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), adamc@104: attrs), _), adamc@95: tag), _), adamc@95: xml) => adamc@95: let adamc@140: fun getTag' (e, _) = adamc@140: case e of adamc@143: L.EFfi ("Basis", tag) => (tag, []) adamc@143: | L.ECApp (e, t) => let adamc@143: val (tag, ts) = getTag' e adamc@143: in adamc@143: (tag, ts @ [t]) adamc@143: end adamc@140: | _ => (E.errorAt loc "Non-constant XML tag"; adamc@140: Print.eprefaces' [("Expression", CorePrint.p_exp env tag)]; adamc@143: ("", [])) adamc@140: adamc@95: fun getTag (e, _) = adamc@95: case e of adamc@143: L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, []) adamc@140: | L.EApp (e, (L.ERecord [], _)) => getTag' e adamc@95: | _ => (E.errorAt loc "Non-constant XML tag"; adamc@95: Print.eprefaces' [("Expression", CorePrint.p_exp env tag)]; adamc@143: ("", [])) adamc@95: adamc@143: val (tag, targs) = getTag tag adamc@95: adamc@179: val (attrs, fm) = monoExp (env, st, fm) attrs adamc@104: adamc@143: fun tagStart tag = adamc@104: case #1 attrs of adamc@104: L'.ERecord xes => adamc@104: let adamc@104: fun lowercaseFirst "" = "" adamc@143: | lowercaseFirst s = str (Char.toLower (String.sub (s, 0))) adamc@143: ^ String.extract (s, 1, NONE) adamc@104: adamc@104: val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc) adamc@104: in adamc@179: foldl (fn ((x, e, t), (s, fm)) => adamc@104: let adamc@104: val xp = " " ^ lowercaseFirst x ^ "=\"" adamc@120: adamc@120: val fooify = adamc@120: case x of adamc@185: "Href" => urlifyExp adamc@185: | "Link" => urlifyExp adamc@143: | "Action" => urlifyExp adamc@120: | _ => attrifyExp adamc@179: adamc@179: val (e, fm) = fooify env fm (e, t) adamc@104: in adamc@179: ((L'.EStrcat (s, adamc@179: (L'.EStrcat ((L'.EPrim (Prim.String xp), loc), adamc@179: (L'.EStrcat (e, adamc@179: (L'.EPrim (Prim.String "\""), adamc@179: loc)), adamc@179: loc)), adamc@179: loc)), loc), adamc@179: fm) adamc@104: end) adamc@179: (s, fm) xes adamc@104: end adamc@143: | _ => raise Fail "Non-record attributes!" adamc@104: adamc@143: fun input typ = adamc@143: case targs of adamc@155: [_, (L.CName name, _)] => adamc@179: let adamc@179: val (ts, fm) = tagStart "input" adamc@179: in adamc@179: ((L'.EStrcat (ts, adamc@179: (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")), adamc@179: loc)), loc), fm) adamc@179: end adamc@143: | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs); adamc@153: raise Fail "No name passed to input tag") adamc@104: adamc@152: fun normal (tag, extra) = adamc@143: let adamc@179: val (tagStart, fm) = tagStart tag adamc@152: val tagStart = case extra of adamc@152: NONE => tagStart adamc@152: | SOME extra => (L'.EStrcat (tagStart, extra), loc) adamc@152: adamc@143: fun normal () = adamc@179: let adamc@179: val (xml, fm) = monoExp (env, st, fm) xml adamc@179: in adamc@179: ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc), adamc@179: (L'.EStrcat (xml, adamc@179: (L'.EPrim (Prim.String (String.concat [""])), adamc@179: loc)), loc)), adamc@179: loc), adamc@179: fm) adamc@179: end adamc@143: in adamc@143: case xml of adamc@143: (L.EApp ((L.ECApp ( adamc@143: (L.ECApp ((L.EFfi ("Basis", "cdata"), _), adamc@143: _), _), adamc@143: _), _), adamc@143: (L.EPrim (Prim.String s), _)), _) => adamc@143: if CharVector.all Char.isSpace s then adamc@179: ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm) adamc@143: else adamc@143: normal () adamc@143: | _ => normal () adamc@143: end adamc@152: in adamc@152: case tag of adamc@179: "submit" => ((L'.EPrim (Prim.String ""), loc), fm) adamc@152: adamc@152: | "textbox" => adamc@152: (case targs of adamc@152: [_, (L.CName name, _)] => adamc@179: let adamc@179: val (ts, fm) = tagStart "input" adamc@179: in adamc@179: ((L'.EStrcat (ts, adamc@179: (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")), adamc@179: loc)), loc), fm) adamc@179: end adamc@152: | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs); adamc@153: raise Fail "No name passed to textarea tag")) adamc@155: | "password" => input "password" adamc@152: | "ltextarea" => adamc@152: (case targs of adamc@152: [_, (L.CName name, _)] => adamc@179: let adamc@179: val (ts, fm) = tagStart "textarea" adamc@179: val (xml, fm) = monoExp (env, st, fm) xml adamc@179: in adamc@179: ((L'.EStrcat ((L'.EStrcat (ts, adamc@179: (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc), adamc@179: (L'.EStrcat (xml, adamc@179: (L'.EPrim (Prim.String ""), adamc@179: loc)), loc)), adamc@179: loc), fm) adamc@179: end adamc@152: | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs); adamc@153: raise Fail "No name passed to ltextarea tag")) adamc@153: adamc@153: | "radio" => adamc@153: (case targs of adamc@153: [_, (L.CName name, _)] => adamc@179: monoExp (env, St.setRadioGroup (st, name), fm) xml adamc@153: | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs); adamc@153: raise Fail "No name passed to radio tag")) adamc@153: | "radioOption" => adamc@153: (case St.radioGroup st of adamc@153: NONE => raise Fail "No name for radioGroup" adamc@153: | SOME name => adamc@153: normal ("input", adamc@153: SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc))) adamc@152: adamc@154: | "lselect" => adamc@154: (case targs of adamc@154: [_, (L.CName name, _)] => adamc@179: let adamc@179: val (ts, fm) = tagStart "select" adamc@179: val (xml, fm) = monoExp (env, st, fm) xml adamc@179: in adamc@179: ((L'.EStrcat ((L'.EStrcat (ts, adamc@179: (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc), adamc@179: (L'.EStrcat (xml, adamc@179: (L'.EPrim (Prim.String ""), adamc@179: loc)), loc)), adamc@179: loc), adamc@179: fm) adamc@179: end adamc@154: | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs); adamc@154: raise Fail "No name passed to lselect tag")) adamc@154: adamc@154: | "loption" => normal ("option", NONE) adamc@154: adamc@152: | _ => normal (tag, NONE) adamc@95: end adamc@94: adamc@141: | L.EApp ((L.ECApp ( adamc@141: (L.ECApp ((L.EFfi ("Basis", "lform"), _), _), _), adamc@141: _), _), adamc@141: xml) => adamc@143: let adamc@143: fun findSubmit (e, _) = adamc@143: case e of adamc@143: L.EApp ( adamc@143: (L.EApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.EFfi ("Basis", "join"), adamc@143: _), _), _), adamc@143: _), _), adamc@143: _), _), adamc@143: _), _), adamc@143: xml1), _), adamc@143: xml2) => (case findSubmit xml1 of adamc@143: Error => Error adamc@143: | NotFound => findSubmit xml2 adamc@143: | Found e => adamc@143: case findSubmit xml2 of adamc@143: NotFound => Found e adamc@143: | _ => Error) adamc@143: | L.EApp ( adamc@143: (L.EApp ( adamc@143: (L.EApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.EFfi ("Basis", "tag"), adamc@143: _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), adamc@143: attrs), _), adamc@143: _), _), adamc@143: xml) => adamc@143: (case #1 attrs of adamc@143: L.ERecord xes => adamc@143: (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t) adamc@143: | _ => NONE) xes of adamc@143: NONE => findSubmit xml adamc@143: | SOME et => adamc@143: case findSubmit xml of adamc@143: NotFound => Found et adamc@143: | _ => Error) adamc@143: | _ => findSubmit xml) adamc@143: | _ => NotFound adamc@143: adamc@143: val (action, actionT) = case findSubmit xml of adamc@143: NotFound => raise Fail "No submit found" adamc@143: | Error => raise Fail "Not ready for multi-submit lforms yet" adamc@143: | Found et => et adamc@143: adamc@143: val actionT = monoType env actionT adamc@179: val (action, fm) = monoExp (env, st, fm) action adamc@179: val (action, fm) = urlifyExp env fm (action, actionT) adamc@179: val (xml, fm) = monoExp (env, st, fm) xml adamc@143: in adamc@179: ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "
"), loc)), loc)), loc), adamc@179: (L'.EStrcat (xml, adamc@179: (L'.EPrim (Prim.String "
"), loc)), loc)), loc), adamc@179: fm) adamc@143: end adamc@141: adamc@148: | L.EApp ((L.ECApp ( adamc@148: (L.ECApp ( adamc@148: (L.ECApp ( adamc@148: (L.ECApp ( adamc@148: (L.EFfi ("Basis", "useMore"), _), _), _), adamc@148: _), _), adamc@148: _), _), adamc@148: _), _), adamc@179: xml) => monoExp (env, st, fm) xml adamc@148: adamc@179: | L.EApp (e1, e2) => adamc@179: let adamc@179: val (e1, fm) = monoExp (env, st, fm) e1 adamc@179: val (e2, fm) = monoExp (env, st, fm) e2 adamc@179: in adamc@179: ((L'.EApp (e1, e2), loc), fm) adamc@179: end adamc@26: | L.EAbs (x, dom, ran, e) => adamc@179: let adamc@179: val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e adamc@179: in adamc@179: ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm) adamc@179: end adamc@25: | L.ECApp _ => poly () adamc@25: | L.ECAbs _ => poly () adamc@25: adamc@179: | L.ERecord xes => adamc@179: let adamc@179: val (xes, fm) = ListUtil.foldlMap adamc@179: (fn ((x, e, t), fm) => adamc@179: let adamc@179: val (e, fm) = monoExp (env, st, fm) e adamc@179: in adamc@179: ((monoName env x, adamc@179: e, adamc@179: monoType env t), fm) adamc@179: end) fm xes adamc@179: in adamc@179: ((L'.ERecord xes, loc), fm) adamc@179: end adamc@179: | L.EField (e, x, _) => adamc@179: let adamc@179: val (e, fm) = monoExp (env, st, fm) e adamc@179: in adamc@179: ((L'.EField (e, monoName env x), loc), fm) adamc@179: end adamc@149: | L.ECut _ => poly () adamc@73: | L.EFold _ => poly () adamc@177: adamc@182: | L.ECase (e, pes, {disc, result}) => adamc@179: let adamc@179: val (e, fm) = monoExp (env, st, fm) e adamc@179: val (pes, fm) = ListUtil.foldlMap adamc@179: (fn ((p, e), fm) => adamc@179: let adamc@179: val (e, fm) = monoExp (env, st, fm) e adamc@179: in adamc@182: ((monoPat env p, e), fm) adamc@179: end) fm pes adamc@179: in adamc@182: ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm) adamc@179: end adamc@177: adamc@179: | L.EWrite e => adamc@179: let adamc@179: val (e, fm) = monoExp (env, st, fm) e adamc@179: in adamc@179: ((L'.EWrite e, loc), fm) adamc@179: end adamc@110: adamc@179: | L.EClosure (n, es) => adamc@179: let adamc@179: val (es, fm) = ListUtil.foldlMap (fn (e, fm) => adamc@179: monoExp (env, st, fm) e) adamc@179: fm es adamc@179: in adamc@179: ((L'.EClosure (n, es), loc), fm) adamc@179: end adamc@25: end adamc@25: adamc@179: fun monoDecl (env, fm) (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@164: | L.DDatatype (x, n, xncs) => adamc@164: let adamc@164: val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env) to)) xncs), loc) adamc@164: in adamc@179: SOME (Env.declBinds env all, fm, d) adamc@164: end adamc@179: | L.DVal (x, n, t, e, s) => adamc@179: let adamc@179: val (e, fm) = monoExp (env, St.empty, fm) e adamc@179: in adamc@179: SOME (Env.pushENamed env x n t NONE s, adamc@179: fm, adamc@179: (L'.DVal (x, n, monoType env t, e, s), loc)) adamc@179: end adamc@128: | L.DValRec vis => adamc@128: let adamc@128: val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis adamc@179: adamc@179: val (vis, fm) = ListUtil.foldlMap adamc@179: (fn ((x, n, t, e, s), fm) => adamc@179: let adamc@179: val (e, fm) = monoExp (env, St.empty, fm) e adamc@179: in adamc@179: ((x, n, monoType env t, e, s), fm) adamc@179: end) adamc@179: fm vis adamc@128: in adamc@128: SOME (env, adamc@179: fm, adamc@179: (L'.DValRec vis, loc)) adamc@128: end adamc@144: | L.DExport (ek, 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@179: SOME (env, fm, (L'.DExport (ek, s, n, ts), loc)) adamc@115: end adamc@25: end adamc@25: adamc@25: fun monoize env ds = adamc@25: let adamc@179: val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) => adamc@179: case monoDecl (env, fm) d of adamc@179: NONE => (env, fm, ds) adamc@179: | SOME (env, fm, d) => adamc@179: (env, adamc@179: Fm.enter fm, adamc@179: d :: Fm.decls fm @ ds)) adamc@179: (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds adamc@25: in adamc@25: rev ds adamc@25: end adamc@25: adamc@25: end