annotate src/monoize.sml @ 152:67ab26888839

textarea
author Adam Chlipala <adamc@hcoop.net>
date Thu, 24 Jul 2008 10:41:53 -0400
parents 7420fa18d657
children cfe6f9db74aa
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@25 36 val dummyTyp = (L'.TNamed 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@25 68 | L.CNamed n => (L'.TNamed n, loc)
adamc@51 69 | L.CFfi mx => (L'.TFfi mx, loc)
adamc@25 70 | L.CApp _ => poly ()
adamc@25 71 | L.CAbs _ => poly ()
adamc@25 72
adamc@25 73 | L.CName _ => poly ()
adamc@25 74
adamc@25 75 | L.CRecord _ => poly ()
adamc@25 76 | L.CConcat _ => poly ()
adamc@69 77 | L.CFold _ => poly ()
adamc@87 78 | L.CUnit => poly ()
adamc@25 79 end
adamc@25 80
adamc@25 81 val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan)
adamc@25 82
adamc@120 83 fun fooifyExp name env =
adamc@120 84 let
adamc@120 85 fun fooify (e, tAll as (t, loc)) =
adamc@120 86 case #1 e of
adamc@120 87 L'.EClosure (fnam, [(L'.ERecord [], _)]) =>
adamc@120 88 let
adamc@120 89 val (_, _, _, s) = Env.lookupENamed env fnam
adamc@120 90 in
adamc@120 91 (L'.EPrim (Prim.String s), loc)
adamc@120 92 end
adamc@120 93 | L'.EClosure (fnam, args) =>
adamc@120 94 let
adamc@120 95 val (_, ft, _, s) = Env.lookupENamed env fnam
adamc@120 96 val ft = monoType env ft
adamc@111 97
adamc@120 98 fun attrify (args, ft, e) =
adamc@120 99 case (args, ft) of
adamc@120 100 ([], _) => e
adamc@120 101 | (arg :: args, (L'.TFun (t, ft), _)) =>
adamc@121 102 attrify (args, ft,
adamc@121 103 (L'.EStrcat (e,
adamc@121 104 (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc),
adamc@121 105 fooify (arg, t)), loc)), loc))
adamc@120 106 | _ => (E.errorAt loc "Type mismatch encoding attribute";
adamc@120 107 e)
adamc@120 108 in
adamc@120 109 attrify (args, ft, (L'.EPrim (Prim.String s), loc))
adamc@120 110 end
adamc@120 111 | _ =>
adamc@120 112 case t of
adamc@120 113 L'.TFfi ("Basis", "string") => (L'.EFfiApp ("Basis", name ^ "ifyString", [e]), loc)
adamc@120 114 | L'.TFfi ("Basis", "int") => (L'.EFfiApp ("Basis", name ^ "ifyInt", [e]), loc)
adamc@120 115 | L'.TFfi ("Basis", "float") => (L'.EFfiApp ("Basis", name ^ "ifyFloat", [e]), loc)
adamc@120 116 | L'.TRecord [] => (L'.EPrim (Prim.String ""), loc)
adamc@111 117
adamc@120 118 | _ => (E.errorAt loc "Don't know how to encode attribute type";
adamc@120 119 Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)];
adamc@120 120 dummyExp)
adamc@120 121 in
adamc@120 122 fooify
adamc@120 123 end
adamc@120 124
adamc@120 125 val attrifyExp = fooifyExp "attr"
adamc@120 126 val urlifyExp = fooifyExp "url"
adamc@105 127
adamc@143 128 datatype 'a failable_search =
adamc@143 129 Found of 'a
adamc@143 130 | NotFound
adamc@143 131 | Error
adamc@143 132
adamc@25 133 fun monoExp env (all as (e, loc)) =
adamc@25 134 let
adamc@25 135 fun poly () =
adamc@25 136 (E.errorAt loc "Unsupported expression";
adamc@25 137 Print.eprefaces' [("Expression", CorePrint.p_exp env all)];
adamc@25 138 dummyExp)
adamc@25 139 in
adamc@25 140 case e of
adamc@25 141 L.EPrim p => (L'.EPrim p, loc)
adamc@25 142 | L.ERel n => (L'.ERel n, loc)
adamc@25 143 | L.ENamed n => (L'.ENamed n, loc)
adamc@51 144 | L.EFfi mx => (L'.EFfi mx, loc)
adamc@51 145 | L.EFfiApp (m, x, es) => (L'.EFfiApp (m, x, map (monoExp env) es), loc)
adamc@94 146
adamc@139 147 | L.EApp (
adamc@139 148 (L.ECApp (
adamc@141 149 (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _),
adamc@139 150 _), _),
adamc@139 151 se) => (L'.EFfiApp ("Basis", "htmlifyString", [monoExp env se]), loc)
adamc@95 152 | L.EApp (
adamc@95 153 (L.EApp (
adamc@95 154 (L.ECApp (
adamc@95 155 (L.ECApp (
adamc@95 156 (L.ECApp (
adamc@139 157 (L.ECApp (
adamc@140 158 (L.EFfi ("Basis", "join"),
adamc@139 159 _), _), _),
adamc@139 160 _), _),
adamc@95 161 _), _),
adamc@95 162 _), _),
adamc@95 163 xml1), _),
adamc@95 164 xml2) => (L'.EStrcat (monoExp env xml1, monoExp env xml2), loc)
adamc@95 165
adamc@95 166 | L.EApp (
adamc@95 167 (L.EApp (
adamc@104 168 (L.EApp (
adamc@95 169 (L.ECApp (
adamc@104 170 (L.ECApp (
adamc@104 171 (L.ECApp (
adamc@104 172 (L.ECApp (
adamc@139 173 (L.ECApp (
adamc@139 174 (L.ECApp (
adamc@139 175 (L.ECApp (
adamc@139 176 (L.ECApp (
adamc@139 177 (L.EFfi ("Basis", "tag"),
adamc@139 178 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@104 179 attrs), _),
adamc@95 180 tag), _),
adamc@95 181 xml) =>
adamc@95 182 let
adamc@140 183 fun getTag' (e, _) =
adamc@140 184 case e of
adamc@143 185 L.EFfi ("Basis", tag) => (tag, [])
adamc@143 186 | L.ECApp (e, t) => let
adamc@143 187 val (tag, ts) = getTag' e
adamc@143 188 in
adamc@143 189 (tag, ts @ [t])
adamc@143 190 end
adamc@140 191 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@140 192 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 193 ("", []))
adamc@140 194
adamc@95 195 fun getTag (e, _) =
adamc@95 196 case e of
adamc@143 197 L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, [])
adamc@140 198 | L.EApp (e, (L.ERecord [], _)) => getTag' e
adamc@95 199 | _ => (E.errorAt loc "Non-constant XML tag";
adamc@95 200 Print.eprefaces' [("Expression", CorePrint.p_exp env tag)];
adamc@143 201 ("", []))
adamc@95 202
adamc@143 203 val (tag, targs) = getTag tag
adamc@95 204
adamc@104 205 val attrs = monoExp env attrs
adamc@104 206
adamc@143 207 fun tagStart tag =
adamc@104 208 case #1 attrs of
adamc@104 209 L'.ERecord xes =>
adamc@104 210 let
adamc@104 211 fun lowercaseFirst "" = ""
adamc@143 212 | lowercaseFirst s = str (Char.toLower (String.sub (s, 0)))
adamc@143 213 ^ String.extract (s, 1, NONE)
adamc@104 214
adamc@104 215 val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc)
adamc@104 216 in
adamc@105 217 foldl (fn ((x, e, t), s) =>
adamc@104 218 let
adamc@104 219 val xp = " " ^ lowercaseFirst x ^ "=\""
adamc@120 220
adamc@120 221 val fooify =
adamc@120 222 case x of
adamc@120 223 "Link" => urlifyExp
adamc@143 224 | "Action" => urlifyExp
adamc@120 225 | _ => attrifyExp
adamc@104 226 in
adamc@104 227 (L'.EStrcat (s,
adamc@104 228 (L'.EStrcat ((L'.EPrim (Prim.String xp), loc),
adamc@120 229 (L'.EStrcat (fooify env (e, t),
adamc@143 230 (L'.EPrim (Prim.String "\""),
adamc@143 231 loc)),
adamc@104 232 loc)),
adamc@104 233 loc)), loc)
adamc@104 234 end)
adamc@143 235 s xes
adamc@104 236 end
adamc@143 237 | _ => raise Fail "Non-record attributes!"
adamc@104 238
adamc@143 239 fun input typ =
adamc@143 240 case targs of
adamc@143 241 [(L.CName name, _)] =>
adamc@143 242 (L'.EStrcat (tagStart "input",
adamc@143 243 (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")),
adamc@143 244 loc)), loc)
adamc@143 245 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@143 246 raise Fail "No named passed to input tag")
adamc@104 247
adamc@152 248 fun normal (tag, extra) =
adamc@143 249 let
adamc@143 250 val tagStart = tagStart tag
adamc@152 251 val tagStart = case extra of
adamc@152 252 NONE => tagStart
adamc@152 253 | SOME extra => (L'.EStrcat (tagStart, extra), loc)
adamc@152 254
adamc@143 255 fun normal () =
adamc@143 256 (L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc),
adamc@143 257 (L'.EStrcat (monoExp env xml,
adamc@143 258 (L'.EPrim (Prim.String (String.concat ["</", tag, ">"])),
adamc@143 259 loc)), loc)),
adamc@143 260 loc)
adamc@143 261 in
adamc@143 262 case xml of
adamc@143 263 (L.EApp ((L.ECApp (
adamc@143 264 (L.ECApp ((L.EFfi ("Basis", "cdata"), _),
adamc@143 265 _), _),
adamc@143 266 _), _),
adamc@143 267 (L.EPrim (Prim.String s), _)), _) =>
adamc@143 268 if CharVector.all Char.isSpace s then
adamc@143 269 (L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc)
adamc@143 270 else
adamc@143 271 normal ()
adamc@143 272 | _ => normal ()
adamc@143 273 end
adamc@152 274 in
adamc@152 275 case tag of
adamc@152 276 "submit" => (L'.EPrim (Prim.String "<input type=\"submit\"/>"), loc)
adamc@152 277
adamc@152 278 | "textbox" =>
adamc@152 279 (case targs of
adamc@152 280 [_, (L.CName name, _)] =>
adamc@152 281 (L'.EStrcat (tagStart "input",
adamc@152 282 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")),
adamc@152 283 loc)), loc)
adamc@152 284 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@152 285 raise Fail "No named passed to textarea tag"))
adamc@152 286 | "ltextarea" =>
adamc@152 287 (case targs of
adamc@152 288 [_, (L.CName name, _)] =>
adamc@152 289 (L'.EStrcat ((L'.EStrcat (tagStart "textarea",
adamc@152 290 (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc),
adamc@152 291 (L'.EStrcat (monoExp env xml,
adamc@152 292 (L'.EPrim (Prim.String "</textarea>"),
adamc@152 293 loc)), loc)),
adamc@152 294 loc)
adamc@152 295 | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs);
adamc@152 296 raise Fail "No named passed to ltextarea tag"))
adamc@152 297
adamc@152 298 | _ => normal (tag, NONE)
adamc@95 299 end
adamc@94 300
adamc@141 301 | L.EApp ((L.ECApp (
adamc@141 302 (L.ECApp ((L.EFfi ("Basis", "lform"), _), _), _),
adamc@141 303 _), _),
adamc@141 304 xml) =>
adamc@143 305 let
adamc@143 306 fun findSubmit (e, _) =
adamc@143 307 case e of
adamc@143 308 L.EApp (
adamc@143 309 (L.EApp (
adamc@143 310 (L.ECApp (
adamc@143 311 (L.ECApp (
adamc@143 312 (L.ECApp (
adamc@143 313 (L.ECApp (
adamc@143 314 (L.EFfi ("Basis", "join"),
adamc@143 315 _), _), _),
adamc@143 316 _), _),
adamc@143 317 _), _),
adamc@143 318 _), _),
adamc@143 319 xml1), _),
adamc@143 320 xml2) => (case findSubmit xml1 of
adamc@143 321 Error => Error
adamc@143 322 | NotFound => findSubmit xml2
adamc@143 323 | Found e =>
adamc@143 324 case findSubmit xml2 of
adamc@143 325 NotFound => Found e
adamc@143 326 | _ => Error)
adamc@143 327 | L.EApp (
adamc@143 328 (L.EApp (
adamc@143 329 (L.EApp (
adamc@143 330 (L.ECApp (
adamc@143 331 (L.ECApp (
adamc@143 332 (L.ECApp (
adamc@143 333 (L.ECApp (
adamc@143 334 (L.ECApp (
adamc@143 335 (L.ECApp (
adamc@143 336 (L.ECApp (
adamc@143 337 (L.ECApp (
adamc@143 338 (L.EFfi ("Basis", "tag"),
adamc@143 339 _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _),
adamc@143 340 attrs), _),
adamc@143 341 _), _),
adamc@143 342 xml) =>
adamc@143 343 (case #1 attrs of
adamc@143 344 L.ERecord xes =>
adamc@143 345 (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t)
adamc@143 346 | _ => NONE) xes of
adamc@143 347 NONE => findSubmit xml
adamc@143 348 | SOME et =>
adamc@143 349 case findSubmit xml of
adamc@143 350 NotFound => Found et
adamc@143 351 | _ => Error)
adamc@143 352 | _ => findSubmit xml)
adamc@143 353 | _ => NotFound
adamc@143 354
adamc@143 355 val (action, actionT) = case findSubmit xml of
adamc@143 356 NotFound => raise Fail "No submit found"
adamc@143 357 | Error => raise Fail "Not ready for multi-submit lforms yet"
adamc@143 358 | Found et => et
adamc@143 359
adamc@143 360 val actionT = monoType env actionT
adamc@143 361 val action = monoExp env action
adamc@143 362 in
adamc@143 363 (L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String "<form action=\""), loc),
adamc@143 364 (L'.EStrcat (urlifyExp env (action, actionT),
adamc@143 365 (L'.EPrim (Prim.String "\">"), loc)), loc)), loc),
adamc@143 366 (L'.EStrcat (monoExp env xml,
adamc@143 367 (L'.EPrim (Prim.String "</form>"), loc)), loc)), loc)
adamc@143 368 end
adamc@141 369
adamc@148 370 | L.EApp ((L.ECApp (
adamc@148 371 (L.ECApp (
adamc@148 372 (L.ECApp (
adamc@148 373 (L.ECApp (
adamc@148 374 (L.EFfi ("Basis", "useMore"), _), _), _),
adamc@148 375 _), _),
adamc@148 376 _), _),
adamc@148 377 _), _),
adamc@148 378 xml) => monoExp env xml
adamc@148 379
adamc@148 380
adamc@25 381 | L.EApp (e1, e2) => (L'.EApp (monoExp env e1, monoExp env e2), loc)
adamc@26 382 | L.EAbs (x, dom, ran, e) =>
adamc@26 383 (L'.EAbs (x, monoType env dom, monoType env ran, monoExp (Env.pushERel env x dom) e), loc)
adamc@25 384 | L.ECApp _ => poly ()
adamc@25 385 | L.ECAbs _ => poly ()
adamc@25 386
adamc@29 387 | L.ERecord xes => (L'.ERecord (map (fn (x, e, t) => (monoName env x, monoExp env e, monoType env t)) xes), loc)
adamc@25 388 | L.EField (e, x, _) => (L'.EField (monoExp env e, monoName env x), loc)
adamc@149 389 | L.ECut _ => poly ()
adamc@73 390 | L.EFold _ => poly ()
adamc@102 391 | L.EWrite e => (L'.EWrite (monoExp env e), loc)
adamc@110 392
adamc@111 393 | L.EClosure (n, es) => (L'.EClosure (n, map (monoExp env) es), loc)
adamc@25 394 end
adamc@25 395
adamc@25 396 fun monoDecl env (all as (d, loc)) =
adamc@25 397 let
adamc@25 398 fun poly () =
adamc@25 399 (E.errorAt loc "Unsupported declaration";
adamc@25 400 Print.eprefaces' [("Declaration", CorePrint.p_decl env all)];
adamc@25 401 NONE)
adamc@25 402 in
adamc@25 403 case d of
adamc@25 404 L.DCon _ => NONE
adamc@109 405 | L.DVal (x, n, t, e, s) => SOME (Env.pushENamed env x n t (SOME e) s,
adamc@109 406 (L'.DVal (x, n, monoType env t, monoExp env e, s), loc))
adamc@128 407 | L.DValRec vis =>
adamc@128 408 let
adamc@128 409 val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis
adamc@128 410 in
adamc@128 411 SOME (env,
adamc@128 412 (L'.DValRec (map (fn (x, n, t, e, s) => (x, n, monoType env t, monoExp env e, s)) vis), loc))
adamc@128 413 end
adamc@144 414 | L.DExport (ek, n) =>
adamc@115 415 let
adamc@120 416 val (_, t, _, s) = Env.lookupENamed env n
adamc@120 417
adamc@120 418 fun unwind (t, _) =
adamc@120 419 case t of
adamc@120 420 L.TFun (dom, ran) => dom :: unwind ran
adamc@120 421 | _ => []
adamc@120 422
adamc@120 423 val ts = map (monoType env) (unwind t)
adamc@115 424 in
adamc@144 425 SOME (env, (L'.DExport (ek, s, n, ts), loc))
adamc@115 426 end
adamc@25 427 end
adamc@25 428
adamc@25 429 fun monoize env ds =
adamc@25 430 let
adamc@25 431 val (_, ds) = List.foldl (fn (d, (env, ds)) =>
adamc@25 432 case monoDecl env d of
adamc@25 433 NONE => (env, ds)
adamc@25 434 | SOME (env, d) => (env, d :: ds)) (env, []) ds
adamc@25 435 in
adamc@25 436 rev ds
adamc@25 437 end
adamc@25 438
adamc@25 439 end