annotate src/monoize.sml @ 182:d11754ffe252

Compiled pattern matching to C
author Adam Chlipala <adamc@hcoop.net>
date Sun, 03 Aug 2008 12:43:20 -0400
parents 3bbed533fbd2
children c0ea24dcb86f
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@182 215 val t = monoType env t
adamc@182 216 val (arg, fm) = fooify fm ((L'.ERel 0, loc), t)
adamc@179 217 in
adamc@182 218 (((L'.PCon (L'.PConVar n, SOME (L'.PVar ("a", t), 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@182 236 {disc = dom,
adamc@182 237 result = ran}), loc)), loc),
adamc@179 238 "")], loc),
adamc@179 239 fm)
adamc@179 240 end
adamc@179 241
adamc@179 242 val (fm, n) = Fm.lookup fm fk i makeDecl
adamc@179 243 in
adamc@179 244 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
adamc@179 245 end
adamc@164 246
adamc@120 247 | _ => (E.errorAt loc "Don't know how to encode attribute type";
adamc@120 248 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
adamc@179 249 (dummyExp, fm))
adamc@120 250 in
adamc@120 251 fooify
adamc@120 252 end
adamc@120 253
adamc@179 254 val attrifyExp = fooifyExp Attr
adamc@179 255 val urlifyExp = fooifyExp Url
adamc@105 256
adamc@143 257 datatype 'a failable_search =
adamc@143 258 Found of 'a
adamc@143 259 | NotFound
adamc@143 260 | Error
adamc@143 261
adamc@153 262 structure St :> sig
adamc@153 263 type t
adamc@153 264
adamc@153 265 val empty : t
adamc@153 266
adamc@153 267 val radioGroup : t -> string option
adamc@153 268 val setRadioGroup : t * string -> t
adamc@153 269 end = struct
adamc@153 270
adamc@153 271 type t = {
adamc@153 272 radioGroup : string option
adamc@153 273 }
adamc@153 274
adamc@153 275 val empty = {radioGroup = NONE}
adamc@153 276
adamc@153 277 fun radioGroup (t : t) = #radioGroup t
adamc@153 278
adamc@153 279 fun setRadioGroup (t : t, x) = {radioGroup = SOME x}
adamc@153 280
adamc@153 281 end
adamc@153 282
adamc@178 283 fun monoPatCon pc =
adamc@178 284 case pc of
adamc@178 285 L.PConVar n => L'.PConVar n
adamc@178 286 | L.PConFfi mx => L'.PConFfi mx
adamc@178 287
adamc@182 288 fun monoPat env (p, loc) =
adamc@178 289 case p of
adamc@178 290 L.PWild => (L'.PWild, loc)
adamc@182 291 | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc)
adamc@178 292 | L.PPrim p => (L'.PPrim p, loc)
adamc@182 293 | L.PCon (pc, po) => (L'.PCon (monoPatCon pc, Option.map (monoPat env) po), loc)
adamc@182 294 | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc)
adamc@178 295
adamc@179 296 fun monoExp (env, st, fm) (all as (e, loc)) =
adamc@25 297 let
adamc@25 298 fun poly () =
adamc@25 299 (E.errorAt loc "Unsupported expression";
adamc@25 300 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
adamc@179 301 (dummyExp, fm))
adamc@25 302 in
adamc@25 303 case e of
adamc@179 304 L.EPrim p => ((L'.EPrim p, loc), fm)
adamc@179 305 | L.ERel n => ((L'.ERel n, loc), fm)
adamc@179 306 | L.ENamed n => ((L'.ENamed n, loc), fm)
adamc@179 307 | L.ECon (n, eo) =>
adamc@179 308 let
adamc@179 309 val (eo, fm) =
adamc@179 310 case eo of
adamc@179 311 NONE => (NONE, fm)
adamc@179 312 | SOME e =>
adamc@179 313 let
adamc@179 314 val (e, fm) = monoExp (env, st, fm) e
adamc@179 315 in
adamc@179 316 (SOME e, fm)
adamc@179 317 end
adamc@179 318 in
adamc@179 319 ((L'.ECon (n, eo), loc), fm)
adamc@179 320 end
adamc@179 321 | L.EFfi mx => ((L'.EFfi mx, loc), fm)
adamc@179 322 | L.EFfiApp (m, x, es) =>
adamc@179 323 let
adamc@179 324 val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es
adamc@179 325 in
adamc@179 326 ((L'.EFfiApp (m, x, es), loc), fm)
adamc@179 327 end
adamc@94 328
adamc@139 329 | L.EApp (
adamc@139 330 (L.ECApp (
adamc@141 331 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 332 _), _),
adamc@179 333 se) =>
adamc@179 334 let
adamc@179 335 val (se, fm) = monoExp (env, st, fm) se
adamc@179 336 in
adamc@179 337 ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm)
adamc@179 338 end
adamc@179 339
adamc@95 340 | L.EApp (
adamc@95 341 (L.EApp (
adamc@95 342 (L.ECApp (
adamc@95 343 (L.ECApp (
adamc@95 344 (L.ECApp (
adamc@139 345 (L.ECApp (
adamc@140 346 (L.EFfi ("Basis", "join"),
adamc@139 347 _), _), _),
adamc@139 348 _), _),
adamc@95 349 _), _),
adamc@95 350 _), _),
adamc@95 351 xml1), _),
adamc@179 352 xml2) =>
adamc@179 353 let
adamc@179 354 val (xml1, fm) = monoExp (env, st, fm) xml1
adamc@179 355 val (xml2, fm) = monoExp (env, st, fm) xml2
adamc@179 356 in
adamc@179 357 ((L'.EStrcat (xml1, xml2), loc), fm)
adamc@179 358 end
adamc@95 359
adamc@95 360 | L.EApp (
adamc@95 361 (L.EApp (
adamc@104 362 (L.EApp (
adamc@95 363 (L.ECApp (
adamc@104 364 (L.ECApp (
adamc@104 365 (L.ECApp (
adamc@104 366 (L.ECApp (
adamc@139 367 (L.ECApp (
adamc@139 368 (L.ECApp (
adamc@139 369 (L.ECApp (
adamc@139 370 (L.ECApp (
adamc@139 371 (L.EFfi ("Basis", "tag"),
adamc@139 372 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@104 373 attrs), _),
adamc@95 374 tag), _),
adamc@95 375 xml) =>
adamc@95 376 let
adamc@140 377 fun getTag' (e, _) =
adamc@140 378 case e of
adamc@143 379 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 380 | L.ECApp (e, t) => let
adamc@143 381 val (tag, ts) = getTag' e
adamc@143 382 in
adamc@143 383 (tag, ts @ [t])
adamc@143 384 end
adamc@140 385 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 386 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 387 ("", []))
adamc@140 388
adamc@95 389 fun getTag (e, _) =
adamc@95 390 case e of
adamc@143 391 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 392 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 393 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 394 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 395 ("", []))
adamc@95 396
adamc@143 397 val (tag, targs) = getTag tag
adamc@95 398
adamc@179 399 val (attrs, fm) = monoExp (env, st, fm) attrs
adamc@104 400
adamc@143 401 fun tagStart tag =
adamc@104 402 case #1 attrs of
adamc@104 403 L'.ERecord xes =>
adamc@104 404 let
adamc@104 405 fun lowercaseFirst "" = ""
adamc@143 406 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
adamc@143 407 ^ String.extract (s, 1, NONE)
adamc@104 408
adamc@104 409 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@104 410 in
adamc@179 411 foldl (fn ((x, e, t), (s, fm)) =>
adamc@104 412 let
adamc@104 413 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@120 414
adamc@120 415 val fooify =
adamc@120 416 case x of
adamc@120 417 "Link" => urlifyExp
adamc@143 418 | "Action" => urlifyExp
adamc@120 419 | _ => attrifyExp
adamc@179 420
adamc@179 421 val (e, fm) = fooify env fm (e, t)
adamc@104 422 in
adamc@179 423 ((L'.EStrcat (s,
adamc@179 424 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@179 425 (L'.EStrcat (e,
adamc@179 426 (L'.EPrim (Prim.String "\""),
adamc@179 427 loc)),
adamc@179 428 loc)),
adamc@179 429 loc)), loc),
adamc@179 430 fm)
adamc@104 431 end)
adamc@179 432 (s, fm) xes
adamc@104 433 end
adamc@143 434 | _ => raise Fail "Non-record attributes!"
adamc@104 435
adamc@143 436 fun input typ =
adamc@143 437 case targs of
adamc@155 438 [_, (L.CName name, _)] =>
adamc@179 439 let
adamc@179 440 val (ts, fm) = tagStart "input"
adamc@179 441 in
adamc@179 442 ((L'.EStrcat (ts,
adamc@179 443 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
adamc@179 444 loc)), loc), fm)
adamc@179 445 end
adamc@143 446 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 447 raise Fail "No name passed to input tag")
adamc@104 448
adamc@152 449 fun normal (tag, extra) =
adamc@143 450 let
adamc@179 451 val (tagStart, fm) = tagStart tag
adamc@152 452 val tagStart = case extra of
adamc@152 453 NONE => tagStart
adamc@152 454 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 455
adamc@143 456 fun normal () =
adamc@179 457 let
adamc@179 458 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 459 in
adamc@179 460 ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@179 461 (L'.EStrcat (xml,
adamc@179 462 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@179 463 loc)), loc)),
adamc@179 464 loc),
adamc@179 465 fm)
adamc@179 466 end
adamc@143 467 in
adamc@143 468 case xml of
adamc@143 469 (L.EApp ((L.ECApp (
adamc@143 470 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 471 _), _),
adamc@143 472 _), _),
adamc@143 473 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 474 if CharVector.all Char.isSpace s then
adamc@179 475 ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm)
adamc@143 476 else
adamc@143 477 normal ()
adamc@143 478 | _ => normal ()
adamc@143 479 end
adamc@152 480 in
adamc@152 481 case tag of
adamc@179 482 "submit" => ((L'.EPrim (Prim.String "<input type=\"submit\"/>"), loc), fm)
adamc@152 483
adamc@152 484 | "textbox" =>
adamc@152 485 (case targs of
adamc@152 486 [_, (L.CName name, _)] =>
adamc@179 487 let
adamc@179 488 val (ts, fm) = tagStart "input"
adamc@179 489 in
adamc@179 490 ((L'.EStrcat (ts,
adamc@179 491 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
adamc@179 492 loc)), loc), fm)
adamc@179 493 end
adamc@152 494 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 495 raise Fail "No name passed to textarea tag"))
adamc@155 496 | "password" => input "password"
adamc@152 497 | "ltextarea" =>
adamc@152 498 (case targs of
adamc@152 499 [_, (L.CName name, _)] =>
adamc@179 500 let
adamc@179 501 val (ts, fm) = tagStart "textarea"
adamc@179 502 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 503 in
adamc@179 504 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 505 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 506 (L'.EStrcat (xml,
adamc@179 507 (L'.EPrim (Prim.String "</textarea>"),
adamc@179 508 loc)), loc)),
adamc@179 509 loc), fm)
adamc@179 510 end
adamc@152 511 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 512 raise Fail "No name passed to ltextarea tag"))
adamc@153 513
adamc@153 514 | "radio" =>
adamc@153 515 (case targs of
adamc@153 516 [_, (L.CName name, _)] =>
adamc@179 517 monoExp (env, St.setRadioGroup (st, name), fm) xml
adamc@153 518 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@153 519 raise Fail "No name passed to radio tag"))
adamc@153 520 | "radioOption" =>
adamc@153 521 (case St.radioGroup st of
adamc@153 522 NONE => raise Fail "No name for radioGroup"
adamc@153 523 | SOME name =>
adamc@153 524 normal ("input",
adamc@153 525 SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc)))
adamc@152 526
adamc@154 527 | "lselect" =>
adamc@154 528 (case targs of
adamc@154 529 [_, (L.CName name, _)] =>
adamc@179 530 let
adamc@179 531 val (ts, fm) = tagStart "select"
adamc@179 532 val (xml, fm) = monoExp (env, st, fm) xml
adamc@179 533 in
adamc@179 534 ((L'.EStrcat ((L'.EStrcat (ts,
adamc@179 535 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@179 536 (L'.EStrcat (xml,
adamc@179 537 (L'.EPrim (Prim.String "</select>"),
adamc@179 538 loc)), loc)),
adamc@179 539 loc),
adamc@179 540 fm)
adamc@179 541 end
adamc@154 542 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@154 543 raise Fail "No name passed to lselect tag"))
adamc@154 544
adamc@154 545 | "loption" => normal ("option", NONE)
adamc@154 546
adamc@152 547 | _ => normal (tag, NONE)
adamc@95 548 end
adamc@94 549
adamc@141 550 | L.EApp ((L.ECApp (
adamc@141 551 (L.ECApp ((L.EFfi ("Basis", "lform"), _), _), _),
adamc@141 552 _), _),
adamc@141 553 xml) =>
adamc@143 554 let
adamc@143 555 fun findSubmit (e, _) =
adamc@143 556 case e of
adamc@143 557 L.EApp (
adamc@143 558 (L.EApp (
adamc@143 559 (L.ECApp (
adamc@143 560 (L.ECApp (
adamc@143 561 (L.ECApp (
adamc@143 562 (L.ECApp (
adamc@143 563 (L.EFfi ("Basis", "join"),
adamc@143 564 _), _), _),
adamc@143 565 _), _),
adamc@143 566 _), _),
adamc@143 567 _), _),
adamc@143 568 xml1), _),
adamc@143 569 xml2) => (case findSubmit xml1 of
adamc@143 570 Error => Error
adamc@143 571 | NotFound => findSubmit xml2
adamc@143 572 | Found e =>
adamc@143 573 case findSubmit xml2 of
adamc@143 574 NotFound => Found e
adamc@143 575 | _ => Error)
adamc@143 576 | L.EApp (
adamc@143 577 (L.EApp (
adamc@143 578 (L.EApp (
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.ECApp (
adamc@143 587 (L.EFfi ("Basis", "tag"),
adamc@143 588 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@143 589 attrs), _),
adamc@143 590 _), _),
adamc@143 591 xml) =>
adamc@143 592 (case #1 attrs of
adamc@143 593 L.ERecord xes =>
adamc@143 594 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 595 | _ => NONE) xes of
adamc@143 596 NONE => findSubmit xml
adamc@143 597 | SOME et =>
adamc@143 598 case findSubmit xml of
adamc@143 599 NotFound => Found et
adamc@143 600 | _ => Error)
adamc@143 601 | _ => findSubmit xml)
adamc@143 602 | _ => NotFound
adamc@143 603
adamc@143 604 val (action, actionT) = case findSubmit xml of
adamc@143 605 NotFound => raise Fail "No submit found"
adamc@143 606 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@143 607 | Found et => et
adamc@143 608
adamc@143 609 val actionT = monoType env actionT
adamc@179 610 val (action, fm) = monoExp (env, st, fm) action
adamc@179 611 val (action, fm) = urlifyExp env fm (action, actionT)
adamc@179 612 val (xml, fm) = monoExp (env, st, fm) xml
adamc@143 613 in
adamc@179 614 ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
adamc@179 615 (L'.EStrcat (action,
adamc@179 616 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
adamc@179 617 (L'.EStrcat (xml,
adamc@179 618 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc),
adamc@179 619 fm)
adamc@143 620 end
adamc@141 621
adamc@148 622 | L.EApp ((L.ECApp (
adamc@148 623 (L.ECApp (
adamc@148 624 (L.ECApp (
adamc@148 625 (L.ECApp (
adamc@148 626 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 627 _), _),
adamc@148 628 _), _),
adamc@148 629 _), _),
adamc@179 630 xml) => monoExp (env, st, fm) xml
adamc@148 631
adamc@179 632 | L.EApp (e1, e2) =>
adamc@179 633 let
adamc@179 634 val (e1, fm) = monoExp (env, st, fm) e1
adamc@179 635 val (e2, fm) = monoExp (env, st, fm) e2
adamc@179 636 in
adamc@179 637 ((L'.EApp (e1, e2), loc), fm)
adamc@179 638 end
adamc@26 639 | L.EAbs (x, dom, ran, e) =>
adamc@179 640 let
adamc@179 641 val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e
adamc@179 642 in
adamc@179 643 ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm)
adamc@179 644 end
adamc@25 645 | L.ECApp _ => poly ()
adamc@25 646 | L.ECAbs _ => poly ()
adamc@25 647
adamc@179 648 | L.ERecord xes =>
adamc@179 649 let
adamc@179 650 val (xes, fm) = ListUtil.foldlMap
adamc@179 651 (fn ((x, e, t), fm) =>
adamc@179 652 let
adamc@179 653 val (e, fm) = monoExp (env, st, fm) e
adamc@179 654 in
adamc@179 655 ((monoName env x,
adamc@179 656 e,
adamc@179 657 monoType env t), fm)
adamc@179 658 end) fm xes
adamc@179 659 in
adamc@179 660 ((L'.ERecord xes, loc), fm)
adamc@179 661 end
adamc@179 662 | L.EField (e, x, _) =>
adamc@179 663 let
adamc@179 664 val (e, fm) = monoExp (env, st, fm) e
adamc@179 665 in
adamc@179 666 ((L'.EField (e, monoName env x), loc), fm)
adamc@179 667 end
adamc@149 668 | L.ECut _ => poly ()
adamc@73 669 | L.EFold _ => poly ()
adamc@177 670
adamc@182 671 | L.ECase (e, pes, {disc, result}) =>
adamc@179 672 let
adamc@179 673 val (e, fm) = monoExp (env, st, fm) e
adamc@179 674 val (pes, fm) = ListUtil.foldlMap
adamc@179 675 (fn ((p, e), fm) =>
adamc@179 676 let
adamc@179 677 val (e, fm) = monoExp (env, st, fm) e
adamc@179 678 in
adamc@182 679 ((monoPat env p, e), fm)
adamc@179 680 end) fm pes
adamc@179 681 in
adamc@182 682 ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm)
adamc@179 683 end
adamc@177 684
adamc@179 685 | L.EWrite e =>
adamc@179 686 let
adamc@179 687 val (e, fm) = monoExp (env, st, fm) e
adamc@179 688 in
adamc@179 689 ((L'.EWrite e, loc), fm)
adamc@179 690 end
adamc@110 691
adamc@179 692 | L.EClosure (n, es) =>
adamc@179 693 let
adamc@179 694 val (es, fm) = ListUtil.foldlMap (fn (e, fm) =>
adamc@179 695 monoExp (env, st, fm) e)
adamc@179 696 fm es
adamc@179 697 in
adamc@179 698 ((L'.EClosure (n, es), loc), fm)
adamc@179 699 end
adamc@25 700 end
adamc@25 701
adamc@179 702 fun monoDecl (env, fm) (all as (d, loc)) =
adamc@25 703 let
adamc@25 704 fun poly () =
adamc@25 705 (E.errorAt loc "Unsupported declaration";
adamc@25 706 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 707 NONE)
adamc@25 708 in
adamc@25 709 case d of
adamc@25 710 L.DCon _ => NONE
adamc@164 711 | L.DDatatype (x, n, xncs) =>
adamc@164 712 let
adamc@164 713 val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env) to)) xncs), loc)
adamc@164 714 in
adamc@179 715 SOME (Env.declBinds env all, fm, d)
adamc@164 716 end
adamc@179 717 | L.DVal (x, n, t, e, s) =>
adamc@179 718 let
adamc@179 719 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 720 in
adamc@179 721 SOME (Env.pushENamed env x n t NONE s,
adamc@179 722 fm,
adamc@179 723 (L'.DVal (x, n, monoType env t, e, s), loc))
adamc@179 724 end
adamc@128 725 | L.DValRec vis =>
adamc@128 726 let
adamc@128 727 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@179 728
adamc@179 729 val (vis, fm) = ListUtil.foldlMap
adamc@179 730 (fn ((x, n, t, e, s), fm) =>
adamc@179 731 let
adamc@179 732 val (e, fm) = monoExp (env, St.empty, fm) e
adamc@179 733 in
adamc@179 734 ((x, n, monoType env t, e, s), fm)
adamc@179 735 end)
adamc@179 736 fm vis
adamc@128 737 in
adamc@128 738 SOME (env,
adamc@179 739 fm,
adamc@179 740 (L'.DValRec vis, loc))
adamc@128 741 end
adamc@144 742 | L.DExport (ek, n) =>
adamc@115 743 let
adamc@120 744 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 745
adamc@120 746 fun unwind (t, _) =
adamc@120 747 case t of
adamc@120 748 L.TFun (dom, ran) => dom :: unwind ran
adamc@120 749 | _ => []
adamc@120 750
adamc@120 751 val ts = map (monoType env) (unwind t)
adamc@115 752 in
adamc@179 753 SOME (env, fm, (L'.DExport (ek, s, n, ts), loc))
adamc@115 754 end
adamc@25 755 end
adamc@25 756
adamc@25 757 fun monoize env ds =
adamc@25 758 let
adamc@179 759 val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) =>
adamc@179 760 case monoDecl (env, fm) d of
adamc@179 761 NONE => (env, fm, ds)
adamc@179 762 | SOME (env, fm, d) =>
adamc@179 763 (env,
adamc@179 764 Fm.enter fm,
adamc@179 765 d :: Fm.decls fm @ ds))
adamc@179 766 (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds
adamc@25 767 in
adamc@25 768 rev ds
adamc@25 769 end
adamc@25 770
adamc@25 771 end