annotate src/jscomp.sml @ 594:55829473f6a7

Injected an option
author Adam Chlipala <adamc@hcoop.net>
date Fri, 02 Jan 2009 13:03:22 -0500
parents f277f5faebcd
children 02c8ab9f3e8b
rev   line source
adamc@567 1 (* Copyright (c) 2008, Adam Chlipala
adamc@567 2 * All rights reserved.
adamc@567 3 *
adamc@567 4 * Redistribution and use in source and binary forms, with or without
adamc@567 5 * modification, are permitted provided that the following conditions are met:
adamc@567 6 *
adamc@567 7 * - Redistributions of source code must retain the above copyright notice,
adamc@567 8 * this list of conditions and the following disclaimer.
adamc@567 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@567 10 * this list of conditions and the following disclaimer in the documentation
adamc@567 11 * and/or other materials provided with the distribution.
adamc@567 12 * - The names of contributors may not be used to endorse or promote products
adamc@567 13 * derived from this software without specific prior written permission.
adamc@567 14 *
adamc@567 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@567 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@567 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@567 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@567 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@567 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@567 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@567 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@567 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@567 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@567 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@567 26 *)
adamc@567 27
adamc@567 28 structure JsComp :> JSCOMP = struct
adamc@567 29
adamc@567 30 open Mono
adamc@567 31
adamc@567 32 structure EM = ErrorMsg
adamc@567 33 structure E = MonoEnv
adamc@567 34 structure U = MonoUtil
adamc@567 35
adamc@589 36 structure IS = IntBinarySet
adamc@589 37 structure IM = IntBinaryMap
adamc@589 38
adamc@572 39 val funcs = [(("Basis", "alert"), "alert"),
adamc@586 40 (("Basis", "htmlifyBool"), "bs"),
adamc@583 41 (("Basis", "htmlifyFloat"), "ts"),
adamc@582 42 (("Basis", "htmlifyInt"), "ts"),
adamc@574 43 (("Basis", "htmlifyString"), "escape"),
adamc@579 44 (("Basis", "new_client_source"), "sc"),
adamc@579 45 (("Basis", "set_client_source"), "sv")]
adamc@572 46
adamc@572 47 structure FM = BinaryMapFn(struct
adamc@572 48 type ord_key = string * string
adamc@572 49 fun compare ((m1, x1), (m2, x2)) =
adamc@572 50 Order.join (String.compare (m1, m2),
adamc@572 51 fn () => String.compare (x1, x2))
adamc@572 52 end)
adamc@572 53
adamc@572 54 val funcs = foldl (fn ((k, v), m) => FM.insert (m, k, v)) FM.empty funcs
adamc@572 55
adamc@572 56 fun ffi k = FM.find (funcs, k)
adamc@572 57
adamc@567 58 type state = {
adamc@567 59 decls : decl list,
adamc@589 60 script : string list,
adamc@589 61 included : IS.set
adamc@567 62 }
adamc@567 63
adamc@567 64 fun varDepth (e, _) =
adamc@567 65 case e of
adamc@567 66 EPrim _ => 0
adamc@567 67 | ERel _ => 0
adamc@567 68 | ENamed _ => 0
adamc@567 69 | ECon (_, _, NONE) => 0
adamc@567 70 | ECon (_, _, SOME e) => varDepth e
adamc@567 71 | ENone _ => 0
adamc@567 72 | ESome (_, e) => varDepth e
adamc@567 73 | EFfi _ => 0
adamc@567 74 | EFfiApp (_, _, es) => foldl Int.max 0 (map varDepth es)
adamc@567 75 | EApp (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 76 | EAbs _ => 0
adamc@567 77 | EUnop (_, e) => varDepth e
adamc@567 78 | EBinop (_, e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 79 | ERecord xes => foldl Int.max 0 (map (fn (_, e, _) => varDepth e) xes)
adamc@567 80 | EField (e, _) => varDepth e
adamc@567 81 | ECase (e, pes, _) =>
adamc@567 82 foldl Int.max (varDepth e)
adamc@567 83 (map (fn (p, e) => E.patBindsN p + varDepth e) pes)
adamc@567 84 | EStrcat (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 85 | EError (e, _) => varDepth e
adamc@567 86 | EWrite e => varDepth e
adamc@567 87 | ESeq (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 88 | ELet (_, _, e1, e2) => Int.max (varDepth e1, 1 + varDepth e2)
adamc@567 89 | EClosure _ => 0
adamc@567 90 | EQuery _ => 0
adamc@567 91 | EDml _ => 0
adamc@567 92 | ENextval _ => 0
adamc@567 93 | EUnurlify _ => 0
adamc@567 94 | EJavaScript _ => 0
adamc@568 95 | ESignalReturn e => varDepth e
adamc@572 96 | ESignalBind (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@574 97 | ESignalSource e => varDepth e
adamc@567 98
adamc@591 99 fun closedUpto d =
adamc@591 100 let
adamc@591 101 fun cu inner (e, _) =
adamc@591 102 case e of
adamc@591 103 EPrim _ => true
adamc@591 104 | ERel n => n < inner orelse n - inner >= d
adamc@591 105 | ENamed _ => true
adamc@591 106 | ECon (_, _, NONE) => true
adamc@591 107 | ECon (_, _, SOME e) => cu inner e
adamc@591 108 | ENone _ => true
adamc@591 109 | ESome (_, e) => cu inner e
adamc@591 110 | EFfi _ => true
adamc@591 111 | EFfiApp (_, _, es) => List.all (cu inner) es
adamc@591 112 | EApp (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 113 | EAbs (_, _, _, e) => cu (inner + 1) e
adamc@591 114 | EUnop (_, e) => cu inner e
adamc@591 115 | EBinop (_, e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 116 | ERecord xes => List.all (fn (_, e, _) => cu inner e) xes
adamc@591 117 | EField (e, _) => cu inner e
adamc@591 118 | ECase (e, pes, _) =>
adamc@591 119 cu inner e
adamc@591 120 andalso List.all (fn (p, e) => cu (inner + E.patBindsN p) e) pes
adamc@591 121 | EStrcat (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 122 | EError (e, _) => cu inner e
adamc@591 123 | EWrite e => cu inner e
adamc@591 124 | ESeq (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 125 | ELet (_, _, e1, e2) => cu inner e1 andalso cu (inner + 1) e2
adamc@591 126 | EClosure (_, es) => List.all (cu inner) es
adamc@591 127 | EQuery {query, body, initial, ...} =>
adamc@591 128 cu inner query
adamc@591 129 andalso cu (inner + 2) body
adamc@591 130 andalso cu inner initial
adamc@591 131 | EDml e => cu inner e
adamc@591 132 | ENextval e => cu inner e
adamc@591 133 | EUnurlify (e, _) => cu inner e
adamc@591 134 | EJavaScript (_, e, _) => cu inner e
adamc@591 135 | ESignalReturn e => cu inner e
adamc@591 136 | ESignalBind (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 137 | ESignalSource e => cu inner e
adamc@591 138 in
adamc@591 139 cu 0
adamc@591 140 end
adamc@591 141
adamc@568 142 fun strcat loc es =
adamc@568 143 case es of
adamc@568 144 [] => (EPrim (Prim.String ""), loc)
adamc@568 145 | [x] => x
adamc@568 146 | x :: es' => (EStrcat (x, strcat loc es'), loc)
adamc@568 147
adamc@589 148 fun process file =
adamc@567 149 let
adamc@589 150 val nameds = foldl (fn ((DVal (_, n, t, e, _), _), nameds) => IM.insert (nameds, n, e)
adamc@589 151 | ((DValRec vis, _), nameds) =>
adamc@589 152 foldl (fn ((_, n, _, e, _), nameds) => IM.insert (nameds, n, e))
adamc@589 153 nameds vis
adamc@589 154 | (_, nameds) => nameds)
adamc@589 155 IM.empty file
adamc@567 156
adamc@590 157 fun str loc s = (EPrim (Prim.String s), loc)
adamc@590 158
adamc@594 159 fun isNullable (t, _) =
adamc@594 160 case t of
adamc@594 161 TOption _ => true
adamc@594 162 | TRecord [] => true
adamc@594 163 | _ => false
adamc@594 164
adamc@593 165 fun quoteExp loc (t : typ) (e, st) =
adamc@590 166 case #1 t of
adamc@593 167 TSource => (strcat loc [str loc "s",
adamc@593 168 (EFfiApp ("Basis", "htmlifyInt", [e]), loc)], st)
adamc@590 169
adamc@593 170 | TRecord [] => (str loc "null", st)
adamc@593 171 | TRecord [(x, t)] =>
adamc@593 172 let
adamc@593 173 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 174 in
adamc@593 175 (strcat loc [str loc ("{_" ^ x ^ ":"),
adamc@593 176 e,
adamc@593 177 str loc "}"], st)
adamc@593 178 end
adamc@593 179 | TRecord ((x, t) :: xts) =>
adamc@593 180 let
adamc@593 181 val (e', st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 182 val (es, st) = ListUtil.foldlMap
adamc@593 183 (fn ((x, t), st) =>
adamc@593 184 let
adamc@593 185 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 186 in
adamc@593 187 (strcat loc [str loc (",_" ^ x ^ ":"), e], st)
adamc@593 188 end)
adamc@593 189 st xts
adamc@593 190 in
adamc@593 191 (strcat loc (str loc ("{_" ^ x ^ ":")
adamc@593 192 :: e'
adamc@593 193 :: es
adamc@593 194 @ [str loc "}"]), st)
adamc@593 195 end
adamc@590 196
adamc@593 197 | TFfi ("Basis", "string") => ((EFfiApp ("Basis", "jsifyString", [e]), loc), st)
adamc@593 198 | TFfi ("Basis", "int") => ((EFfiApp ("Basis", "htmlifyInt", [e]), loc), st)
adamc@593 199 | TFfi ("Basis", "float") => ((EFfiApp ("Basis", "htmlifyFloat", [e]), loc), st)
adamc@593 200
adamc@593 201 | TFfi ("Basis", "bool") => ((ECase (e,
adamc@593 202 [((PCon (Enum, PConFfi {mod = "Basis",
adamc@593 203 datatyp = "bool",
adamc@593 204 con = "True",
adamc@593 205 arg = NONE}, NONE), loc),
adamc@593 206 str loc "true"),
adamc@593 207 ((PCon (Enum, PConFfi {mod = "Basis",
adamc@593 208 datatyp = "bool",
adamc@593 209 con = "False",
adamc@593 210 arg = NONE}, NONE), loc),
adamc@593 211 str loc "false")],
adamc@593 212 {disc = (TFfi ("Basis", "bool"), loc),
adamc@593 213 result = (TFfi ("Basis", "string"), loc)}), loc),
adamc@593 214 st)
adamc@592 215
adamc@594 216 | TOption t =>
adamc@594 217 let
adamc@594 218 val (e', st) = quoteExp loc t ((ERel 0, loc), st)
adamc@594 219 in
adamc@594 220 ((ECase (e,
adamc@594 221 [((PNone t, loc),
adamc@594 222 str loc "null"),
adamc@594 223 ((PSome (t, (PVar ("x", t), loc)), loc),
adamc@594 224 if isNullable t then
adamc@594 225 strcat loc [str loc "{v:", e', str loc "}"]
adamc@594 226 else
adamc@594 227 e')],
adamc@594 228 {disc = (TOption t, loc),
adamc@594 229 result = (TFfi ("Basis", "string"), loc)}), loc),
adamc@594 230 st)
adamc@594 231 end
adamc@594 232
adamc@590 233 | _ => (EM.errorAt loc "Don't know how to embed type in JavaScript";
adamc@590 234 Print.prefaces "Can't embed" [("t", MonoPrint.p_typ MonoEnv.empty t)];
adamc@593 235 (str loc "ERROR", st))
adamc@590 236
adamc@589 237 fun jsExp mode skip outer =
adamc@567 238 let
adamc@589 239 val len = length outer
adamc@567 240
adamc@589 241 fun jsE inner (e as (_, loc), st) =
adamc@589 242 let
adamc@590 243 val str = str loc
adamc@567 244
adamc@589 245 fun var n = Int.toString (len + inner - n - 1)
adamc@567 246
adamc@589 247 fun patCon pc =
adamc@589 248 case pc of
adamc@589 249 PConVar n => str (Int.toString n)
adamc@589 250 | PConFfi {mod = "Basis", con = "True", ...} => str "true"
adamc@589 251 | PConFfi {mod = "Basis", con = "False", ...} => str "false"
adamc@589 252 | PConFfi {con, ...} => str ("\"_" ^ con ^ "\"")
adamc@567 253
adamc@591 254 fun unsupported s =
adamc@591 255 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript[2]");
adamc@591 256 (str "ERROR", st))
adamc@577 257
adamc@589 258 val strcat = strcat loc
adamc@584 259
adamc@589 260 fun jsPrim p =
adamc@589 261 case p of
adamc@589 262 Prim.String s =>
adamc@589 263 str ("\""
adamc@589 264 ^ String.translate (fn #"'" =>
adamc@589 265 if mode = Attribute then
adamc@589 266 "\\047"
adamc@589 267 else
adamc@589 268 "'"
adamc@589 269 | #"\"" => "\\\""
adamc@589 270 | #"<" =>
adamc@589 271 if mode = Script then
adamc@589 272 "<"
adamc@589 273 else
adamc@589 274 "\\074"
adamc@589 275 | #"\\" => "\\\\"
adamc@589 276 | ch => String.str ch) s
adamc@589 277 ^ "\"")
adamc@589 278 | _ => str (Prim.toString p)
adamc@589 279
adamc@589 280 fun jsPat depth inner (p, _) succ fail =
adamc@589 281 case p of
adamc@589 282 PWild => succ
adamc@589 283 | PVar _ => strcat [str ("(_" ^ Int.toString (len + inner) ^ "=d"
adamc@589 284 ^ Int.toString depth ^ ","),
adamc@589 285 succ,
adamc@589 286 str ")"]
adamc@589 287 | PPrim p => strcat [str ("(d" ^ Int.toString depth ^ "=="),
adamc@589 288 jsPrim p,
adamc@589 289 str "?",
adamc@589 290 succ,
adamc@589 291 str ":",
adamc@589 292 fail,
adamc@589 293 str ")"]
adamc@589 294 | PCon (_, PConFfi {mod = "Basis", con = "True", ...}, NONE) =>
adamc@589 295 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 296 succ,
adamc@589 297 str ":",
adamc@589 298 fail,
adamc@589 299 str ")"]
adamc@589 300 | PCon (_, PConFfi {mod = "Basis", con = "False", ...}, NONE) =>
adamc@589 301 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 302 fail,
adamc@589 303 str ":",
adamc@589 304 succ,
adamc@589 305 str ")"]
adamc@589 306 | PCon (_, pc, NONE) =>
adamc@589 307 strcat [str ("(d" ^ Int.toString depth ^ "=="),
adamc@589 308 patCon pc,
adamc@589 309 str "?",
adamc@589 310 succ,
adamc@589 311 str ":",
adamc@589 312 fail,
adamc@589 313 str ")"]
adamc@589 314 | PCon (_, pc, SOME p) =>
adamc@589 315 strcat [str ("(d" ^ Int.toString depth ^ ".n=="),
adamc@589 316 patCon pc,
adamc@589 317 str ("?(d" ^ Int.toString depth ^ "=d" ^ Int.toString depth ^ ".v,"),
adamc@589 318 jsPat depth inner p succ fail,
adamc@589 319 str "):",
adamc@589 320 fail,
adamc@589 321 str ")"]
adamc@589 322 | PRecord xps =>
adamc@589 323 let
adamc@589 324 val (_, succ) = foldl
adamc@589 325 (fn ((x, p, _), (inner, succ)) =>
adamc@589 326 (inner + E.patBindsN p,
adamc@589 327 strcat [str ("(d" ^ Int.toString (depth+1) ^ "=d"
adamc@589 328 ^ Int.toString depth ^ "._" ^ x ^ ","),
adamc@589 329 jsPat (depth+1) inner p succ fail,
adamc@589 330 str ")"]))
adamc@589 331 (inner, succ) xps
adamc@589 332 in
adamc@589 333 succ
adamc@589 334 end
adamc@589 335 | PNone _ => strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 336 fail,
adamc@589 337 str ":",
adamc@589 338 succ,
adamc@589 339 str ")"]
adamc@594 340 | PSome (t, p) => strcat (str ("(d" ^ Int.toString depth ^ "?")
adamc@594 341 :: (if isNullable t then
adamc@594 342 [str ("d" ^ Int.toString depth
adamc@594 343 ^ "=d" ^ Int.toString depth ^ ".v")]
adamc@594 344 else
adamc@594 345 [])
adamc@594 346 @ [jsPat depth inner p succ fail,
adamc@594 347 str ":",
adamc@594 348 fail,
adamc@594 349 str ")"])
adamc@589 350
adamc@589 351 fun deStrcat (e, _) =
adamc@589 352 case e of
adamc@589 353 EPrim (Prim.String s) => s
adamc@589 354 | EStrcat (e1, e2) => deStrcat e1 ^ deStrcat e2
adamc@589 355 | _ => raise Fail "Jscomp: deStrcat"
adamc@590 356
adamc@590 357 val quoteExp = quoteExp loc
adamc@567 358 in
adamc@590 359 (*Print.prefaces "jsE" [("e", MonoPrint.p_exp MonoEnv.empty e)];*)
adamc@590 360
adamc@589 361 case #1 e of
adamc@589 362 EPrim p => (jsPrim p, st)
adamc@589 363 | ERel n =>
adamc@589 364 if n < inner then
adamc@589 365 (str ("_" ^ var n), st)
adamc@589 366 else
adamc@589 367 let
adamc@589 368 val n = n - inner
adamc@589 369 in
adamc@593 370 quoteExp (List.nth (outer, n)) ((ERel (n - skip), loc), st)
adamc@589 371 end
adamc@567 372
adamc@589 373 | ENamed n =>
adamc@589 374 let
adamc@589 375 val st =
adamc@589 376 if IS.member (#included st, n) then
adamc@589 377 st
adamc@589 378 else
adamc@589 379 case IM.find (nameds, n) of
adamc@589 380 NONE => raise Fail "Jscomp: Unbound ENamed"
adamc@589 381 | SOME e =>
adamc@589 382 let
adamc@589 383 val st = {decls = #decls st,
adamc@589 384 script = #script st,
adamc@589 385 included = IS.add (#included st, n)}
adamc@578 386
adamc@589 387 val (e, st) = jsExp mode skip [] 0 (e, st)
adamc@589 388 val e = deStrcat e
adamc@589 389
adamc@589 390 val sc = "_n" ^ Int.toString n ^ "=" ^ e ^ ";\n"
adamc@589 391 in
adamc@589 392 {decls = #decls st,
adamc@589 393 script = sc :: #script st,
adamc@589 394 included = #included st}
adamc@589 395 end
adamc@589 396 in
adamc@589 397 (str ("_n" ^ Int.toString n), st)
adamc@589 398 end
adamc@589 399
adamc@589 400 | ECon (_, pc, NONE) => (patCon pc, st)
adamc@589 401 | ECon (_, pc, SOME e) =>
adamc@589 402 let
adamc@589 403 val (s, st) = jsE inner (e, st)
adamc@589 404 in
adamc@589 405 (strcat [str "{n:",
adamc@589 406 patCon pc,
adamc@589 407 str ",v:",
adamc@589 408 s,
adamc@589 409 str "}"], st)
adamc@589 410 end
adamc@589 411 | ENone _ => (str "null", st)
adamc@589 412 | ESome (t, e) =>
adamc@572 413 let
adamc@572 414 val (e, st) = jsE inner (e, st)
adamc@572 415 in
adamc@589 416 (if isNullable t then
adamc@589 417 strcat [str "{v:", e, str "}"]
adamc@589 418 else
adamc@589 419 e, st)
adamc@589 420 end
adamc@589 421
adamc@589 422 | EFfi k =>
adamc@589 423 let
adamc@589 424 val name = case ffi k of
adamc@589 425 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k
adamc@589 426 ^ " in JavaScript");
adamc@589 427 "ERROR")
adamc@589 428 | SOME s => s
adamc@589 429 in
adamc@589 430 (str name, st)
adamc@589 431 end
adamc@589 432 | EFfiApp (m, x, args) =>
adamc@589 433 let
adamc@589 434 val args =
adamc@589 435 case (m, x, args) of
adamc@589 436 ("Basis", "new_client_source", [(EJavaScript (_, e, _), _)]) => [e]
adamc@589 437 | ("Basis", "set_client_source", [e1, (EJavaScript (_, e2, _), _)]) => [e1, e2]
adamc@589 438 | _ => args
adamc@589 439
adamc@589 440 val name = case ffi (m, x) of
adamc@589 441 NONE => (EM.errorAt loc ("Unsupported FFI function "
adamc@589 442 ^ x ^ " in JavaScript");
adamc@589 443 "ERROR")
adamc@589 444 | SOME s => s
adamc@589 445 in
adamc@589 446 case args of
adamc@589 447 [] => (str (name ^ "()"), st)
adamc@589 448 | [e] =>
adamc@589 449 let
adamc@589 450 val (e, st) = jsE inner (e, st)
adamc@589 451 in
adamc@589 452 (strcat [str (name ^ "("),
adamc@589 453 e,
adamc@589 454 str ")"], st)
adamc@589 455 end
adamc@589 456 | e :: es =>
adamc@589 457 let
adamc@589 458 val (e, st) = jsE inner (e, st)
adamc@589 459 val (es, st) = ListUtil.foldlMapConcat
adamc@589 460 (fn (e, st) =>
adamc@589 461 let
adamc@589 462 val (e, st) = jsE inner (e, st)
adamc@589 463 in
adamc@589 464 ([str ",", e], st)
adamc@589 465 end)
adamc@589 466 st es
adamc@589 467 in
adamc@589 468 (strcat (str (name ^ "(")
adamc@589 469 :: e
adamc@589 470 :: es
adamc@589 471 @ [str ")"]), st)
adamc@589 472 end
adamc@589 473 end
adamc@589 474
adamc@589 475 | EApp (e1, e2) =>
adamc@589 476 let
adamc@589 477 val (e1, st) = jsE inner (e1, st)
adamc@589 478 val (e2, st) = jsE inner (e2, st)
adamc@589 479 in
adamc@589 480 (strcat [e1, str "(", e2, str ")"], st)
adamc@589 481 end
adamc@589 482 | EAbs (_, _, _, e) =>
adamc@589 483 let
adamc@589 484 val locals = List.tabulate
adamc@589 485 (varDepth e,
adamc@589 486 fn i => str ("var _" ^ Int.toString (len + inner + i + 1) ^ ";"))
adamc@589 487 val (e, st) = jsE (inner + 1) (e, st)
adamc@589 488 in
adamc@589 489 (strcat (str ("function(_"
adamc@589 490 ^ Int.toString (len + inner)
adamc@589 491 ^ "){")
adamc@589 492 :: locals
adamc@589 493 @ [str "return ",
adamc@589 494 e,
adamc@589 495 str "}"]),
adamc@589 496 st)
adamc@589 497 end
adamc@589 498
adamc@589 499 | EUnop (s, e) =>
adamc@589 500 let
adamc@589 501 val (e, st) = jsE inner (e, st)
adamc@589 502 in
adamc@589 503 (strcat [str ("(" ^ s),
adamc@572 504 e,
adamc@589 505 str ")"],
adamc@589 506 st)
adamc@589 507 end
adamc@589 508 | EBinop (s, e1, e2) =>
adamc@589 509 let
adamc@589 510 val (e1, st) = jsE inner (e1, st)
adamc@589 511 val (e2, st) = jsE inner (e2, st)
adamc@589 512 in
adamc@589 513 (strcat [str "(",
adamc@589 514 e1,
adamc@589 515 str s,
adamc@589 516 e2,
adamc@589 517 str ")"],
adamc@589 518 st)
adamc@589 519 end
adamc@589 520
adamc@589 521 | ERecord [] => (str "null", st)
adamc@589 522 | ERecord [(x, e, _)] =>
adamc@589 523 let
adamc@589 524 val (e, st) = jsE inner (e, st)
adamc@589 525 in
adamc@589 526 (strcat [str "{_x:", e, str "}"], st)
adamc@589 527 end
adamc@589 528 | ERecord ((x, e, _) :: xes) =>
adamc@589 529 let
adamc@589 530 val (e, st) = jsE inner (e, st)
adamc@589 531
adamc@589 532 val (es, st) =
adamc@589 533 foldr (fn ((x, e, _), (es, st)) =>
adamc@589 534 let
adamc@589 535 val (e, st) = jsE inner (e, st)
adamc@589 536 in
adamc@589 537 (str (",_" ^ x ^ ":")
adamc@589 538 :: e
adamc@589 539 :: es,
adamc@589 540 st)
adamc@589 541 end)
adamc@589 542 ([str "}"], st) xes
adamc@589 543 in
adamc@589 544 (strcat (str ("{_" ^ x ^ ":")
adamc@589 545 :: e
adamc@589 546 :: es),
adamc@589 547 st)
adamc@589 548 end
adamc@589 549 | EField (e, x) =>
adamc@589 550 let
adamc@589 551 val (e, st) = jsE inner (e, st)
adamc@589 552 in
adamc@589 553 (strcat [e,
adamc@589 554 str ("._" ^ x)], st)
adamc@589 555 end
adamc@589 556
adamc@591 557 | ECase (e', pes, {result, ...}) =>
adamc@591 558 if closedUpto inner e andalso List.all (fn (_, e) => closedUpto inner e) pes then
adamc@593 559 let
adamc@593 560 val (e', st) = quoteExp result ((ERel 0, loc), st)
adamc@593 561 in
adamc@593 562 ((ELet ("js", result, e, e'), loc),
adamc@593 563 st)
adamc@593 564 end
adamc@591 565 else
adamc@591 566 let
adamc@591 567 val plen = length pes
adamc@589 568
adamc@591 569 val (cases, st) = ListUtil.foldliMap
adamc@591 570 (fn (i, (p, e), st) =>
adamc@591 571 let
adamc@591 572 val (e, st) = jsE (inner + E.patBindsN p) (e, st)
adamc@591 573 val fail =
adamc@591 574 if i = plen - 1 then
adamc@591 575 str "pf()"
adamc@591 576 else
adamc@591 577 str ("c" ^ Int.toString (i+1) ^ "()")
adamc@591 578 val c = jsPat 0 inner p e fail
adamc@591 579 in
adamc@591 580 (strcat [str ("c" ^ Int.toString i ^ "=function(){return "),
adamc@591 581 c,
adamc@591 582 str "},"],
adamc@591 583 st)
adamc@591 584 end)
adamc@591 585 st pes
adamc@589 586
adamc@591 587 val (e, st) = jsE inner (e', st)
adamc@591 588 in
adamc@591 589 (strcat (str "("
adamc@591 590 :: List.revAppend (cases,
adamc@591 591 [str "d0=",
adamc@591 592 e,
adamc@591 593 str ",c0())"])), st)
adamc@591 594 end
adamc@589 595
adamc@589 596 | EStrcat (e1, e2) =>
adamc@589 597 let
adamc@589 598 val (e1, st) = jsE inner (e1, st)
adamc@589 599 val (e2, st) = jsE inner (e2, st)
adamc@589 600 in
adamc@589 601 (strcat [str "(", e1, str "+", e2, str ")"], st)
adamc@589 602 end
adamc@589 603
adamc@589 604 | EError (e, _) =>
adamc@589 605 let
adamc@589 606 val (e, st) = jsE inner (e, st)
adamc@589 607 in
adamc@589 608 (strcat [str "alert(\"ERROR: \"+", e, str ")"],
adamc@589 609 st)
adamc@589 610 end
adamc@589 611
adamc@589 612 | EWrite e =>
adamc@589 613 let
adamc@589 614 val (e, st) = jsE inner (e, st)
adamc@589 615 in
adamc@589 616 (strcat [str "document.write(",
adamc@589 617 e,
adamc@589 618 str ".v)"], st)
adamc@589 619 end
adamc@589 620
adamc@589 621 | ESeq (e1, e2) =>
adamc@589 622 let
adamc@589 623 val (e1, st) = jsE inner (e1, st)
adamc@589 624 val (e2, st) = jsE inner (e2, st)
adamc@589 625 in
adamc@589 626 (strcat [str "(", e1, str ",", e2, str ")"], st)
adamc@589 627 end
adamc@589 628 | ELet (_, _, e1, e2) =>
adamc@589 629 let
adamc@589 630 val (e1, st) = jsE inner (e1, st)
adamc@589 631 val (e2, st) = jsE (inner + 1) (e2, st)
adamc@589 632 in
adamc@589 633 (strcat [str ("(_" ^ Int.toString (len + inner) ^ "="),
adamc@589 634 e1,
adamc@589 635 str ",",
adamc@589 636 e2,
adamc@572 637 str ")"], st)
adamc@572 638 end
adamc@589 639
adamc@591 640 | EJavaScript (_, _, SOME _) => (e, st)
adamc@590 641
adamc@589 642 | EClosure _ => unsupported "EClosure"
adamc@589 643 | EQuery _ => unsupported "Query"
adamc@589 644 | EDml _ => unsupported "DML"
adamc@589 645 | ENextval _ => unsupported "Nextval"
adamc@589 646 | EUnurlify _ => unsupported "EUnurlify"
adamc@590 647 | EJavaScript (_, e, _) => unsupported "Nested JavaScript"
adamc@590 648
adamc@589 649 | ESignalReturn e =>
adamc@572 650 let
adamc@572 651 val (e, st) = jsE inner (e, st)
adamc@572 652 in
adamc@589 653 (strcat [str "sr(",
adamc@589 654 e,
adamc@589 655 str ")"],
adamc@589 656 st)
adamc@589 657 end
adamc@589 658 | ESignalBind (e1, e2) =>
adamc@589 659 let
adamc@589 660 val (e1, st) = jsE inner (e1, st)
adamc@589 661 val (e2, st) = jsE inner (e2, st)
adamc@589 662 in
adamc@589 663 (strcat [str "sb(",
adamc@589 664 e1,
adamc@589 665 str ",",
adamc@589 666 e2,
adamc@589 667 str ")"],
adamc@589 668 st)
adamc@589 669 end
adamc@589 670 | ESignalSource e =>
adamc@589 671 let
adamc@589 672 val (e, st) = jsE inner (e, st)
adamc@589 673 in
adamc@589 674 (strcat [str "ss(",
adamc@589 675 e,
adamc@589 676 str ")"],
adamc@589 677 st)
adamc@572 678 end
adamc@567 679 end
adamc@589 680 in
adamc@589 681 jsE
adamc@589 682 end
adamc@567 683
adamc@589 684 val decl : state -> decl -> decl * state =
adamc@589 685 U.Decl.foldMapB {typ = fn x => x,
adamc@589 686 exp = fn (env, e, st) =>
adamc@589 687 let
adamc@589 688 fun doCode m skip env orig e =
adamc@589 689 let
adamc@589 690 val len = length env
adamc@589 691 fun str s = (EPrim (Prim.String s), #2 e)
adamc@567 692
adamc@589 693 val locals = List.tabulate
adamc@589 694 (varDepth e,
adamc@589 695 fn i => str ("var _" ^ Int.toString (len + i) ^ ";"))
adamc@589 696 val (e, st) = jsExp m skip env 0 (e, st)
adamc@589 697 in
adamc@589 698 (EJavaScript (m, orig, SOME (strcat (#2 e) (locals @ [e]))), st)
adamc@589 699 end
adamc@589 700 in
adamc@589 701 case e of
adamc@591 702 EJavaScript (m, orig as (EAbs (_, t, _, e), _), NONE) =>
adamc@591 703 doCode m 1 (t :: env) orig e
adamc@591 704 | EJavaScript (m, orig, NONE) =>
adamc@591 705 doCode m 0 env orig orig
adamc@589 706 | _ => (e, st)
adamc@589 707 end,
adamc@589 708 decl = fn (_, e, st) => (e, st),
adamc@589 709 bind = fn (env, U.Decl.RelE (_, t)) => t :: env
adamc@589 710 | (env, _) => env}
adamc@589 711 []
adamc@567 712
adamc@567 713 fun doDecl (d, st) =
adamc@567 714 let
adamc@567 715 val (d, st) = decl st d
adamc@567 716 in
adamc@567 717 (List.revAppend (#decls st, [d]),
adamc@567 718 {decls = [],
adamc@589 719 script = #script st,
adamc@589 720 included = #included st})
adamc@567 721 end
adamc@567 722
adamc@567 723 val (ds, st) = ListUtil.foldlMapConcat doDecl
adamc@567 724 {decls = [],
adamc@589 725 script = [],
adamc@589 726 included = IS.empty}
adamc@567 727 file
adamc@569 728
adamc@569 729 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
adamc@569 730 fun lines acc =
adamc@569 731 case TextIO.inputLine inf of
adamc@569 732 NONE => String.concat (rev acc)
adamc@569 733 | SOME line => lines (line :: acc)
adamc@569 734 val lines = lines []
adamc@567 735 in
adamc@569 736 TextIO.closeIn inf;
adamc@589 737 (DJavaScript (lines ^ String.concat (rev (#script st))), ErrorMsg.dummySpan) :: ds
adamc@567 738 end
adamc@567 739
adamc@567 740 end