annotate src/monoize.sml @ 179:3bbed533fbd2

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