annotate src/jscomp.sml @ 603:b1064de2b1f9

dlist example working
author Adam Chlipala <adamc@hcoop.net>
date Fri, 16 Jan 2009 15:49:10 -0500
parents 7c3c21eb5b4c
children 330a7de47914
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 e = deStrcat 0 e
adamc@589 490
adamc@589 491 val sc = "_n" ^ Int.toString n ^ "=" ^ e ^ ";\n"
adamc@589 492 in
adamc@589 493 {decls = #decls st,
adamc@589 494 script = sc :: #script st,
adamc@595 495 included = #included st,
adamc@595 496 injectors = #injectors st,
adamc@595 497 maxName = #maxName st}
adamc@589 498 end
adamc@589 499 in
adamc@589 500 (str ("_n" ^ Int.toString n), st)
adamc@589 501 end
adamc@589 502
adamc@596 503 | ECon (Option, _, NONE) => (str "null", st)
adamc@596 504 | ECon (Option, PConVar n, SOME e) =>
adamc@596 505 let
adamc@596 506 val (e, st) = jsE inner (e, st)
adamc@596 507 in
adamc@596 508 case IM.find (someTs, n) of
adamc@596 509 NONE => raise Fail "Jscomp: Not in someTs [2]"
adamc@596 510 | SOME t =>
adamc@596 511 (if isNullable t then
adamc@596 512 strcat [str "{v:",
adamc@596 513 e,
adamc@596 514 str "}"]
adamc@596 515 else
adamc@596 516 e, st)
adamc@596 517 end
adamc@596 518
adamc@589 519 | ECon (_, pc, NONE) => (patCon pc, st)
adamc@589 520 | ECon (_, pc, SOME e) =>
adamc@589 521 let
adamc@589 522 val (s, st) = jsE inner (e, st)
adamc@589 523 in
adamc@589 524 (strcat [str "{n:",
adamc@589 525 patCon pc,
adamc@589 526 str ",v:",
adamc@589 527 s,
adamc@589 528 str "}"], st)
adamc@589 529 end
adamc@596 530
adamc@589 531 | ENone _ => (str "null", st)
adamc@589 532 | ESome (t, e) =>
adamc@572 533 let
adamc@572 534 val (e, st) = jsE inner (e, st)
adamc@572 535 in
adamc@589 536 (if isNullable t then
adamc@589 537 strcat [str "{v:", e, str "}"]
adamc@589 538 else
adamc@589 539 e, st)
adamc@589 540 end
adamc@589 541
adamc@589 542 | EFfi k =>
adamc@589 543 let
adamc@589 544 val name = case ffi k of
adamc@589 545 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k
adamc@589 546 ^ " in JavaScript");
adamc@589 547 "ERROR")
adamc@589 548 | SOME s => s
adamc@589 549 in
adamc@589 550 (str name, st)
adamc@589 551 end
adamc@589 552 | EFfiApp (m, x, args) =>
adamc@589 553 let
adamc@589 554 val args =
adamc@589 555 case (m, x, args) of
adamc@589 556 ("Basis", "new_client_source", [(EJavaScript (_, e, _), _)]) => [e]
adamc@589 557 | ("Basis", "set_client_source", [e1, (EJavaScript (_, e2, _), _)]) => [e1, e2]
adamc@589 558 | _ => args
adamc@589 559
adamc@589 560 val name = case ffi (m, x) of
adamc@589 561 NONE => (EM.errorAt loc ("Unsupported FFI function "
adamc@589 562 ^ x ^ " in JavaScript");
adamc@589 563 "ERROR")
adamc@589 564 | SOME s => s
adamc@589 565 in
adamc@589 566 case args of
adamc@589 567 [] => (str (name ^ "()"), st)
adamc@589 568 | [e] =>
adamc@589 569 let
adamc@589 570 val (e, st) = jsE inner (e, st)
adamc@589 571 in
adamc@589 572 (strcat [str (name ^ "("),
adamc@589 573 e,
adamc@589 574 str ")"], st)
adamc@589 575 end
adamc@589 576 | e :: es =>
adamc@589 577 let
adamc@589 578 val (e, st) = jsE inner (e, st)
adamc@589 579 val (es, st) = ListUtil.foldlMapConcat
adamc@589 580 (fn (e, st) =>
adamc@589 581 let
adamc@589 582 val (e, st) = jsE inner (e, st)
adamc@589 583 in
adamc@589 584 ([str ",", e], st)
adamc@589 585 end)
adamc@589 586 st es
adamc@589 587 in
adamc@589 588 (strcat (str (name ^ "(")
adamc@589 589 :: e
adamc@589 590 :: es
adamc@589 591 @ [str ")"]), st)
adamc@589 592 end
adamc@589 593 end
adamc@589 594
adamc@589 595 | EApp (e1, e2) =>
adamc@589 596 let
adamc@589 597 val (e1, st) = jsE inner (e1, st)
adamc@589 598 val (e2, st) = jsE inner (e2, st)
adamc@589 599 in
adamc@589 600 (strcat [e1, str "(", e2, str ")"], st)
adamc@589 601 end
adamc@589 602 | EAbs (_, _, _, e) =>
adamc@589 603 let
adamc@589 604 val locals = List.tabulate
adamc@589 605 (varDepth e,
adamc@589 606 fn i => str ("var _" ^ Int.toString (len + inner + i + 1) ^ ";"))
adamc@589 607 val (e, st) = jsE (inner + 1) (e, st)
adamc@589 608 in
adamc@589 609 (strcat (str ("function(_"
adamc@589 610 ^ Int.toString (len + inner)
adamc@589 611 ^ "){")
adamc@589 612 :: locals
adamc@589 613 @ [str "return ",
adamc@589 614 e,
adamc@589 615 str "}"]),
adamc@589 616 st)
adamc@589 617 end
adamc@589 618
adamc@589 619 | EUnop (s, e) =>
adamc@589 620 let
adamc@589 621 val (e, st) = jsE inner (e, st)
adamc@589 622 in
adamc@589 623 (strcat [str ("(" ^ s),
adamc@572 624 e,
adamc@589 625 str ")"],
adamc@589 626 st)
adamc@589 627 end
adamc@589 628 | EBinop (s, e1, e2) =>
adamc@589 629 let
adamc@589 630 val (e1, st) = jsE inner (e1, st)
adamc@589 631 val (e2, st) = jsE inner (e2, st)
adamc@589 632 in
adamc@589 633 (strcat [str "(",
adamc@589 634 e1,
adamc@589 635 str s,
adamc@589 636 e2,
adamc@589 637 str ")"],
adamc@589 638 st)
adamc@589 639 end
adamc@589 640
adamc@589 641 | ERecord [] => (str "null", st)
adamc@589 642 | ERecord [(x, e, _)] =>
adamc@589 643 let
adamc@589 644 val (e, st) = jsE inner (e, st)
adamc@589 645 in
adamc@589 646 (strcat [str "{_x:", e, str "}"], st)
adamc@589 647 end
adamc@589 648 | ERecord ((x, e, _) :: xes) =>
adamc@589 649 let
adamc@589 650 val (e, st) = jsE inner (e, st)
adamc@589 651
adamc@589 652 val (es, st) =
adamc@589 653 foldr (fn ((x, e, _), (es, st)) =>
adamc@589 654 let
adamc@589 655 val (e, st) = jsE inner (e, st)
adamc@589 656 in
adamc@589 657 (str (",_" ^ x ^ ":")
adamc@589 658 :: e
adamc@589 659 :: es,
adamc@589 660 st)
adamc@589 661 end)
adamc@589 662 ([str "}"], st) xes
adamc@589 663 in
adamc@589 664 (strcat (str ("{_" ^ x ^ ":")
adamc@589 665 :: e
adamc@589 666 :: es),
adamc@589 667 st)
adamc@589 668 end
adamc@589 669 | EField (e, x) =>
adamc@589 670 let
adamc@589 671 val (e, st) = jsE inner (e, st)
adamc@589 672 in
adamc@589 673 (strcat [e,
adamc@589 674 str ("._" ^ x)], st)
adamc@589 675 end
adamc@589 676
adamc@591 677 | ECase (e', pes, {result, ...}) =>
adamc@591 678 if closedUpto inner e andalso List.all (fn (_, e) => closedUpto inner e) pes then
adamc@593 679 let
adamc@593 680 val (e', st) = quoteExp result ((ERel 0, loc), st)
adamc@593 681 in
adamc@593 682 ((ELet ("js", result, e, e'), loc),
adamc@593 683 st)
adamc@593 684 end
adamc@591 685 else
adamc@591 686 let
adamc@591 687 val plen = length pes
adamc@589 688
adamc@591 689 val (cases, st) = ListUtil.foldliMap
adamc@591 690 (fn (i, (p, e), st) =>
adamc@591 691 let
adamc@591 692 val (e, st) = jsE (inner + E.patBindsN p) (e, st)
adamc@591 693 val fail =
adamc@591 694 if i = plen - 1 then
adamc@591 695 str "pf()"
adamc@591 696 else
adamc@591 697 str ("c" ^ Int.toString (i+1) ^ "()")
adamc@591 698 val c = jsPat 0 inner p e fail
adamc@591 699 in
adamc@591 700 (strcat [str ("c" ^ Int.toString i ^ "=function(){return "),
adamc@591 701 c,
adamc@591 702 str "},"],
adamc@591 703 st)
adamc@591 704 end)
adamc@591 705 st pes
adamc@589 706
adamc@591 707 val (e, st) = jsE inner (e', st)
adamc@591 708 in
adamc@591 709 (strcat (str "("
adamc@591 710 :: List.revAppend (cases,
adamc@591 711 [str "d0=",
adamc@591 712 e,
adamc@591 713 str ",c0())"])), st)
adamc@591 714 end
adamc@589 715
adamc@589 716 | EStrcat (e1, e2) =>
adamc@589 717 let
adamc@589 718 val (e1, st) = jsE inner (e1, st)
adamc@589 719 val (e2, st) = jsE inner (e2, st)
adamc@589 720 in
adamc@589 721 (strcat [str "(", e1, str "+", e2, str ")"], st)
adamc@589 722 end
adamc@589 723
adamc@589 724 | EError (e, _) =>
adamc@589 725 let
adamc@589 726 val (e, st) = jsE inner (e, st)
adamc@589 727 in
adamc@589 728 (strcat [str "alert(\"ERROR: \"+", e, str ")"],
adamc@589 729 st)
adamc@589 730 end
adamc@589 731
adamc@589 732 | EWrite e =>
adamc@589 733 let
adamc@589 734 val (e, st) = jsE inner (e, st)
adamc@589 735 in
adamc@589 736 (strcat [str "document.write(",
adamc@589 737 e,
adamc@589 738 str ".v)"], st)
adamc@589 739 end
adamc@589 740
adamc@589 741 | ESeq (e1, e2) =>
adamc@589 742 let
adamc@589 743 val (e1, st) = jsE inner (e1, st)
adamc@589 744 val (e2, st) = jsE inner (e2, st)
adamc@589 745 in
adamc@589 746 (strcat [str "(", e1, str ",", e2, str ")"], st)
adamc@589 747 end
adamc@589 748 | ELet (_, _, e1, e2) =>
adamc@589 749 let
adamc@589 750 val (e1, st) = jsE inner (e1, st)
adamc@589 751 val (e2, st) = jsE (inner + 1) (e2, st)
adamc@589 752 in
adamc@589 753 (strcat [str ("(_" ^ Int.toString (len + inner) ^ "="),
adamc@589 754 e1,
adamc@589 755 str ",",
adamc@589 756 e2,
adamc@572 757 str ")"], st)
adamc@572 758 end
adamc@589 759
adamc@601 760 | EJavaScript (Source _, _, SOME _) => (e, st)
adamc@603 761 | EJavaScript (_, _, SOME e) =>
adamc@603 762 (strcat [str "\"cr(\"+ca(function(){return ",
adamc@603 763 e,
adamc@603 764 str "})+\")\""],
adamc@603 765 st)
adamc@590 766
adamc@589 767 | EClosure _ => unsupported "EClosure"
adamc@589 768 | EQuery _ => unsupported "Query"
adamc@589 769 | EDml _ => unsupported "DML"
adamc@589 770 | ENextval _ => unsupported "Nextval"
adamc@589 771 | EUnurlify _ => unsupported "EUnurlify"
adamc@601 772 | EJavaScript (_, e, _) =>
adamc@601 773 let
adamc@601 774 val (e, st) = jsE inner (e, st)
adamc@601 775 in
adamc@603 776 (strcat [str "\"cr(\"+ca(function(){return ",
adamc@603 777 e,
adamc@603 778 str "})+\")\""],
adamc@603 779 st)
adamc@601 780 end
adamc@590 781
adamc@589 782 | ESignalReturn e =>
adamc@572 783 let
adamc@572 784 val (e, st) = jsE inner (e, st)
adamc@572 785 in
adamc@589 786 (strcat [str "sr(",
adamc@589 787 e,
adamc@589 788 str ")"],
adamc@589 789 st)
adamc@589 790 end
adamc@589 791 | ESignalBind (e1, e2) =>
adamc@589 792 let
adamc@589 793 val (e1, st) = jsE inner (e1, st)
adamc@589 794 val (e2, st) = jsE inner (e2, st)
adamc@589 795 in
adamc@589 796 (strcat [str "sb(",
adamc@589 797 e1,
adamc@589 798 str ",",
adamc@589 799 e2,
adamc@589 800 str ")"],
adamc@589 801 st)
adamc@589 802 end
adamc@589 803 | ESignalSource e =>
adamc@589 804 let
adamc@589 805 val (e, st) = jsE inner (e, st)
adamc@589 806 in
adamc@589 807 (strcat [str "ss(",
adamc@589 808 e,
adamc@589 809 str ")"],
adamc@589 810 st)
adamc@572 811 end
adamc@567 812 end
adamc@589 813 in
adamc@589 814 jsE
adamc@589 815 end
adamc@567 816
adamc@589 817 val decl : state -> decl -> decl * state =
adamc@589 818 U.Decl.foldMapB {typ = fn x => x,
adamc@589 819 exp = fn (env, e, st) =>
adamc@589 820 let
adamc@589 821 fun doCode m skip env orig e =
adamc@589 822 let
adamc@589 823 val len = length env
adamc@589 824 fun str s = (EPrim (Prim.String s), #2 e)
adamc@567 825
adamc@589 826 val locals = List.tabulate
adamc@589 827 (varDepth e,
adamc@589 828 fn i => str ("var _" ^ Int.toString (len + i) ^ ";"))
adamc@589 829 val (e, st) = jsExp m skip env 0 (e, st)
adamc@589 830 in
adamc@589 831 (EJavaScript (m, orig, SOME (strcat (#2 e) (locals @ [e]))), st)
adamc@589 832 end
adamc@589 833 in
adamc@589 834 case e of
adamc@591 835 EJavaScript (m, orig as (EAbs (_, t, _, e), _), NONE) =>
adamc@591 836 doCode m 1 (t :: env) orig e
adamc@591 837 | EJavaScript (m, orig, NONE) =>
adamc@591 838 doCode m 0 env orig orig
adamc@589 839 | _ => (e, st)
adamc@589 840 end,
adamc@589 841 decl = fn (_, e, st) => (e, st),
adamc@589 842 bind = fn (env, U.Decl.RelE (_, t)) => t :: env
adamc@589 843 | (env, _) => env}
adamc@589 844 []
adamc@567 845
adamc@567 846 fun doDecl (d, st) =
adamc@567 847 let
adamc@567 848 val (d, st) = decl st d
adamc@567 849 in
adamc@567 850 (List.revAppend (#decls st, [d]),
adamc@567 851 {decls = [],
adamc@589 852 script = #script st,
adamc@595 853 included = #included st,
adamc@595 854 injectors = #injectors st,
adamc@595 855 maxName = #maxName st})
adamc@567 856 end
adamc@567 857
adamc@567 858 val (ds, st) = ListUtil.foldlMapConcat doDecl
adamc@567 859 {decls = [],
adamc@589 860 script = [],
adamc@595 861 included = IS.empty,
adamc@595 862 injectors = IM.empty,
adamc@595 863 maxName = U.File.maxName file + 1}
adamc@567 864 file
adamc@569 865
adamc@569 866 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
adamc@569 867 fun lines acc =
adamc@569 868 case TextIO.inputLine inf of
adamc@569 869 NONE => String.concat (rev acc)
adamc@569 870 | SOME line => lines (line :: acc)
adamc@569 871 val lines = lines []
adamc@567 872 in
adamc@569 873 TextIO.closeIn inf;
adamc@589 874 (DJavaScript (lines ^ String.concat (rev (#script st))), ErrorMsg.dummySpan) :: ds
adamc@567 875 end
adamc@567 876
adamc@567 877 end