annotate src/jscomp.sml @ 591:8f8771f32909

Injecting a float
author Adam Chlipala <adamc@hcoop.net>
date Thu, 01 Jan 2009 15:59:02 -0500
parents 57f476c934da
children a8be5a2068a5
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@590 159 fun quoteExp loc (t : typ) e =
adamc@590 160 case #1 t of
adamc@590 161 TSource => strcat loc [str loc "s",
adamc@590 162 (EFfiApp ("Basis", "htmlifyInt", [e]), loc)]
adamc@590 163 | TRecord [] => str loc "null"
adamc@590 164
adamc@590 165 | TFfi ("Basis", "string") => e
adamc@590 166 | TFfi ("Basis", "int") => (EFfiApp ("Basis", "htmlifyInt", [e]), loc)
adamc@591 167 | TFfi ("Basis", "float") => (EFfiApp ("Basis", "htmlifyFloat", [e]), loc)
adamc@590 168
adamc@590 169 | _ => (EM.errorAt loc "Don't know how to embed type in JavaScript";
adamc@590 170 Print.prefaces "Can't embed" [("t", MonoPrint.p_typ MonoEnv.empty t)];
adamc@590 171 str loc "ERROR")
adamc@590 172
adamc@589 173 fun jsExp mode skip outer =
adamc@567 174 let
adamc@589 175 val len = length outer
adamc@567 176
adamc@589 177 fun jsE inner (e as (_, loc), st) =
adamc@589 178 let
adamc@590 179 val str = str loc
adamc@567 180
adamc@589 181 fun var n = Int.toString (len + inner - n - 1)
adamc@567 182
adamc@589 183 fun patCon pc =
adamc@589 184 case pc of
adamc@589 185 PConVar n => str (Int.toString n)
adamc@589 186 | PConFfi {mod = "Basis", con = "True", ...} => str "true"
adamc@589 187 | PConFfi {mod = "Basis", con = "False", ...} => str "false"
adamc@589 188 | PConFfi {con, ...} => str ("\"_" ^ con ^ "\"")
adamc@567 189
adamc@589 190 fun isNullable (t, _) =
adamc@589 191 case t of
adamc@589 192 TOption _ => true
adamc@589 193 | TRecord [] => true
adamc@589 194 | _ => false
adamc@568 195
adamc@591 196 fun unsupported s =
adamc@591 197 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript[2]");
adamc@591 198 (str "ERROR", st))
adamc@577 199
adamc@589 200 val strcat = strcat loc
adamc@584 201
adamc@589 202 fun jsPrim p =
adamc@589 203 case p of
adamc@589 204 Prim.String s =>
adamc@589 205 str ("\""
adamc@589 206 ^ String.translate (fn #"'" =>
adamc@589 207 if mode = Attribute then
adamc@589 208 "\\047"
adamc@589 209 else
adamc@589 210 "'"
adamc@589 211 | #"\"" => "\\\""
adamc@589 212 | #"<" =>
adamc@589 213 if mode = Script then
adamc@589 214 "<"
adamc@589 215 else
adamc@589 216 "\\074"
adamc@589 217 | #"\\" => "\\\\"
adamc@589 218 | ch => String.str ch) s
adamc@589 219 ^ "\"")
adamc@589 220 | _ => str (Prim.toString p)
adamc@589 221
adamc@589 222 fun jsPat depth inner (p, _) succ fail =
adamc@589 223 case p of
adamc@589 224 PWild => succ
adamc@589 225 | PVar _ => strcat [str ("(_" ^ Int.toString (len + inner) ^ "=d"
adamc@589 226 ^ Int.toString depth ^ ","),
adamc@589 227 succ,
adamc@589 228 str ")"]
adamc@589 229 | PPrim p => strcat [str ("(d" ^ Int.toString depth ^ "=="),
adamc@589 230 jsPrim p,
adamc@589 231 str "?",
adamc@589 232 succ,
adamc@589 233 str ":",
adamc@589 234 fail,
adamc@589 235 str ")"]
adamc@589 236 | PCon (_, PConFfi {mod = "Basis", con = "True", ...}, NONE) =>
adamc@589 237 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 238 succ,
adamc@589 239 str ":",
adamc@589 240 fail,
adamc@589 241 str ")"]
adamc@589 242 | PCon (_, PConFfi {mod = "Basis", con = "False", ...}, NONE) =>
adamc@589 243 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 244 fail,
adamc@589 245 str ":",
adamc@589 246 succ,
adamc@589 247 str ")"]
adamc@589 248 | PCon (_, pc, NONE) =>
adamc@589 249 strcat [str ("(d" ^ Int.toString depth ^ "=="),
adamc@589 250 patCon pc,
adamc@589 251 str "?",
adamc@589 252 succ,
adamc@589 253 str ":",
adamc@589 254 fail,
adamc@589 255 str ")"]
adamc@589 256 | PCon (_, pc, SOME p) =>
adamc@589 257 strcat [str ("(d" ^ Int.toString depth ^ ".n=="),
adamc@589 258 patCon pc,
adamc@589 259 str ("?(d" ^ Int.toString depth ^ "=d" ^ Int.toString depth ^ ".v,"),
adamc@589 260 jsPat depth inner p succ fail,
adamc@589 261 str "):",
adamc@589 262 fail,
adamc@589 263 str ")"]
adamc@589 264 | PRecord xps =>
adamc@589 265 let
adamc@589 266 val (_, succ) = foldl
adamc@589 267 (fn ((x, p, _), (inner, succ)) =>
adamc@589 268 (inner + E.patBindsN p,
adamc@589 269 strcat [str ("(d" ^ Int.toString (depth+1) ^ "=d"
adamc@589 270 ^ Int.toString depth ^ "._" ^ x ^ ","),
adamc@589 271 jsPat (depth+1) inner p succ fail,
adamc@589 272 str ")"]))
adamc@589 273 (inner, succ) xps
adamc@589 274 in
adamc@589 275 succ
adamc@589 276 end
adamc@589 277 | PNone _ => strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 278 fail,
adamc@589 279 str ":",
adamc@589 280 succ,
adamc@589 281 str ")"]
adamc@589 282 | PSome (_, p) => strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 283 jsPat depth inner p succ fail,
adamc@589 284 str ":",
adamc@589 285 fail,
adamc@589 286 str ")"]
adamc@589 287
adamc@589 288 fun deStrcat (e, _) =
adamc@589 289 case e of
adamc@589 290 EPrim (Prim.String s) => s
adamc@589 291 | EStrcat (e1, e2) => deStrcat e1 ^ deStrcat e2
adamc@589 292 | _ => raise Fail "Jscomp: deStrcat"
adamc@590 293
adamc@590 294 val quoteExp = quoteExp loc
adamc@567 295 in
adamc@590 296 (*Print.prefaces "jsE" [("e", MonoPrint.p_exp MonoEnv.empty e)];*)
adamc@590 297
adamc@589 298 case #1 e of
adamc@589 299 EPrim p => (jsPrim p, st)
adamc@589 300 | ERel n =>
adamc@589 301 if n < inner then
adamc@589 302 (str ("_" ^ var n), st)
adamc@589 303 else
adamc@589 304 let
adamc@589 305 val n = n - inner
adamc@589 306 in
adamc@589 307 (quoteExp (List.nth (outer, n)) (ERel (n - skip), loc), st)
adamc@589 308 end
adamc@567 309
adamc@589 310 | ENamed n =>
adamc@589 311 let
adamc@589 312 val st =
adamc@589 313 if IS.member (#included st, n) then
adamc@589 314 st
adamc@589 315 else
adamc@589 316 case IM.find (nameds, n) of
adamc@589 317 NONE => raise Fail "Jscomp: Unbound ENamed"
adamc@589 318 | SOME e =>
adamc@589 319 let
adamc@589 320 val st = {decls = #decls st,
adamc@589 321 script = #script st,
adamc@589 322 included = IS.add (#included st, n)}
adamc@578 323
adamc@589 324 val (e, st) = jsExp mode skip [] 0 (e, st)
adamc@589 325 val e = deStrcat e
adamc@589 326
adamc@589 327 val sc = "_n" ^ Int.toString n ^ "=" ^ e ^ ";\n"
adamc@589 328 in
adamc@589 329 {decls = #decls st,
adamc@589 330 script = sc :: #script st,
adamc@589 331 included = #included st}
adamc@589 332 end
adamc@589 333 in
adamc@589 334 (str ("_n" ^ Int.toString n), st)
adamc@589 335 end
adamc@589 336
adamc@589 337 | ECon (_, pc, NONE) => (patCon pc, st)
adamc@589 338 | ECon (_, pc, SOME e) =>
adamc@589 339 let
adamc@589 340 val (s, st) = jsE inner (e, st)
adamc@589 341 in
adamc@589 342 (strcat [str "{n:",
adamc@589 343 patCon pc,
adamc@589 344 str ",v:",
adamc@589 345 s,
adamc@589 346 str "}"], st)
adamc@589 347 end
adamc@589 348 | ENone _ => (str "null", st)
adamc@589 349 | ESome (t, e) =>
adamc@572 350 let
adamc@572 351 val (e, st) = jsE inner (e, st)
adamc@572 352 in
adamc@589 353 (if isNullable t then
adamc@589 354 strcat [str "{v:", e, str "}"]
adamc@589 355 else
adamc@589 356 e, st)
adamc@589 357 end
adamc@589 358
adamc@589 359 | EFfi k =>
adamc@589 360 let
adamc@589 361 val name = case ffi k of
adamc@589 362 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k
adamc@589 363 ^ " in JavaScript");
adamc@589 364 "ERROR")
adamc@589 365 | SOME s => s
adamc@589 366 in
adamc@589 367 (str name, st)
adamc@589 368 end
adamc@589 369 | EFfiApp (m, x, args) =>
adamc@589 370 let
adamc@589 371 val args =
adamc@589 372 case (m, x, args) of
adamc@589 373 ("Basis", "new_client_source", [(EJavaScript (_, e, _), _)]) => [e]
adamc@589 374 | ("Basis", "set_client_source", [e1, (EJavaScript (_, e2, _), _)]) => [e1, e2]
adamc@589 375 | _ => args
adamc@589 376
adamc@589 377 val name = case ffi (m, x) of
adamc@589 378 NONE => (EM.errorAt loc ("Unsupported FFI function "
adamc@589 379 ^ x ^ " in JavaScript");
adamc@589 380 "ERROR")
adamc@589 381 | SOME s => s
adamc@589 382 in
adamc@589 383 case args of
adamc@589 384 [] => (str (name ^ "()"), st)
adamc@589 385 | [e] =>
adamc@589 386 let
adamc@589 387 val (e, st) = jsE inner (e, st)
adamc@589 388 in
adamc@589 389 (strcat [str (name ^ "("),
adamc@589 390 e,
adamc@589 391 str ")"], st)
adamc@589 392 end
adamc@589 393 | e :: es =>
adamc@589 394 let
adamc@589 395 val (e, st) = jsE inner (e, st)
adamc@589 396 val (es, st) = ListUtil.foldlMapConcat
adamc@589 397 (fn (e, st) =>
adamc@589 398 let
adamc@589 399 val (e, st) = jsE inner (e, st)
adamc@589 400 in
adamc@589 401 ([str ",", e], st)
adamc@589 402 end)
adamc@589 403 st es
adamc@589 404 in
adamc@589 405 (strcat (str (name ^ "(")
adamc@589 406 :: e
adamc@589 407 :: es
adamc@589 408 @ [str ")"]), st)
adamc@589 409 end
adamc@589 410 end
adamc@589 411
adamc@589 412 | EApp (e1, e2) =>
adamc@589 413 let
adamc@589 414 val (e1, st) = jsE inner (e1, st)
adamc@589 415 val (e2, st) = jsE inner (e2, st)
adamc@589 416 in
adamc@589 417 (strcat [e1, str "(", e2, str ")"], st)
adamc@589 418 end
adamc@589 419 | EAbs (_, _, _, e) =>
adamc@589 420 let
adamc@589 421 val locals = List.tabulate
adamc@589 422 (varDepth e,
adamc@589 423 fn i => str ("var _" ^ Int.toString (len + inner + i + 1) ^ ";"))
adamc@589 424 val (e, st) = jsE (inner + 1) (e, st)
adamc@589 425 in
adamc@589 426 (strcat (str ("function(_"
adamc@589 427 ^ Int.toString (len + inner)
adamc@589 428 ^ "){")
adamc@589 429 :: locals
adamc@589 430 @ [str "return ",
adamc@589 431 e,
adamc@589 432 str "}"]),
adamc@589 433 st)
adamc@589 434 end
adamc@589 435
adamc@589 436 | EUnop (s, e) =>
adamc@589 437 let
adamc@589 438 val (e, st) = jsE inner (e, st)
adamc@589 439 in
adamc@589 440 (strcat [str ("(" ^ s),
adamc@572 441 e,
adamc@589 442 str ")"],
adamc@589 443 st)
adamc@589 444 end
adamc@589 445 | EBinop (s, e1, e2) =>
adamc@589 446 let
adamc@589 447 val (e1, st) = jsE inner (e1, st)
adamc@589 448 val (e2, st) = jsE inner (e2, st)
adamc@589 449 in
adamc@589 450 (strcat [str "(",
adamc@589 451 e1,
adamc@589 452 str s,
adamc@589 453 e2,
adamc@589 454 str ")"],
adamc@589 455 st)
adamc@589 456 end
adamc@589 457
adamc@589 458 | ERecord [] => (str "null", st)
adamc@589 459 | ERecord [(x, e, _)] =>
adamc@589 460 let
adamc@589 461 val (e, st) = jsE inner (e, st)
adamc@589 462 in
adamc@589 463 (strcat [str "{_x:", e, str "}"], st)
adamc@589 464 end
adamc@589 465 | ERecord ((x, e, _) :: xes) =>
adamc@589 466 let
adamc@589 467 val (e, st) = jsE inner (e, st)
adamc@589 468
adamc@589 469 val (es, st) =
adamc@589 470 foldr (fn ((x, e, _), (es, st)) =>
adamc@589 471 let
adamc@589 472 val (e, st) = jsE inner (e, st)
adamc@589 473 in
adamc@589 474 (str (",_" ^ x ^ ":")
adamc@589 475 :: e
adamc@589 476 :: es,
adamc@589 477 st)
adamc@589 478 end)
adamc@589 479 ([str "}"], st) xes
adamc@589 480 in
adamc@589 481 (strcat (str ("{_" ^ x ^ ":")
adamc@589 482 :: e
adamc@589 483 :: es),
adamc@589 484 st)
adamc@589 485 end
adamc@589 486 | EField (e, x) =>
adamc@589 487 let
adamc@589 488 val (e, st) = jsE inner (e, st)
adamc@589 489 in
adamc@589 490 (strcat [e,
adamc@589 491 str ("._" ^ x)], st)
adamc@589 492 end
adamc@589 493
adamc@591 494 | ECase (e', pes, {result, ...}) =>
adamc@591 495 if closedUpto inner e andalso List.all (fn (_, e) => closedUpto inner e) pes then
adamc@591 496 ((ELet ("js", result, e, quoteExp result (ERel 0, loc)), loc),
adamc@591 497 st)
adamc@591 498 else
adamc@591 499 let
adamc@591 500 val plen = length pes
adamc@589 501
adamc@591 502 val (cases, st) = ListUtil.foldliMap
adamc@591 503 (fn (i, (p, e), st) =>
adamc@591 504 let
adamc@591 505 val (e, st) = jsE (inner + E.patBindsN p) (e, st)
adamc@591 506 val fail =
adamc@591 507 if i = plen - 1 then
adamc@591 508 str "pf()"
adamc@591 509 else
adamc@591 510 str ("c" ^ Int.toString (i+1) ^ "()")
adamc@591 511 val c = jsPat 0 inner p e fail
adamc@591 512 in
adamc@591 513 (strcat [str ("c" ^ Int.toString i ^ "=function(){return "),
adamc@591 514 c,
adamc@591 515 str "},"],
adamc@591 516 st)
adamc@591 517 end)
adamc@591 518 st pes
adamc@589 519
adamc@591 520 val (e, st) = jsE inner (e', st)
adamc@591 521 in
adamc@591 522 (strcat (str "("
adamc@591 523 :: List.revAppend (cases,
adamc@591 524 [str "d0=",
adamc@591 525 e,
adamc@591 526 str ",c0())"])), st)
adamc@591 527 end
adamc@589 528
adamc@589 529 | EStrcat (e1, e2) =>
adamc@589 530 let
adamc@589 531 val (e1, st) = jsE inner (e1, st)
adamc@589 532 val (e2, st) = jsE inner (e2, st)
adamc@589 533 in
adamc@589 534 (strcat [str "(", e1, str "+", e2, str ")"], st)
adamc@589 535 end
adamc@589 536
adamc@589 537 | EError (e, _) =>
adamc@589 538 let
adamc@589 539 val (e, st) = jsE inner (e, st)
adamc@589 540 in
adamc@589 541 (strcat [str "alert(\"ERROR: \"+", e, str ")"],
adamc@589 542 st)
adamc@589 543 end
adamc@589 544
adamc@589 545 | EWrite e =>
adamc@589 546 let
adamc@589 547 val (e, st) = jsE inner (e, st)
adamc@589 548 in
adamc@589 549 (strcat [str "document.write(",
adamc@589 550 e,
adamc@589 551 str ".v)"], st)
adamc@589 552 end
adamc@589 553
adamc@589 554 | ESeq (e1, e2) =>
adamc@589 555 let
adamc@589 556 val (e1, st) = jsE inner (e1, st)
adamc@589 557 val (e2, st) = jsE inner (e2, st)
adamc@589 558 in
adamc@589 559 (strcat [str "(", e1, str ",", e2, str ")"], st)
adamc@589 560 end
adamc@589 561 | ELet (_, _, e1, e2) =>
adamc@589 562 let
adamc@589 563 val (e1, st) = jsE inner (e1, st)
adamc@589 564 val (e2, st) = jsE (inner + 1) (e2, st)
adamc@589 565 in
adamc@589 566 (strcat [str ("(_" ^ Int.toString (len + inner) ^ "="),
adamc@589 567 e1,
adamc@589 568 str ",",
adamc@589 569 e2,
adamc@572 570 str ")"], st)
adamc@572 571 end
adamc@589 572
adamc@591 573 | EJavaScript (_, _, SOME _) => (e, st)
adamc@590 574
adamc@589 575 | EClosure _ => unsupported "EClosure"
adamc@589 576 | EQuery _ => unsupported "Query"
adamc@589 577 | EDml _ => unsupported "DML"
adamc@589 578 | ENextval _ => unsupported "Nextval"
adamc@589 579 | EUnurlify _ => unsupported "EUnurlify"
adamc@590 580 | EJavaScript (_, e, _) => unsupported "Nested JavaScript"
adamc@590 581
adamc@589 582 | ESignalReturn e =>
adamc@572 583 let
adamc@572 584 val (e, st) = jsE inner (e, st)
adamc@572 585 in
adamc@589 586 (strcat [str "sr(",
adamc@589 587 e,
adamc@589 588 str ")"],
adamc@589 589 st)
adamc@589 590 end
adamc@589 591 | ESignalBind (e1, e2) =>
adamc@589 592 let
adamc@589 593 val (e1, st) = jsE inner (e1, st)
adamc@589 594 val (e2, st) = jsE inner (e2, st)
adamc@589 595 in
adamc@589 596 (strcat [str "sb(",
adamc@589 597 e1,
adamc@589 598 str ",",
adamc@589 599 e2,
adamc@589 600 str ")"],
adamc@589 601 st)
adamc@589 602 end
adamc@589 603 | ESignalSource e =>
adamc@589 604 let
adamc@589 605 val (e, st) = jsE inner (e, st)
adamc@589 606 in
adamc@589 607 (strcat [str "ss(",
adamc@589 608 e,
adamc@589 609 str ")"],
adamc@589 610 st)
adamc@572 611 end
adamc@567 612 end
adamc@589 613 in
adamc@589 614 jsE
adamc@589 615 end
adamc@567 616
adamc@589 617 val decl : state -> decl -> decl * state =
adamc@589 618 U.Decl.foldMapB {typ = fn x => x,
adamc@589 619 exp = fn (env, e, st) =>
adamc@589 620 let
adamc@589 621 fun doCode m skip env orig e =
adamc@589 622 let
adamc@589 623 val len = length env
adamc@589 624 fun str s = (EPrim (Prim.String s), #2 e)
adamc@567 625
adamc@589 626 val locals = List.tabulate
adamc@589 627 (varDepth e,
adamc@589 628 fn i => str ("var _" ^ Int.toString (len + i) ^ ";"))
adamc@589 629 val (e, st) = jsExp m skip env 0 (e, st)
adamc@589 630 in
adamc@589 631 (EJavaScript (m, orig, SOME (strcat (#2 e) (locals @ [e]))), st)
adamc@589 632 end
adamc@589 633 in
adamc@589 634 case e of
adamc@591 635 EJavaScript (m, orig as (EAbs (_, t, _, e), _), NONE) =>
adamc@591 636 doCode m 1 (t :: env) orig e
adamc@591 637 | EJavaScript (m, orig, NONE) =>
adamc@591 638 doCode m 0 env orig orig
adamc@589 639 | _ => (e, st)
adamc@589 640 end,
adamc@589 641 decl = fn (_, e, st) => (e, st),
adamc@589 642 bind = fn (env, U.Decl.RelE (_, t)) => t :: env
adamc@589 643 | (env, _) => env}
adamc@589 644 []
adamc@567 645
adamc@567 646 fun doDecl (d, st) =
adamc@567 647 let
adamc@567 648 val (d, st) = decl st d
adamc@567 649 in
adamc@567 650 (List.revAppend (#decls st, [d]),
adamc@567 651 {decls = [],
adamc@589 652 script = #script st,
adamc@589 653 included = #included st})
adamc@567 654 end
adamc@567 655
adamc@567 656 val (ds, st) = ListUtil.foldlMapConcat doDecl
adamc@567 657 {decls = [],
adamc@589 658 script = [],
adamc@589 659 included = IS.empty}
adamc@567 660 file
adamc@569 661
adamc@569 662 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
adamc@569 663 fun lines acc =
adamc@569 664 case TextIO.inputLine inf of
adamc@569 665 NONE => String.concat (rev acc)
adamc@569 666 | SOME line => lines (line :: acc)
adamc@569 667 val lines = lines []
adamc@567 668 in
adamc@569 669 TextIO.closeIn inf;
adamc@589 670 (DJavaScript (lines ^ String.concat (rev (#script st))), ErrorMsg.dummySpan) :: ds
adamc@567 671 end
adamc@567 672
adamc@567 673 end