annotate src/monoize.sml @ 200:5dbba661deab

Urlifying records
author Adam Chlipala <adamc@hcoop.net>
date Sat, 09 Aug 2008 20:08:29 -0400
parents ab86aa858e6c
children 766b5475477f
rev   line source
adamc@25 1 (* Copyright (c) 2008, Adam Chlipala
adamc@25 2 * All rights reserved.
adamc@25 3 *
adamc@25 4 * Redistribution and use in source and binary forms, with or without
adamc@25 5 * modification, are permitted provided that the following conditions are met:
adamc@25 6 *
adamc@25 7 * - Redistributions of source code must retain the above copyright notice,
adamc@25 8 * this list of conditions and the following disclaimer.
adamc@25 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@25 10 * this list of conditions and the following disclaimer in the documentation
adamc@25 11 * and/or other materials provided with the distribution.
adamc@25 12 * - The names of contributors may not be used to endorse or promote products
adamc@25 13 * derived from this software without specific prior written permission.
adamc@25 14 *
adamc@25 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@25 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@25 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@25 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@25 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@25 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@25 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@25 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@25 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@25 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@25 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@25 26 *)
adamc@25 27
adamc@25 28 structure Monoize :> MONOIZE = struct
adamc@25 29
adamc@25 30 structure E = ErrorMsg
adamc@25 31 structure Env = CoreEnv
adamc@25 32
adamc@25 33 structure L = Core
adamc@25 34 structure L' = Mono
adamc@25 35
adamc@196 36 structure IM = IntBinaryMap
adamc@196 37
adamc@196 38 val dummyTyp = (L'.TDatatype (0, ref (L'.Enum, [])), E.dummySpan)
adamc@25 39
adamc@25 40 fun monoName env (all as (c, loc)) =
adamc@25 41 let
adamc@25 42 fun poly () =
adamc@25 43 (E.errorAt loc "Unsupported name constructor";
adamc@25 44 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@25 45 "")
adamc@25 46 in
adamc@25 47 case c of
adamc@25 48 L.CName s => s
adamc@25 49 | _ => poly ()
adamc@25 50 end
adamc@25 51
adamc@196 52 fun monoType env =
adamc@25 53 let
adamc@196 54 fun mt env dtmap (all as (c, loc)) =
adamc@196 55 let
adamc@196 56 fun poly () =
adamc@196 57 (E.errorAt loc "Unsupported type constructor";
adamc@196 58 Print.eprefaces' [("Constructor", CorePrint.p_con env all)];
adamc@196 59 dummyTyp)
adamc@196 60 in
adamc@196 61 case c of
adamc@196 62 L.TFun (c1, c2) => (L'.TFun (mt env dtmap c1, mt env dtmap c2), loc)
adamc@196 63 | L.TCFun _ => poly ()
adamc@196 64 | L.TRecord (L.CRecord ((L.KType, _), xcs), _) =>
adamc@196 65 (L'.TRecord (map (fn (x, t) => (monoName env x, mt env dtmap t)) xcs), loc)
adamc@196 66 | L.TRecord _ => poly ()
adamc@196 67
adamc@196 68 | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
adamc@196 69 (L'.TFfi ("Basis", "string"), loc)
adamc@196 70 | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) =>
adamc@196 71 (L'.TFfi ("Basis", "string"), loc)
adamc@196 72
adamc@196 73 | L.CRel _ => poly ()
adamc@196 74 | L.CNamed n =>
adamc@196 75 (case IM.find (dtmap, n) of
adamc@196 76 SOME r => (L'.TDatatype (n, r), loc)
adamc@196 77 | NONE =>
adamc@196 78 let
adamc@196 79 val r = ref (L'.Default, [])
adamc@196 80 val (_, xs, xncs) = Env.lookupDatatype env n
adamc@196 81
adamc@196 82 val dtmap' = IM.insert (dtmap, n, r)
adamc@196 83
adamc@196 84 val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs
adamc@196 85 in
adamc@196 86 case xs of
adamc@198 87 [] =>(r := (ElabUtil.classifyDatatype xncs, xncs);
adamc@196 88 (L'.TDatatype (n, r), loc))
adamc@196 89 | _ => poly ()
adamc@196 90 end)
adamc@196 91 | L.CFfi mx => (L'.TFfi mx, loc)
adamc@196 92 | L.CApp _ => poly ()
adamc@196 93 | L.CAbs _ => poly ()
adamc@196 94
adamc@196 95 | L.CName _ => poly ()
adamc@196 96
adamc@196 97 | L.CRecord _ => poly ()
adamc@196 98 | L.CConcat _ => poly ()
adamc@196 99 | L.CFold _ => poly ()
adamc@196 100 | L.CUnit => poly ()
adamc@196 101 end
adamc@25 102 in
adamc@196 103 mt env IM.empty
adamc@25 104 end
adamc@25 105
adamc@25 106 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
adamc@25 107
adamc@179 108 structure IM = IntBinaryMap
adamc@179 109
adamc@179 110 datatype foo_kind =
adamc@179 111 Attr
adamc@179 112 | Url
adamc@179 113
adamc@179 114 fun fk2s fk =
adamc@179 115 case fk of
adamc@179 116 Attr => "attr"
adamc@179 117 | Url => "url"
adamc@179 118
adamc@179 119 structure Fm :> sig
adamc@179 120 type t
adamc@179 121
adamc@179 122 val empty : int -> t
adamc@179 123
adamc@179 124 val lookup : t -> foo_kind -> int -> (int -> t -> L'.decl * t) -> t * int
adamc@179 125 val enter : t -> t
adamc@179 126 val decls : t -> L'.decl list
adamc@179 127 end = struct
adamc@179 128
adamc@179 129 structure M = BinaryMapFn(struct
adamc@179 130 type ord_key = foo_kind
adamc@179 131 fun compare x =
adamc@179 132 case x of
adamc@179 133 (Attr, Attr) => EQUAL
adamc@179 134 | (Attr, _) => LESS
adamc@179 135 | (_, Attr) => GREATER
adamc@179 136
adamc@179 137 | (Url, Url) => EQUAL
adamc@179 138 end)
adamc@179 139
adamc@179 140 type t = {
adamc@179 141 count : int,
adamc@179 142 map : int IM.map M.map,
adamc@179 143 decls : L'.decl list
adamc@179 144 }
adamc@179 145
adamc@179 146 fun empty count = {
adamc@179 147 count = count,
adamc@179 148 map = M.empty,
adamc@179 149 decls = []
adamc@179 150 }
adamc@179 151
adamc@179 152 fun enter ({count, map, ...} : t) = {count = count, map = map, decls = []}
adamc@179 153 fun decls ({decls, ...} : t) = decls
adamc@179 154
adamc@179 155 fun lookup (t as {count, map, decls}) k n thunk =
adamc@120 156 let
adamc@179 157 val im = Option.getOpt (M.find (map, k), IM.empty)
adamc@179 158 in
adamc@179 159 case IM.find (im, n) of
adamc@179 160 NONE =>
adamc@179 161 let
adamc@179 162 val n' = count
adamc@179 163 val (d, {count, map, decls}) = thunk count {count = count + 1,
adamc@179 164 map = M.insert (map, k, IM.insert (im, n, n')),
adamc@179 165 decls = decls}
adamc@179 166 in
adamc@179 167 ({count = count,
adamc@179 168 map = map,
adamc@179 169 decls = d :: decls}, n')
adamc@179 170 end
adamc@179 171 | SOME n' => (t, n')
adamc@179 172 end
adamc@179 173
adamc@179 174 end
adamc@185 175
adamc@185 176
adamc@185 177 fun capitalize s =
adamc@185 178 if s = "" then
adamc@185 179 s
adamc@185 180 else
adamc@185 181 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@179 182
adamc@179 183 fun fooifyExp fk env =
adamc@179 184 let
adamc@179 185 fun fooify fm (e, tAll as (t, loc)) =
adamc@120 186 case #1 e of
adamc@120 187 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
adamc@120 188 let
adamc@120 189 val (_, _, _, s) = Env.lookupENamed env fnam
adamc@120 190 in
adamc@183 191 ((L'.EPrim (Prim.String ("/" ^ s)), loc), fm)
adamc@120 192 end
adamc@120 193 | L'.EClosure (fnam, args) =>
adamc@120 194 let
adamc@120 195 val (_, ft, _, s) = Env.lookupENamed env fnam
adamc@120 196 val ft = monoType env ft
adamc@111 197
adamc@179 198 fun attrify (args, ft, e, fm) =
adamc@120 199 case (args, ft) of
adamc@179 200 ([], _) => (e, fm)
adamc@120 201 | (arg :: args, (L'.TFun (t, ft), _)) =>
adamc@179 202 let
adamc@179 203 val (arg', fm) = fooify fm (arg, t)
adamc@179 204 in
adamc@179 205 attrify (args, ft,
adamc@179 206 (L'.EStrcat (e,
adamc@179 207 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@179 208 arg'), loc)), loc),
adamc@179 209 fm)
adamc@179 210 end
adamc@120 211 | _ => (E.errorAt loc "Type mismatch encoding attribute";
adamc@179 212 (e, fm))
adamc@120 213 in
adamc@183 214 attrify (args, ft, (L'.EPrim (Prim.String ("/" ^ s)), loc), fm)
adamc@120 215 end
adamc@120 216 | _ =>
adamc@120 217 case t of
adamc@185 218 L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [e]), loc), fm)
adamc@200 219
adamc@179 220 | L'.TRecord [] => ((L'.EPrim (Prim.String ""), loc), fm)
adamc@200 221 | L'.TRecord ((x, t) :: xts) =>
adamc@200 222 let
adamc@200 223 val (se, fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 224 in
adamc@200 225 foldl (fn ((x, t), (se, fm)) =>
adamc@200 226 let
adamc@200 227 val (se', fm) = fooify fm ((L'.EField (e, x), loc), t)
adamc@200 228 in
adamc@200 229 ((L'.EStrcat (se,
adamc@200 230 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@200 231 se'), loc)), loc),
adamc@200 232 fm)
adamc@200 233 end) (se, fm) xts
adamc@200 234 end
adamc@111 235
adamc@196 236 | L'.TDatatype (i, ref (dk, _)) =>
adamc@179 237 let
adamc@179 238 fun makeDecl n fm =
adamc@179 239 let
adamc@193 240 val (x, _, xncs) = Env.lookupDatatype env i
adamc@179 241
adamc@179 242 val (branches, fm) =
adamc@179 243 ListUtil.foldlMap
adamc@179 244 (fn ((x, n, to), fm) =>
adamc@179 245 case to of
adamc@179 246 NONE =>
adamc@188 247 (((L'.PCon (dk, L'.PConVar n, NONE), loc),
adamc@179 248 (L'.EPrim (Prim.String x), loc)),
adamc@179 249 fm)
adamc@179 250 | SOME t =>
adamc@179 251 let
adamc@182 252 val t = monoType env t
adamc@182 253 val (arg, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@179 254 in
adamc@188 255 (((L'.PCon (dk, L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc),
adamc@179 256 (L'.EStrcat ((L'.EPrim (Prim.String (x ^ "/")), loc),
adamc@179 257 arg), loc)),
adamc@179 258 fm)
adamc@179 259 end)
adamc@179 260 fm xncs
adamc@179 261
adamc@179 262 val dom = tAll
adamc@179 263 val ran = (L'.TFfi ("Basis", "string"), loc)
adamc@179 264 in
adamc@179 265 ((L'.DValRec [(fk2s fk ^ "ify_" ^ x,
adamc@179 266 n,
adamc@179 267 (L'.TFun (dom, ran), loc),
adamc@179 268 (L'.EAbs ("x",
adamc@179 269 dom,
adamc@179 270 ran,
adamc@179 271 (L'.ECase ((L'.ERel 0, loc),
adamc@179 272 branches,
adamc@182 273 {disc = dom,
adamc@182 274 result = ran}), loc)), loc),
adamc@179 275 "")], loc),
adamc@179 276 fm)
adamc@179 277 end
adamc@179 278
adamc@179 279 val (fm, n) = Fm.lookup fm fk i makeDecl
adamc@179 280 in
adamc@179 281 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
adamc@179 282 end
adamc@164 283
adamc@120 284 | _ => (E.errorAt loc "Don't know how to encode attribute type";
adamc@120 285 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
adamc@179 286 (dummyExp, fm))
adamc@120 287 in
adamc@120 288 fooify
adamc@120 289 end
adamc@120 290
adamc@179 291 val attrifyExp = fooifyExp Attr
adamc@179 292 val urlifyExp = fooifyExp Url
adamc@105 293
adamc@143 294 datatype 'a failable_search =
adamc@143 295 Found of 'a
adamc@143 296 | NotFound
adamc@143 297 | Error
adamc@143 298
adamc@153 299 structure St :> sig
adamc@153 300 type t
adamc@153 301
adamc@153 302 val empty : t
adamc@153 303
adamc@153 304 val radioGroup : t -> string option
adamc@153 305 val setRadioGroup : t * string -> t
adamc@153 306 end = struct
adamc@153 307
adamc@153 308 type t = {
adamc@153 309 radioGroup : string option
adamc@153 310 }
adamc@153 311
adamc@153 312 val empty = {radioGroup = NONE}
adamc@153 313
adamc@153 314 fun radioGroup (t : t) = #radioGroup t
adamc@153 315
adamc@153 316 fun setRadioGroup (t : t, x) = {radioGroup = SOME x}
adamc@153 317
adamc@153 318 end
adamc@153 319
adamc@186 320 fun monoPatCon env pc =
adamc@178 321 case pc of
adamc@178 322 L.PConVar n => L'.PConVar n
adamc@188 323 | L.PConFfi {mod = m, datatyp, con, arg, ...} => L'.PConFfi {mod = m, datatyp = datatyp, con = con,
adamc@188 324 arg = Option.map (monoType env) arg}
adamc@178 325
adamc@193 326 val dummyPat = (L'.PPrim (Prim.Int 0), ErrorMsg.dummySpan)
adamc@193 327
adamc@193 328 fun monoPat env (all as (p, loc)) =
adamc@193 329 let
adamc@193 330 fun poly () =
adamc@193 331 (E.errorAt loc "Unsupported pattern";
adamc@193 332 Print.eprefaces' [("Pattern", CorePrint.p_pat env all)];
adamc@193 333 dummyPat)
adamc@193 334 in
adamc@193 335 case p of
adamc@193 336 L.PWild => (L'.PWild, loc)
adamc@193 337 | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc)
adamc@193 338 | L.PPrim p => (L'.PPrim p, loc)
adamc@193 339 | L.PCon (dk, pc, [], po) => (L'.PCon (dk, monoPatCon env pc, Option.map (monoPat env) po), loc)
adamc@193 340 | L.PCon _ => poly ()
adamc@193 341 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc)
adamc@193 342 end
adamc@178 343
adamc@179 344 fun monoExp (env, st, fm) (all as (e, loc)) =
adamc@25 345 let
adamc@25 346 fun poly () =
adamc@25 347 (E.errorAt loc "Unsupported expression";
adamc@25 348 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
adamc@179 349 (dummyExp, fm))
adamc@25 350 in
adamc@25 351 case e of
adamc@179 352 L.EPrim p => ((L'.EPrim p, loc), fm)
adamc@179 353 | L.ERel n => ((L'.ERel n, loc), fm)
adamc@179 354 | L.ENamed n => ((L'.ENamed n, loc), fm)
adamc@193 355 | L.ECon (dk, pc, [], eo) =>
adamc@193 356 let
adamc@179 357 val (eo, fm) =
adamc@179 358 case eo of
adamc@179 359 NONE => (NONE, fm)
adamc@179 360 | SOME e =>
adamc@179 361 let
adamc@179 362 val (e, fm) = monoExp (env, st, fm) e
adamc@179 363 in
adamc@179 364 (SOME e, fm)
adamc@179 365 end
adamc@179 366 in
adamc@188 367 ((L'.ECon (dk, monoPatCon env pc, eo), loc), fm)
adamc@193 368 end
adamc@193 369 | L.ECon _ => poly ()
adamc@179 370 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
adamc@179 371 | L.EFfiApp (m, x, es) =>
adamc@179 372 let
adamc@179 373 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@179 374 in
adamc@179 375 ((L'.EFfiApp (m, x, es), loc), fm)
adamc@179 376 end
adamc@94 377
adamc@139 378 | L.EApp (
adamc@139 379 (L.ECApp (
adamc@141 380 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 381 _), _),
adamc@179 382 se) =>
adamc@179 383 let
adamc@179 384 val (se, fm) = monoExp (env, st, fm) se
adamc@179 385 in
adamc@179 386 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
adamc@179 387 end
adamc@179 388
adamc@95 389 | L.EApp (
adamc@95 390 (L.EApp (
adamc@95 391 (L.ECApp (
adamc@95 392 (L.ECApp (
adamc@95 393 (L.ECApp (
adamc@139 394 (L.ECApp (
adamc@140 395 (L.EFfi ("Basis", "join"),
adamc@139 396 _), _), _),
adamc@139 397 _), _),
adamc@95 398 _), _),
adamc@95 399 _), _),
adamc@95 400 xml1), _),
adamc@179 401 xml2) =>
adamc@179 402 let
adamc@179 403 val (xml1, fm) = monoExp (env, st, fm) xml1
adamc@179 404 val (xml2, fm) = monoExp (env, st, fm) xml2
adamc@179 405 in
adamc@179 406 ((L'.EStrcat (xml1, xml2), loc), fm)
adamc@179 407 end
adamc@95 408
adamc@95 409 | L.EApp (
adamc@95 410 (L.EApp (
adamc@104 411 (L.EApp (
adamc@95 412 (L.ECApp (
adamc@104 413 (L.ECApp (
adamc@104 414 (L.ECApp (
adamc@104 415 (L.ECApp (
adamc@139 416 (L.ECApp (
adamc@139 417 (L.ECApp (
adamc@139 418 (L.ECApp (
adamc@139 419 (L.ECApp (
adamc@139 420 (L.EFfi ("Basis", "tag"),
adamc@139 421 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@104 422 attrs), _),
adamc@95 423 tag), _),
adamc@95 424 xml) =>
adamc@95 425 let
adamc@140 426 fun getTag' (e, _) =
adamc@140 427 case e of
adamc@143 428 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 429 | L.ECApp (e, t) => let
adamc@143 430 val (tag, ts) = getTag' e
adamc@143 431 in
adamc@143 432 (tag, ts @ [t])
adamc@143 433 end
adamc@140 434 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 435 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 436 ("", []))
adamc@140 437
adamc@95 438 fun getTag (e, _) =
adamc@95 439 case e of
adamc@143 440 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 441 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 442 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 443 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 444 ("", []))
adamc@95 445
adamc@143 446 val (tag, targs) = getTag tag
adamc@95 447
adamc@179 448 val (attrs, fm) = monoExp (env, st, fm) attrs
adamc@104 449
adamc@143 450 fun tagStart tag =
adamc@104 451 case #1 attrs of
adamc@104 452 L'.ERecord xes =>
adamc@104 453 let
adamc@104 454 fun lowercaseFirst "" = ""
adamc@143 455 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
adamc@143 456 ^ String.extract (s, 1, NONE)
adamc@104 457
adamc@104 458 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@104 459 in
adamc@179 460 foldl (fn ((x, e, t), (s, fm)) =>
adamc@104 461 let
adamc@104 462 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@120 463
adamc@120 464 val fooify =
adamc@120 465 case x of
adamc@185 466 "Href" => urlifyExp
adamc@185 467 | "Link" => urlifyExp
adamc@143 468 | "Action" => urlifyExp
adamc@120 469 | _ => attrifyExp
adamc@179 470
adamc@179 471 val (e, fm) = fooify env fm (e, t)
adamc@104 472 in
adamc@179 473 ((L'.EStrcat (s,
adamc@179 474 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@179 475 (L'.EStrcat (e,
adamc@179 476 (L'.EPrim (Prim.String "\""),
adamc@179 477 loc)),
adamc@179 478 loc)),
adamc@179 479 loc)), loc),
adamc@179 480 fm)
adamc@104 481 end)
adamc@179 482 (s, fm) xes
adamc@104 483 end
adamc@143 484 | _ => raise Fail "Non-record attributes!"
adamc@104 485
adamc@143 486 fun input typ =
adamc@143 487 case targs of
adamc@155 488 [_, (L.CName name, _)] =>
adamc@179 489 let
adamc@179 490 val (ts, fm) = tagStart "input"
adamc@179 491 in
adamc@179 492 ((L'.EStrcat (ts,
adamc@179 493 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
adamc@179 494 loc)), loc), fm)
adamc@179 495 end
adamc@143 496 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 497 raise Fail "No name passed to input tag")
adamc@104 498
adamc@152 499 fun normal (tag, extra) =
adamc@143 500 let
adamc@179 501 val (tagStart, fm) = tagStart tag
adamc@152 502 val tagStart = case extra of
adamc@152 503 NONE => tagStart
adamc@152 504 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 505
adamc@143 506 fun normal () =
adamc@179 507 let
adamc@179 508 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 509 in
adamc@179 510 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@179 511 (L'.EStrcat (xml,
adamc@179 512 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@179 513 loc)), loc)),
adamc@179 514 loc),
adamc@179 515 fm)
adamc@179 516 end
adamc@143 517 in
adamc@143 518 case xml of
adamc@143 519 (L.EApp ((L.ECApp (
adamc@143 520 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 521 _), _),
adamc@143 522 _), _),
adamc@143 523 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 524 if CharVector.all Char.isSpace s then
adamc@179 525 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
adamc@143 526 else
adamc@143 527 normal ()
adamc@143 528 | _ => normal ()
adamc@143 529 end
adamc@152 530 in
adamc@152 531 case tag of
adamc@179 532 "submit" => ((L'.EPrim (Prim.String "<input type=\"submit\"/>"), loc), fm)
adamc@152 533
adamc@152 534 | "textbox" =>
adamc@152 535 (case targs of
adamc@152 536 [_, (L.CName name, _)] =>
adamc@179 537 let
adamc@179 538 val (ts, fm) = tagStart "input"
adamc@179 539 in
adamc@179 540 ((L'.EStrcat (ts,
adamc@179 541 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
adamc@179 542 loc)), loc), fm)
adamc@179 543 end
adamc@152 544 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 545 raise Fail "No name passed to textarea tag"))
adamc@155 546 | "password" => input "password"
adamc@152 547 | "ltextarea" =>
adamc@152 548 (case targs of
adamc@152 549 [_, (L.CName name, _)] =>
adamc@179 550 let
adamc@179 551 val (ts, fm) = tagStart "textarea"
adamc@179 552 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 553 in
adamc@179 554 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 555 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 556 (L'.EStrcat (xml,
adamc@179 557 (L'.EPrim (Prim.String "</textarea>"),
adamc@179 558 loc)), loc)),
adamc@179 559 loc), fm)
adamc@179 560 end
adamc@152 561 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 562 raise Fail "No name passed to ltextarea tag"))
adamc@153 563
adamc@190 564 | "checkbox" => input "checkbox"
adamc@190 565
adamc@153 566 | "radio" =>
adamc@153 567 (case targs of
adamc@153 568 [_, (L.CName name, _)] =>
adamc@179 569 monoExp (env, St.setRadioGroup (st, name), fm) xml
adamc@153 570 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 571 raise Fail "No name passed to radio tag"))
adamc@153 572 | "radioOption" =>
adamc@153 573 (case St.radioGroup st of
adamc@153 574 NONE => raise Fail "No name for radioGroup"
adamc@153 575 | SOME name =>
adamc@153 576 normal ("input",
adamc@153 577 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc)))
adamc@152 578
adamc@154 579 | "lselect" =>
adamc@154 580 (case targs of
adamc@154 581 [_, (L.CName name, _)] =>
adamc@179 582 let
adamc@179 583 val (ts, fm) = tagStart "select"
adamc@179 584 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 585 in
adamc@179 586 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 587 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 588 (L'.EStrcat (xml,
adamc@179 589 (L'.EPrim (Prim.String "</select>"),
adamc@179 590 loc)), loc)),
adamc@179 591 loc),
adamc@179 592 fm)
adamc@179 593 end
adamc@154 594 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 595 raise Fail "No name passed to lselect tag"))
adamc@154 596
adamc@154 597 | "loption" => normal ("option", NONE)
adamc@154 598
adamc@152 599 | _ => normal (tag, NONE)
adamc@95 600 end
adamc@94 601
adamc@141 602 | L.EApp ((L.ECApp (
adamc@141 603 (L.ECApp ((L.EFfi ("Basis", "lform"), _), _), _),
adamc@141 604 _), _),
adamc@141 605 xml) =>
adamc@143 606 let
adamc@143 607 fun findSubmit (e, _) =
adamc@143 608 case e of
adamc@143 609 L.EApp (
adamc@143 610 (L.EApp (
adamc@143 611 (L.ECApp (
adamc@143 612 (L.ECApp (
adamc@143 613 (L.ECApp (
adamc@143 614 (L.ECApp (
adamc@143 615 (L.EFfi ("Basis", "join"),
adamc@143 616 _), _), _),
adamc@143 617 _), _),
adamc@143 618 _), _),
adamc@143 619 _), _),
adamc@143 620 xml1), _),
adamc@143 621 xml2) => (case findSubmit xml1 of
adamc@143 622 Error => Error
adamc@143 623 | NotFound => findSubmit xml2
adamc@143 624 | Found e =>
adamc@143 625 case findSubmit xml2 of
adamc@143 626 NotFound => Found e
adamc@143 627 | _ => Error)
adamc@143 628 | L.EApp (
adamc@143 629 (L.EApp (
adamc@143 630 (L.EApp (
adamc@143 631 (L.ECApp (
adamc@143 632 (L.ECApp (
adamc@143 633 (L.ECApp (
adamc@143 634 (L.ECApp (
adamc@143 635 (L.ECApp (
adamc@143 636 (L.ECApp (
adamc@143 637 (L.ECApp (
adamc@143 638 (L.ECApp (
adamc@143 639 (L.EFfi ("Basis", "tag"),
adamc@143 640 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@143 641 attrs), _),
adamc@143 642 _), _),
adamc@143 643 xml) =>
adamc@143 644 (case #1 attrs of
adamc@143 645 L.ERecord xes =>
adamc@143 646 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 647 | _ => NONE) xes of
adamc@143 648 NONE => findSubmit xml
adamc@143 649 | SOME et =>
adamc@143 650 case findSubmit xml of
adamc@143 651 NotFound => Found et
adamc@143 652 | _ => Error)
adamc@143 653 | _ => findSubmit xml)
adamc@143 654 | _ => NotFound
adamc@143 655
adamc@143 656 val (action, actionT) = case findSubmit xml of
adamc@143 657 NotFound => raise Fail "No submit found"
adamc@143 658 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@143 659 | Found et => et
adamc@143 660
adamc@143 661 val actionT = monoType env actionT
adamc@179 662 val (action, fm) = monoExp (env, st, fm) action
adamc@179 663 val (action, fm) = urlifyExp env fm (action, actionT)
adamc@179 664 val (xml, fm) = monoExp (env, st, fm) xml
adamc@143 665 in
adamc@179 666 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
adamc@179 667 (L'.EStrcat (action,
adamc@179 668 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
adamc@179 669 (L'.EStrcat (xml,
adamc@179 670 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
adamc@179 671 fm)
adamc@143 672 end
adamc@141 673
adamc@148 674 | L.EApp ((L.ECApp (
adamc@148 675 (L.ECApp (
adamc@148 676 (L.ECApp (
adamc@148 677 (L.ECApp (
adamc@148 678 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 679 _), _),
adamc@148 680 _), _),
adamc@148 681 _), _),
adamc@179 682 xml) => monoExp (env, st, fm) xml
adamc@148 683
adamc@179 684 | L.EApp (e1, e2) =>
adamc@179 685 let
adamc@179 686 val (e1, fm) = monoExp (env, st, fm) e1
adamc@179 687 val (e2, fm) = monoExp (env, st, fm) e2
adamc@179 688 in
adamc@179 689 ((L'.EApp (e1, e2), loc), fm)
adamc@179 690 end
adamc@26 691 | L.EAbs (x, dom, ran, e) =>
adamc@179 692 let
adamc@179 693 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
adamc@179 694 in
adamc@179 695 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
adamc@179 696 end
adamc@25 697 | L.ECApp _ => poly ()
adamc@25 698 | L.ECAbs _ => poly ()
adamc@25 699
adamc@179 700 | L.ERecord xes =>
adamc@179 701 let
adamc@179 702 val (xes, fm) = ListUtil.foldlMap
adamc@179 703 (fn ((x, e, t), fm) =>
adamc@179 704 let
adamc@179 705 val (e, fm) = monoExp (env, st, fm) e
adamc@179 706 in
adamc@179 707 ((monoName env x,
adamc@179 708 e,
adamc@179 709 monoType env t), fm)
adamc@179 710 end) fm xes
adamc@179 711 in
adamc@179 712 ((L'.ERecord xes, loc), fm)
adamc@179 713 end
adamc@179 714 | L.EField (e, x, _) =>
adamc@179 715 let
adamc@179 716 val (e, fm) = monoExp (env, st, fm) e
adamc@179 717 in
adamc@179 718 ((L'.EField (e, monoName env x), loc), fm)
adamc@179 719 end
adamc@149 720 | L.ECut _ => poly ()
adamc@73 721 | L.EFold _ => poly ()
adamc@177 722
adamc@182 723 | L.ECase (e, pes, {disc, result}) =>
adamc@179 724 let
adamc@179 725 val (e, fm) = monoExp (env, st, fm) e
adamc@179 726 val (pes, fm) = ListUtil.foldlMap
adamc@179 727 (fn ((p, e), fm) =>
adamc@179 728 let
adamc@179 729 val (e, fm) = monoExp (env, st, fm) e
adamc@179 730 in
adamc@182 731 ((monoPat env p, e), fm)
adamc@179 732 end) fm pes
adamc@179 733 in
adamc@182 734 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
adamc@179 735 end
adamc@177 736
adamc@179 737 | L.EWrite e =>
adamc@179 738 let
adamc@179 739 val (e, fm) = monoExp (env, st, fm) e
adamc@179 740 in
adamc@179 741 ((L'.EWrite e, loc), fm)
adamc@179 742 end
adamc@110 743
adamc@179 744 | L.EClosure (n, es) =>
adamc@179 745 let
adamc@179 746 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
adamc@179 747 monoExp (env, st, fm) e)
adamc@179 748 fm es
adamc@179 749 in
adamc@179 750 ((L'.EClosure (n, es), loc), fm)
adamc@179 751 end
adamc@25 752 end
adamc@25 753
adamc@179 754 fun monoDecl (env, fm) (all as (d, loc)) =
adamc@25 755 let
adamc@25 756 fun poly () =
adamc@25 757 (E.errorAt loc "Unsupported declaration";
adamc@25 758 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 759 NONE)
adamc@25 760 in
adamc@25 761 case d of
adamc@25 762 L.DCon _ => NONE
adamc@193 763 | L.DDatatype (x, n, [], xncs) =>
adamc@193 764 let
adamc@196 765 val env' = Env.declBinds env all
adamc@196 766 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc)
adamc@164 767 in
adamc@196 768 SOME (env', fm, d)
adamc@193 769 end
adamc@193 770 | L.DDatatype _ => poly ()
adamc@179 771 | L.DVal (x, n, t, e, s) =>
adamc@179 772 let
adamc@179 773 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 774 in
adamc@179 775 SOME (Env.pushENamed env x n t NONE s,
adamc@179 776 fm,
adamc@179 777 (L'.DVal (x, n, monoType env t, e, s), loc))
adamc@179 778 end
adamc@128 779 | L.DValRec vis =>
adamc@128 780 let
adamc@128 781 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@179 782
adamc@179 783 val (vis, fm) = ListUtil.foldlMap
adamc@179 784 (fn ((x, n, t, e, s), fm) =>
adamc@179 785 let
adamc@179 786 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 787 in
adamc@179 788 ((x, n, monoType env t, e, s), fm)
adamc@179 789 end)
adamc@179 790 fm vis
adamc@128 791 in
adamc@128 792 SOME (env,
adamc@179 793 fm,
adamc@179 794 (L'.DValRec vis, loc))
adamc@128 795 end
adamc@144 796 | L.DExport (ek, n) =>
adamc@115 797 let
adamc@120 798 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 799
adamc@120 800 fun unwind (t, _) =
adamc@120 801 case t of
adamc@120 802 L.TFun (dom, ran) => dom :: unwind ran
adamc@120 803 | _ => []
adamc@120 804
adamc@120 805 val ts = map (monoType env) (unwind t)
adamc@115 806 in
adamc@179 807 SOME (env, fm, (L'.DExport (ek, s, n, ts), loc))
adamc@115 808 end
adamc@25 809 end
adamc@25 810
adamc@25 811 fun monoize env ds =
adamc@25 812 let
adamc@179 813 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
adamc@179 814 case monoDecl (env, fm) d of
adamc@179 815 NONE => (env, fm, ds)
adamc@179 816 | SOME (env, fm, d) =>
adamc@179 817 (env,
adamc@179 818 Fm.enter fm,
adamc@179 819 d :: Fm.decls fm @ ds))
adamc@179 820 (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds
adamc@25 821 in
adamc@25 822 rev ds
adamc@25 823 end
adamc@25 824
adamc@25 825 end