annotate src/jscomp.sml @ 601:7c3c21eb5b4c

Initial experiments with nested <dyn>
author Adam Chlipala <adamc@hcoop.net>
date Tue, 13 Jan 2009 15:17:11 -0500
parents d49d58a69877
children b1064de2b1f9
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@601 40 (("Basis", "get_client_source"), "sg"),
adamc@586 41 (("Basis", "htmlifyBool"), "bs"),
adamc@583 42 (("Basis", "htmlifyFloat"), "ts"),
adamc@582 43 (("Basis", "htmlifyInt"), "ts"),
adamc@597 44 (("Basis", "htmlifyString"), "eh"),
adamc@579 45 (("Basis", "new_client_source"), "sc"),
adamc@579 46 (("Basis", "set_client_source"), "sv")]
adamc@572 47
adamc@572 48 structure FM = BinaryMapFn(struct
adamc@572 49 type ord_key = string * string
adamc@572 50 fun compare ((m1, x1), (m2, x2)) =
adamc@572 51 Order.join (String.compare (m1, m2),
adamc@572 52 fn () => String.compare (x1, x2))
adamc@572 53 end)
adamc@572 54
adamc@572 55 val funcs = foldl (fn ((k, v), m) => FM.insert (m, k, v)) FM.empty funcs
adamc@572 56
adamc@572 57 fun ffi k = FM.find (funcs, k)
adamc@572 58
adamc@567 59 type state = {
adamc@567 60 decls : decl list,
adamc@589 61 script : string list,
adamc@595 62 included : IS.set,
adamc@595 63 injectors : int IM.map,
adamc@595 64 maxName : int
adamc@567 65 }
adamc@567 66
adamc@567 67 fun varDepth (e, _) =
adamc@567 68 case e of
adamc@567 69 EPrim _ => 0
adamc@567 70 | ERel _ => 0
adamc@567 71 | ENamed _ => 0
adamc@567 72 | ECon (_, _, NONE) => 0
adamc@567 73 | ECon (_, _, SOME e) => varDepth e
adamc@567 74 | ENone _ => 0
adamc@567 75 | ESome (_, e) => varDepth e
adamc@567 76 | EFfi _ => 0
adamc@567 77 | EFfiApp (_, _, es) => foldl Int.max 0 (map varDepth es)
adamc@567 78 | EApp (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 79 | EAbs _ => 0
adamc@567 80 | EUnop (_, e) => varDepth e
adamc@567 81 | EBinop (_, e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 82 | ERecord xes => foldl Int.max 0 (map (fn (_, e, _) => varDepth e) xes)
adamc@567 83 | EField (e, _) => varDepth e
adamc@567 84 | ECase (e, pes, _) =>
adamc@567 85 foldl Int.max (varDepth e)
adamc@567 86 (map (fn (p, e) => E.patBindsN p + varDepth e) pes)
adamc@567 87 | EStrcat (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 88 | EError (e, _) => varDepth e
adamc@567 89 | EWrite e => varDepth e
adamc@567 90 | ESeq (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 91 | ELet (_, _, e1, e2) => Int.max (varDepth e1, 1 + varDepth e2)
adamc@567 92 | EClosure _ => 0
adamc@567 93 | EQuery _ => 0
adamc@567 94 | EDml _ => 0
adamc@567 95 | ENextval _ => 0
adamc@567 96 | EUnurlify _ => 0
adamc@567 97 | EJavaScript _ => 0
adamc@568 98 | ESignalReturn e => varDepth e
adamc@572 99 | ESignalBind (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@574 100 | ESignalSource e => varDepth e
adamc@567 101
adamc@591 102 fun closedUpto d =
adamc@591 103 let
adamc@591 104 fun cu inner (e, _) =
adamc@591 105 case e of
adamc@591 106 EPrim _ => true
adamc@591 107 | ERel n => n < inner orelse n - inner >= d
adamc@591 108 | ENamed _ => true
adamc@591 109 | ECon (_, _, NONE) => true
adamc@591 110 | ECon (_, _, SOME e) => cu inner e
adamc@591 111 | ENone _ => true
adamc@591 112 | ESome (_, e) => cu inner e
adamc@591 113 | EFfi _ => true
adamc@591 114 | EFfiApp (_, _, es) => List.all (cu inner) es
adamc@591 115 | EApp (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 116 | EAbs (_, _, _, e) => cu (inner + 1) e
adamc@591 117 | EUnop (_, e) => cu inner e
adamc@591 118 | EBinop (_, e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 119 | ERecord xes => List.all (fn (_, e, _) => cu inner e) xes
adamc@591 120 | EField (e, _) => cu inner e
adamc@591 121 | ECase (e, pes, _) =>
adamc@591 122 cu inner e
adamc@591 123 andalso List.all (fn (p, e) => cu (inner + E.patBindsN p) e) pes
adamc@591 124 | EStrcat (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 125 | EError (e, _) => cu inner e
adamc@591 126 | EWrite e => cu inner e
adamc@591 127 | ESeq (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 128 | ELet (_, _, e1, e2) => cu inner e1 andalso cu (inner + 1) e2
adamc@591 129 | EClosure (_, es) => List.all (cu inner) es
adamc@591 130 | EQuery {query, body, initial, ...} =>
adamc@591 131 cu inner query
adamc@591 132 andalso cu (inner + 2) body
adamc@591 133 andalso cu inner initial
adamc@591 134 | EDml e => cu inner e
adamc@591 135 | ENextval e => cu inner e
adamc@591 136 | EUnurlify (e, _) => cu inner e
adamc@591 137 | EJavaScript (_, e, _) => cu inner e
adamc@591 138 | ESignalReturn e => cu inner e
adamc@591 139 | ESignalBind (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 140 | ESignalSource e => cu inner e
adamc@591 141 in
adamc@591 142 cu 0
adamc@591 143 end
adamc@591 144
adamc@568 145 fun strcat loc es =
adamc@568 146 case es of
adamc@568 147 [] => (EPrim (Prim.String ""), loc)
adamc@568 148 | [x] => x
adamc@568 149 | x :: es' => (EStrcat (x, strcat loc es'), loc)
adamc@568 150
adamc@589 151 fun process file =
adamc@567 152 let
adamc@596 153 val (someTs, nameds) =
adamc@596 154 foldl (fn ((DVal (_, n, t, e, _), _), (someTs, nameds)) => (someTs, IM.insert (nameds, n, e))
adamc@596 155 | ((DValRec vis, _), (someTs, nameds)) =>
adamc@596 156 (someTs, foldl (fn ((_, n, _, e, _), nameds) => IM.insert (nameds, n, e))
adamc@596 157 nameds vis)
adamc@596 158 | ((DDatatype (_, _, cs), _), state as (someTs, nameds)) =>
adamc@596 159 if ElabUtil.classifyDatatype cs = Option then
adamc@596 160 (foldl (fn ((_, n, SOME t), someTs) => IM.insert (someTs, n, t)
adamc@596 161 | (_, someTs) => someTs) someTs cs,
adamc@596 162 nameds)
adamc@596 163 else
adamc@596 164 state
adamc@595 165 | (_, state) => state)
adamc@596 166 (IM.empty, IM.empty) file
adamc@567 167
adamc@590 168 fun str loc s = (EPrim (Prim.String s), loc)
adamc@590 169
adamc@594 170 fun isNullable (t, _) =
adamc@594 171 case t of
adamc@594 172 TOption _ => true
adamc@594 173 | TRecord [] => true
adamc@594 174 | _ => false
adamc@594 175
adamc@593 176 fun quoteExp loc (t : typ) (e, st) =
adamc@590 177 case #1 t of
adamc@593 178 TSource => (strcat loc [str loc "s",
adamc@593 179 (EFfiApp ("Basis", "htmlifyInt", [e]), loc)], st)
adamc@590 180
adamc@593 181 | TRecord [] => (str loc "null", st)
adamc@593 182 | TRecord [(x, t)] =>
adamc@593 183 let
adamc@593 184 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 185 in
adamc@593 186 (strcat loc [str loc ("{_" ^ x ^ ":"),
adamc@593 187 e,
adamc@593 188 str loc "}"], st)
adamc@593 189 end
adamc@593 190 | TRecord ((x, t) :: xts) =>
adamc@593 191 let
adamc@593 192 val (e', st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 193 val (es, st) = ListUtil.foldlMap
adamc@593 194 (fn ((x, t), st) =>
adamc@593 195 let
adamc@593 196 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 197 in
adamc@593 198 (strcat loc [str loc (",_" ^ x ^ ":"), e], st)
adamc@593 199 end)
adamc@593 200 st xts
adamc@593 201 in
adamc@593 202 (strcat loc (str loc ("{_" ^ x ^ ":")
adamc@593 203 :: e'
adamc@593 204 :: es
adamc@593 205 @ [str loc "}"]), st)
adamc@593 206 end
adamc@590 207
adamc@593 208 | TFfi ("Basis", "string") => ((EFfiApp ("Basis", "jsifyString", [e]), loc), st)
adamc@593 209 | TFfi ("Basis", "int") => ((EFfiApp ("Basis", "htmlifyInt", [e]), loc), st)
adamc@593 210 | TFfi ("Basis", "float") => ((EFfiApp ("Basis", "htmlifyFloat", [e]), loc), st)
adamc@593 211
adamc@593 212 | TFfi ("Basis", "bool") => ((ECase (e,
adamc@593 213 [((PCon (Enum, PConFfi {mod = "Basis",
adamc@593 214 datatyp = "bool",
adamc@593 215 con = "True",
adamc@593 216 arg = NONE}, NONE), loc),
adamc@593 217 str loc "true"),
adamc@593 218 ((PCon (Enum, PConFfi {mod = "Basis",
adamc@593 219 datatyp = "bool",
adamc@593 220 con = "False",
adamc@593 221 arg = NONE}, NONE), loc),
adamc@593 222 str loc "false")],
adamc@593 223 {disc = (TFfi ("Basis", "bool"), loc),
adamc@593 224 result = (TFfi ("Basis", "string"), loc)}), loc),
adamc@593 225 st)
adamc@592 226
adamc@594 227 | TOption t =>
adamc@594 228 let
adamc@594 229 val (e', st) = quoteExp loc t ((ERel 0, loc), st)
adamc@594 230 in
adamc@594 231 ((ECase (e,
adamc@594 232 [((PNone t, loc),
adamc@594 233 str loc "null"),
adamc@594 234 ((PSome (t, (PVar ("x", t), loc)), loc),
adamc@594 235 if isNullable t then
adamc@594 236 strcat loc [str loc "{v:", e', str loc "}"]
adamc@594 237 else
adamc@594 238 e')],
adamc@594 239 {disc = (TOption t, loc),
adamc@594 240 result = (TFfi ("Basis", "string"), loc)}), loc),
adamc@594 241 st)
adamc@594 242 end
adamc@594 243
adamc@595 244 | TDatatype (n, ref (dk, cs)) =>
adamc@595 245 (case IM.find (#injectors st, n) of
adamc@595 246 SOME n' => ((EApp ((ENamed n', loc), e), loc), st)
adamc@595 247 | NONE =>
adamc@595 248 let
adamc@595 249 val dk = ElabUtil.classifyDatatype cs
adamc@595 250
adamc@595 251 val n' = #maxName st
adamc@595 252 val st = {decls = #decls st,
adamc@595 253 script = #script st,
adamc@595 254 included = #included st,
adamc@595 255 injectors = IM.insert (#injectors st, n, n'),
adamc@595 256 maxName = n' + 1}
adamc@595 257
adamc@595 258 val (pes, st) = ListUtil.foldlMap
adamc@595 259 (fn ((_, cn, NONE), st) =>
adamc@595 260 (((PCon (dk, PConVar cn, NONE), loc),
adamc@596 261 case dk of
adamc@596 262 Option => str loc "null"
adamc@596 263 | _ => str loc (Int.toString cn)),
adamc@595 264 st)
adamc@595 265 | ((_, cn, SOME t), st) =>
adamc@595 266 let
adamc@595 267 val (e, st) = quoteExp loc t ((ERel 0, loc), st)
adamc@595 268 in
adamc@595 269 (((PCon (dk, PConVar cn, SOME (PVar ("x", t), loc)), loc),
adamc@596 270 case dk of
adamc@596 271 Option =>
adamc@596 272 if isNullable t then
adamc@596 273 strcat loc [str loc "{_v:",
adamc@596 274 e,
adamc@596 275 str loc "}"]
adamc@596 276 else
adamc@596 277 e
adamc@597 278 | _ => strcat loc [str loc ("{n:" ^ Int.toString cn
adamc@597 279 ^ ",v:"),
adamc@597 280 e,
adamc@597 281 str loc "}"]),
adamc@595 282 st)
adamc@595 283 end)
adamc@595 284 st cs
adamc@595 285
adamc@595 286 val s = (TFfi ("Basis", "string"), loc)
adamc@595 287 val body = (ECase ((ERel 0, loc), pes,
adamc@595 288 {disc = t, result = s}), loc)
adamc@595 289 val body = (EAbs ("x", t, s, body), loc)
adamc@595 290
adamc@595 291 val st = {decls = (DValRec [("jsify", n', (TFun (t, s), loc),
adamc@595 292 body, "jsify")], loc) :: #decls st,
adamc@595 293 script = #script st,
adamc@595 294 included = #included st,
adamc@595 295 injectors = #injectors st,
adamc@595 296 maxName = #maxName st}
adamc@595 297 in
adamc@595 298 ((EApp ((ENamed n', loc), e), loc), st)
adamc@595 299 end)
adamc@595 300
adamc@590 301 | _ => (EM.errorAt loc "Don't know how to embed type in JavaScript";
adamc@590 302 Print.prefaces "Can't embed" [("t", MonoPrint.p_typ MonoEnv.empty t)];
adamc@593 303 (str loc "ERROR", st))
adamc@590 304
adamc@589 305 fun jsExp mode skip outer =
adamc@567 306 let
adamc@589 307 val len = length outer
adamc@567 308
adamc@589 309 fun jsE inner (e as (_, loc), st) =
adamc@589 310 let
adamc@590 311 val str = str loc
adamc@567 312
adamc@589 313 fun var n = Int.toString (len + inner - n - 1)
adamc@567 314
adamc@589 315 fun patCon pc =
adamc@589 316 case pc of
adamc@589 317 PConVar n => str (Int.toString n)
adamc@589 318 | PConFfi {mod = "Basis", con = "True", ...} => str "true"
adamc@589 319 | PConFfi {mod = "Basis", con = "False", ...} => str "false"
adamc@589 320 | PConFfi {con, ...} => str ("\"_" ^ con ^ "\"")
adamc@567 321
adamc@591 322 fun unsupported s =
adamc@591 323 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript[2]");
adamc@591 324 (str "ERROR", st))
adamc@577 325
adamc@589 326 val strcat = strcat loc
adamc@584 327
adamc@589 328 fun jsPrim p =
adamc@589 329 case p of
adamc@589 330 Prim.String s =>
adamc@589 331 str ("\""
adamc@589 332 ^ String.translate (fn #"'" =>
adamc@589 333 if mode = Attribute then
adamc@589 334 "\\047"
adamc@589 335 else
adamc@589 336 "'"
adamc@589 337 | #"\"" => "\\\""
adamc@589 338 | #"<" =>
adamc@589 339 if mode = Script then
adamc@589 340 "<"
adamc@589 341 else
adamc@589 342 "\\074"
adamc@589 343 | #"\\" => "\\\\"
adamc@589 344 | ch => String.str ch) s
adamc@589 345 ^ "\"")
adamc@589 346 | _ => str (Prim.toString p)
adamc@589 347
adamc@589 348 fun jsPat depth inner (p, _) succ fail =
adamc@589 349 case p of
adamc@589 350 PWild => succ
adamc@589 351 | PVar _ => strcat [str ("(_" ^ Int.toString (len + inner) ^ "=d"
adamc@589 352 ^ Int.toString depth ^ ","),
adamc@589 353 succ,
adamc@589 354 str ")"]
adamc@589 355 | PPrim p => strcat [str ("(d" ^ Int.toString depth ^ "=="),
adamc@589 356 jsPrim p,
adamc@589 357 str "?",
adamc@589 358 succ,
adamc@589 359 str ":",
adamc@589 360 fail,
adamc@589 361 str ")"]
adamc@589 362 | PCon (_, PConFfi {mod = "Basis", con = "True", ...}, NONE) =>
adamc@589 363 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 364 succ,
adamc@589 365 str ":",
adamc@589 366 fail,
adamc@589 367 str ")"]
adamc@589 368 | PCon (_, PConFfi {mod = "Basis", con = "False", ...}, NONE) =>
adamc@589 369 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 370 fail,
adamc@589 371 str ":",
adamc@589 372 succ,
adamc@589 373 str ")"]
adamc@596 374 | PCon (Option, _, NONE) =>
adamc@596 375 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@596 376 fail,
adamc@596 377 str ":",
adamc@596 378 succ,
adamc@596 379 str ")"]
adamc@596 380 | PCon (Option, PConVar n, SOME p) =>
adamc@596 381 (case IM.find (someTs, n) of
adamc@596 382 NONE => raise Fail "Jscomp: Not in someTs"
adamc@596 383 | SOME t =>
adamc@596 384 strcat [str ("(d" ^ Int.toString depth ^ "?("
adamc@596 385 ^ (if isNullable t then
adamc@596 386 "d" ^ Int.toString depth ^ "=d"
adamc@596 387 ^ Int.toString depth ^ ".v,"
adamc@596 388 else
adamc@596 389 "")),
adamc@596 390 jsPat depth inner p succ fail,
adamc@596 391 str "):",
adamc@596 392 fail,
adamc@596 393 str ")"])
adamc@589 394 | PCon (_, pc, NONE) =>
adamc@589 395 strcat [str ("(d" ^ Int.toString depth ^ "=="),
adamc@589 396 patCon pc,
adamc@589 397 str "?",
adamc@589 398 succ,
adamc@589 399 str ":",
adamc@589 400 fail,
adamc@589 401 str ")"]
adamc@589 402 | PCon (_, pc, SOME p) =>
adamc@589 403 strcat [str ("(d" ^ Int.toString depth ^ ".n=="),
adamc@589 404 patCon pc,
adamc@589 405 str ("?(d" ^ Int.toString depth ^ "=d" ^ Int.toString depth ^ ".v,"),
adamc@589 406 jsPat depth inner p succ fail,
adamc@589 407 str "):",
adamc@589 408 fail,
adamc@589 409 str ")"]
adamc@589 410 | PRecord xps =>
adamc@589 411 let
adamc@589 412 val (_, succ) = foldl
adamc@589 413 (fn ((x, p, _), (inner, succ)) =>
adamc@589 414 (inner + E.patBindsN p,
adamc@589 415 strcat [str ("(d" ^ Int.toString (depth+1) ^ "=d"
adamc@589 416 ^ Int.toString depth ^ "._" ^ x ^ ","),
adamc@589 417 jsPat (depth+1) inner p succ fail,
adamc@589 418 str ")"]))
adamc@589 419 (inner, succ) xps
adamc@589 420 in
adamc@589 421 succ
adamc@589 422 end
adamc@589 423 | PNone _ => strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 424 fail,
adamc@589 425 str ":",
adamc@589 426 succ,
adamc@589 427 str ")"]
adamc@594 428 | PSome (t, p) => strcat (str ("(d" ^ Int.toString depth ^ "?")
adamc@594 429 :: (if isNullable t then
adamc@594 430 [str ("d" ^ Int.toString depth
adamc@594 431 ^ "=d" ^ Int.toString depth ^ ".v")]
adamc@594 432 else
adamc@594 433 [])
adamc@594 434 @ [jsPat depth inner p succ fail,
adamc@594 435 str ":",
adamc@594 436 fail,
adamc@594 437 str ")"])
adamc@589 438
adamc@601 439 val jsifyString = String.translate (fn #"\"" => "\\\""
adamc@601 440 | #"\\" => "\\\\"
adamc@601 441 | ch => String.str ch)
adamc@601 442
adamc@601 443 fun jsifyStringMulti (n, s) =
adamc@601 444 case n of
adamc@601 445 0 => s
adamc@601 446 | _ => jsifyStringMulti (n - 1, jsifyString s)
adamc@601 447
adamc@601 448 fun deStrcat level (all as (e, _)) =
adamc@589 449 case e of
adamc@601 450 EPrim (Prim.String s) => jsifyStringMulti (level, s)
adamc@601 451 | EStrcat (e1, e2) => deStrcat level e1 ^ deStrcat level e2
adamc@601 452 | EFfiApp ("Basis", "jsifyString", [e]) => "\"" ^ deStrcat (level + 1) e ^ "\""
adamc@601 453 | _ => (Print.prefaces "deStrcat" [("e", MonoPrint.p_exp MonoEnv.empty all)];
adamc@601 454 raise Fail "Jscomp: deStrcat")
adamc@590 455
adamc@590 456 val quoteExp = quoteExp loc
adamc@567 457 in
adamc@590 458 (*Print.prefaces "jsE" [("e", MonoPrint.p_exp MonoEnv.empty e)];*)
adamc@590 459
adamc@589 460 case #1 e of
adamc@589 461 EPrim p => (jsPrim p, st)
adamc@589 462 | ERel n =>
adamc@589 463 if n < inner then
adamc@589 464 (str ("_" ^ var n), st)
adamc@589 465 else
adamc@589 466 let
adamc@589 467 val n = n - inner
adamc@589 468 in
adamc@593 469 quoteExp (List.nth (outer, n)) ((ERel (n - skip), loc), st)
adamc@589 470 end
adamc@567 471
adamc@589 472 | ENamed n =>
adamc@589 473 let
adamc@589 474 val st =
adamc@589 475 if IS.member (#included st, n) then
adamc@589 476 st
adamc@589 477 else
adamc@589 478 case IM.find (nameds, n) of
adamc@589 479 NONE => raise Fail "Jscomp: Unbound ENamed"
adamc@589 480 | SOME e =>
adamc@589 481 let
adamc@589 482 val st = {decls = #decls st,
adamc@589 483 script = #script st,
adamc@595 484 included = IS.add (#included st, n),
adamc@595 485 injectors = #injectors st,
adamc@595 486 maxName = #maxName st}
adamc@578 487
adamc@589 488 val (e, st) = jsExp mode skip [] 0 (e, st)
adamc@601 489 val () = Print.prefaces "Pre-e" [("e", MonoPrint.p_exp MonoEnv.empty e)]
adamc@601 490 val e = deStrcat 0 e
adamc@589 491
adamc@589 492 val sc = "_n" ^ Int.toString n ^ "=" ^ e ^ ";\n"
adamc@589 493 in
adamc@589 494 {decls = #decls st,
adamc@589 495 script = sc :: #script st,
adamc@595 496 included = #included st,
adamc@595 497 injectors = #injectors st,
adamc@595 498 maxName = #maxName st}
adamc@589 499 end
adamc@589 500 in
adamc@589 501 (str ("_n" ^ Int.toString n), st)
adamc@589 502 end
adamc@589 503
adamc@596 504 | ECon (Option, _, NONE) => (str "null", st)
adamc@596 505 | ECon (Option, PConVar n, SOME e) =>
adamc@596 506 let
adamc@596 507 val (e, st) = jsE inner (e, st)
adamc@596 508 in
adamc@596 509 case IM.find (someTs, n) of
adamc@596 510 NONE => raise Fail "Jscomp: Not in someTs [2]"
adamc@596 511 | SOME t =>
adamc@596 512 (if isNullable t then
adamc@596 513 strcat [str "{v:",
adamc@596 514 e,
adamc@596 515 str "}"]
adamc@596 516 else
adamc@596 517 e, st)
adamc@596 518 end
adamc@596 519
adamc@589 520 | ECon (_, pc, NONE) => (patCon pc, st)
adamc@589 521 | ECon (_, pc, SOME e) =>
adamc@589 522 let
adamc@589 523 val (s, st) = jsE inner (e, st)
adamc@589 524 in
adamc@589 525 (strcat [str "{n:",
adamc@589 526 patCon pc,
adamc@589 527 str ",v:",
adamc@589 528 s,
adamc@589 529 str "}"], st)
adamc@589 530 end
adamc@596 531
adamc@589 532 | ENone _ => (str "null", st)
adamc@589 533 | ESome (t, e) =>
adamc@572 534 let
adamc@572 535 val (e, st) = jsE inner (e, st)
adamc@572 536 in
adamc@589 537 (if isNullable t then
adamc@589 538 strcat [str "{v:", e, str "}"]
adamc@589 539 else
adamc@589 540 e, st)
adamc@589 541 end
adamc@589 542
adamc@589 543 | EFfi k =>
adamc@589 544 let
adamc@589 545 val name = case ffi k of
adamc@589 546 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k
adamc@589 547 ^ " in JavaScript");
adamc@589 548 "ERROR")
adamc@589 549 | SOME s => s
adamc@589 550 in
adamc@589 551 (str name, st)
adamc@589 552 end
adamc@589 553 | EFfiApp (m, x, args) =>
adamc@589 554 let
adamc@589 555 val args =
adamc@589 556 case (m, x, args) of
adamc@589 557 ("Basis", "new_client_source", [(EJavaScript (_, e, _), _)]) => [e]
adamc@589 558 | ("Basis", "set_client_source", [e1, (EJavaScript (_, e2, _), _)]) => [e1, e2]
adamc@589 559 | _ => args
adamc@589 560
adamc@589 561 val name = case ffi (m, x) of
adamc@589 562 NONE => (EM.errorAt loc ("Unsupported FFI function "
adamc@589 563 ^ x ^ " in JavaScript");
adamc@589 564 "ERROR")
adamc@589 565 | SOME s => s
adamc@589 566 in
adamc@589 567 case args of
adamc@589 568 [] => (str (name ^ "()"), st)
adamc@589 569 | [e] =>
adamc@589 570 let
adamc@589 571 val (e, st) = jsE inner (e, st)
adamc@589 572 in
adamc@589 573 (strcat [str (name ^ "("),
adamc@589 574 e,
adamc@589 575 str ")"], st)
adamc@589 576 end
adamc@589 577 | e :: es =>
adamc@589 578 let
adamc@589 579 val (e, st) = jsE inner (e, st)
adamc@589 580 val (es, st) = ListUtil.foldlMapConcat
adamc@589 581 (fn (e, st) =>
adamc@589 582 let
adamc@589 583 val (e, st) = jsE inner (e, st)
adamc@589 584 in
adamc@589 585 ([str ",", e], st)
adamc@589 586 end)
adamc@589 587 st es
adamc@589 588 in
adamc@589 589 (strcat (str (name ^ "(")
adamc@589 590 :: e
adamc@589 591 :: es
adamc@589 592 @ [str ")"]), st)
adamc@589 593 end
adamc@589 594 end
adamc@589 595
adamc@589 596 | EApp (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 [e1, str "(", e2, str ")"], st)
adamc@589 602 end
adamc@589 603 | EAbs (_, _, _, e) =>
adamc@589 604 let
adamc@589 605 val locals = List.tabulate
adamc@589 606 (varDepth e,
adamc@589 607 fn i => str ("var _" ^ Int.toString (len + inner + i + 1) ^ ";"))
adamc@589 608 val (e, st) = jsE (inner + 1) (e, st)
adamc@589 609 in
adamc@589 610 (strcat (str ("function(_"
adamc@589 611 ^ Int.toString (len + inner)
adamc@589 612 ^ "){")
adamc@589 613 :: locals
adamc@589 614 @ [str "return ",
adamc@589 615 e,
adamc@589 616 str "}"]),
adamc@589 617 st)
adamc@589 618 end
adamc@589 619
adamc@589 620 | EUnop (s, e) =>
adamc@589 621 let
adamc@589 622 val (e, st) = jsE inner (e, st)
adamc@589 623 in
adamc@589 624 (strcat [str ("(" ^ s),
adamc@572 625 e,
adamc@589 626 str ")"],
adamc@589 627 st)
adamc@589 628 end
adamc@589 629 | EBinop (s, e1, e2) =>
adamc@589 630 let
adamc@589 631 val (e1, st) = jsE inner (e1, st)
adamc@589 632 val (e2, st) = jsE inner (e2, st)
adamc@589 633 in
adamc@589 634 (strcat [str "(",
adamc@589 635 e1,
adamc@589 636 str s,
adamc@589 637 e2,
adamc@589 638 str ")"],
adamc@589 639 st)
adamc@589 640 end
adamc@589 641
adamc@589 642 | ERecord [] => (str "null", st)
adamc@589 643 | ERecord [(x, e, _)] =>
adamc@589 644 let
adamc@589 645 val (e, st) = jsE inner (e, st)
adamc@589 646 in
adamc@589 647 (strcat [str "{_x:", e, str "}"], st)
adamc@589 648 end
adamc@589 649 | ERecord ((x, e, _) :: xes) =>
adamc@589 650 let
adamc@589 651 val (e, st) = jsE inner (e, st)
adamc@589 652
adamc@589 653 val (es, st) =
adamc@589 654 foldr (fn ((x, e, _), (es, st)) =>
adamc@589 655 let
adamc@589 656 val (e, st) = jsE inner (e, st)
adamc@589 657 in
adamc@589 658 (str (",_" ^ x ^ ":")
adamc@589 659 :: e
adamc@589 660 :: es,
adamc@589 661 st)
adamc@589 662 end)
adamc@589 663 ([str "}"], st) xes
adamc@589 664 in
adamc@589 665 (strcat (str ("{_" ^ x ^ ":")
adamc@589 666 :: e
adamc@589 667 :: es),
adamc@589 668 st)
adamc@589 669 end
adamc@589 670 | EField (e, x) =>
adamc@589 671 let
adamc@589 672 val (e, st) = jsE inner (e, st)
adamc@589 673 in
adamc@589 674 (strcat [e,
adamc@589 675 str ("._" ^ x)], st)
adamc@589 676 end
adamc@589 677
adamc@591 678 | ECase (e', pes, {result, ...}) =>
adamc@591 679 if closedUpto inner e andalso List.all (fn (_, e) => closedUpto inner e) pes then
adamc@593 680 let
adamc@593 681 val (e', st) = quoteExp result ((ERel 0, loc), st)
adamc@593 682 in
adamc@593 683 ((ELet ("js", result, e, e'), loc),
adamc@593 684 st)
adamc@593 685 end
adamc@591 686 else
adamc@591 687 let
adamc@591 688 val plen = length pes
adamc@589 689
adamc@591 690 val (cases, st) = ListUtil.foldliMap
adamc@591 691 (fn (i, (p, e), st) =>
adamc@591 692 let
adamc@591 693 val (e, st) = jsE (inner + E.patBindsN p) (e, st)
adamc@591 694 val fail =
adamc@591 695 if i = plen - 1 then
adamc@591 696 str "pf()"
adamc@591 697 else
adamc@591 698 str ("c" ^ Int.toString (i+1) ^ "()")
adamc@591 699 val c = jsPat 0 inner p e fail
adamc@591 700 in
adamc@591 701 (strcat [str ("c" ^ Int.toString i ^ "=function(){return "),
adamc@591 702 c,
adamc@591 703 str "},"],
adamc@591 704 st)
adamc@591 705 end)
adamc@591 706 st pes
adamc@589 707
adamc@591 708 val (e, st) = jsE inner (e', st)
adamc@591 709 in
adamc@591 710 (strcat (str "("
adamc@591 711 :: List.revAppend (cases,
adamc@591 712 [str "d0=",
adamc@591 713 e,
adamc@591 714 str ",c0())"])), st)
adamc@591 715 end
adamc@589 716
adamc@589 717 | EStrcat (e1, e2) =>
adamc@589 718 let
adamc@589 719 val (e1, st) = jsE inner (e1, st)
adamc@589 720 val (e2, st) = jsE inner (e2, st)
adamc@589 721 in
adamc@589 722 (strcat [str "(", e1, str "+", e2, str ")"], st)
adamc@589 723 end
adamc@589 724
adamc@589 725 | EError (e, _) =>
adamc@589 726 let
adamc@589 727 val (e, st) = jsE inner (e, st)
adamc@589 728 in
adamc@589 729 (strcat [str "alert(\"ERROR: \"+", e, str ")"],
adamc@589 730 st)
adamc@589 731 end
adamc@589 732
adamc@589 733 | EWrite e =>
adamc@589 734 let
adamc@589 735 val (e, st) = jsE inner (e, st)
adamc@589 736 in
adamc@589 737 (strcat [str "document.write(",
adamc@589 738 e,
adamc@589 739 str ".v)"], st)
adamc@589 740 end
adamc@589 741
adamc@589 742 | ESeq (e1, e2) =>
adamc@589 743 let
adamc@589 744 val (e1, st) = jsE inner (e1, st)
adamc@589 745 val (e2, st) = jsE inner (e2, st)
adamc@589 746 in
adamc@589 747 (strcat [str "(", e1, str ",", e2, str ")"], st)
adamc@589 748 end
adamc@589 749 | ELet (_, _, e1, e2) =>
adamc@589 750 let
adamc@589 751 val (e1, st) = jsE inner (e1, st)
adamc@589 752 val (e2, st) = jsE (inner + 1) (e2, st)
adamc@589 753 in
adamc@589 754 (strcat [str ("(_" ^ Int.toString (len + inner) ^ "="),
adamc@589 755 e1,
adamc@589 756 str ",",
adamc@589 757 e2,
adamc@572 758 str ")"], st)
adamc@572 759 end
adamc@589 760
adamc@601 761 | EJavaScript (Source _, _, SOME _) => (e, st)
adamc@601 762 | EJavaScript (_, _, SOME e) => ((EFfiApp ("Basis", "jsifyString", [e]), loc), st)
adamc@590 763
adamc@589 764 | EClosure _ => unsupported "EClosure"
adamc@589 765 | EQuery _ => unsupported "Query"
adamc@589 766 | EDml _ => unsupported "DML"
adamc@589 767 | ENextval _ => unsupported "Nextval"
adamc@589 768 | EUnurlify _ => unsupported "EUnurlify"
adamc@601 769 | EJavaScript (_, e, _) =>
adamc@601 770 let
adamc@601 771 val (e, st) = jsE inner (e, st)
adamc@601 772 in
adamc@601 773 ((EFfiApp ("Basis", "jsifyString", [e]), loc), st)
adamc@601 774 end
adamc@590 775
adamc@589 776 | ESignalReturn e =>
adamc@572 777 let
adamc@572 778 val (e, st) = jsE inner (e, st)
adamc@572 779 in
adamc@589 780 (strcat [str "sr(",
adamc@589 781 e,
adamc@589 782 str ")"],
adamc@589 783 st)
adamc@589 784 end
adamc@589 785 | ESignalBind (e1, e2) =>
adamc@589 786 let
adamc@589 787 val (e1, st) = jsE inner (e1, st)
adamc@589 788 val (e2, st) = jsE inner (e2, st)
adamc@589 789 in
adamc@589 790 (strcat [str "sb(",
adamc@589 791 e1,
adamc@589 792 str ",",
adamc@589 793 e2,
adamc@589 794 str ")"],
adamc@589 795 st)
adamc@589 796 end
adamc@589 797 | ESignalSource e =>
adamc@589 798 let
adamc@589 799 val (e, st) = jsE inner (e, st)
adamc@589 800 in
adamc@589 801 (strcat [str "ss(",
adamc@589 802 e,
adamc@589 803 str ")"],
adamc@589 804 st)
adamc@572 805 end
adamc@567 806 end
adamc@589 807 in
adamc@589 808 jsE
adamc@589 809 end
adamc@567 810
adamc@589 811 val decl : state -> decl -> decl * state =
adamc@589 812 U.Decl.foldMapB {typ = fn x => x,
adamc@589 813 exp = fn (env, e, st) =>
adamc@589 814 let
adamc@589 815 fun doCode m skip env orig e =
adamc@589 816 let
adamc@589 817 val len = length env
adamc@589 818 fun str s = (EPrim (Prim.String s), #2 e)
adamc@567 819
adamc@589 820 val locals = List.tabulate
adamc@589 821 (varDepth e,
adamc@589 822 fn i => str ("var _" ^ Int.toString (len + i) ^ ";"))
adamc@589 823 val (e, st) = jsExp m skip env 0 (e, st)
adamc@589 824 in
adamc@589 825 (EJavaScript (m, orig, SOME (strcat (#2 e) (locals @ [e]))), st)
adamc@589 826 end
adamc@589 827 in
adamc@589 828 case e of
adamc@591 829 EJavaScript (m, orig as (EAbs (_, t, _, e), _), NONE) =>
adamc@591 830 doCode m 1 (t :: env) orig e
adamc@591 831 | EJavaScript (m, orig, NONE) =>
adamc@591 832 doCode m 0 env orig orig
adamc@589 833 | _ => (e, st)
adamc@589 834 end,
adamc@589 835 decl = fn (_, e, st) => (e, st),
adamc@589 836 bind = fn (env, U.Decl.RelE (_, t)) => t :: env
adamc@589 837 | (env, _) => env}
adamc@589 838 []
adamc@567 839
adamc@567 840 fun doDecl (d, st) =
adamc@567 841 let
adamc@567 842 val (d, st) = decl st d
adamc@567 843 in
adamc@567 844 (List.revAppend (#decls st, [d]),
adamc@567 845 {decls = [],
adamc@589 846 script = #script st,
adamc@595 847 included = #included st,
adamc@595 848 injectors = #injectors st,
adamc@595 849 maxName = #maxName st})
adamc@567 850 end
adamc@567 851
adamc@567 852 val (ds, st) = ListUtil.foldlMapConcat doDecl
adamc@567 853 {decls = [],
adamc@589 854 script = [],
adamc@595 855 included = IS.empty,
adamc@595 856 injectors = IM.empty,
adamc@595 857 maxName = U.File.maxName file + 1}
adamc@567 858 file
adamc@569 859
adamc@569 860 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
adamc@569 861 fun lines acc =
adamc@569 862 case TextIO.inputLine inf of
adamc@569 863 NONE => String.concat (rev acc)
adamc@569 864 | SOME line => lines (line :: acc)
adamc@569 865 val lines = lines []
adamc@567 866 in
adamc@569 867 TextIO.closeIn inf;
adamc@589 868 (DJavaScript (lines ^ String.concat (rev (#script st))), ErrorMsg.dummySpan) :: ds
adamc@567 869 end
adamc@567 870
adamc@567 871 end