annotate src/monoize.sml @ 190:3eb53c957d10

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