annotate src/jscomp.sml @ 583:1fd4c041634e

Reactive computation with more base types and records
author Adam Chlipala <adamc@hcoop.net>
date Thu, 01 Jan 2009 10:18:20 -0500
parents 66463006f893
children 101eb0058136
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@572 36 val funcs = [(("Basis", "alert"), "alert"),
adamc@583 37 (("Basis", "htmlifyFloat"), "ts"),
adamc@582 38 (("Basis", "htmlifyInt"), "ts"),
adamc@574 39 (("Basis", "htmlifyString"), "escape"),
adamc@579 40 (("Basis", "new_client_source"), "sc"),
adamc@579 41 (("Basis", "set_client_source"), "sv")]
adamc@572 42
adamc@572 43 structure FM = BinaryMapFn(struct
adamc@572 44 type ord_key = string * string
adamc@572 45 fun compare ((m1, x1), (m2, x2)) =
adamc@572 46 Order.join (String.compare (m1, m2),
adamc@572 47 fn () => String.compare (x1, x2))
adamc@572 48 end)
adamc@572 49
adamc@572 50 val funcs = foldl (fn ((k, v), m) => FM.insert (m, k, v)) FM.empty funcs
adamc@572 51
adamc@572 52 fun ffi k = FM.find (funcs, k)
adamc@572 53
adamc@567 54 type state = {
adamc@567 55 decls : decl list,
adamc@567 56 script : string
adamc@567 57 }
adamc@567 58
adamc@567 59 fun varDepth (e, _) =
adamc@567 60 case e of
adamc@567 61 EPrim _ => 0
adamc@567 62 | ERel _ => 0
adamc@567 63 | ENamed _ => 0
adamc@567 64 | ECon (_, _, NONE) => 0
adamc@567 65 | ECon (_, _, SOME e) => varDepth e
adamc@567 66 | ENone _ => 0
adamc@567 67 | ESome (_, e) => varDepth e
adamc@567 68 | EFfi _ => 0
adamc@567 69 | EFfiApp (_, _, es) => foldl Int.max 0 (map varDepth es)
adamc@567 70 | EApp (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 71 | EAbs _ => 0
adamc@567 72 | EUnop (_, e) => varDepth e
adamc@567 73 | EBinop (_, e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 74 | ERecord xes => foldl Int.max 0 (map (fn (_, e, _) => varDepth e) xes)
adamc@567 75 | EField (e, _) => varDepth e
adamc@567 76 | ECase (e, pes, _) =>
adamc@567 77 foldl Int.max (varDepth e)
adamc@567 78 (map (fn (p, e) => E.patBindsN p + varDepth e) pes)
adamc@567 79 | EStrcat (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 80 | EError (e, _) => varDepth e
adamc@567 81 | EWrite e => varDepth e
adamc@567 82 | ESeq (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 83 | ELet (_, _, e1, e2) => Int.max (varDepth e1, 1 + varDepth e2)
adamc@567 84 | EClosure _ => 0
adamc@567 85 | EQuery _ => 0
adamc@567 86 | EDml _ => 0
adamc@567 87 | ENextval _ => 0
adamc@567 88 | EUnurlify _ => 0
adamc@567 89 | EJavaScript _ => 0
adamc@568 90 | ESignalReturn e => varDepth e
adamc@572 91 | ESignalBind (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@574 92 | ESignalSource e => varDepth e
adamc@567 93
adamc@568 94 fun strcat loc es =
adamc@568 95 case es of
adamc@568 96 [] => (EPrim (Prim.String ""), loc)
adamc@568 97 | [x] => x
adamc@568 98 | x :: es' => (EStrcat (x, strcat loc es'), loc)
adamc@568 99
adamc@579 100 fun jsExp mode skip outer =
adamc@567 101 let
adamc@567 102 val len = length outer
adamc@567 103
adamc@567 104 fun jsE inner (e as (_, loc), st) =
adamc@567 105 let
adamc@567 106 fun str s = (EPrim (Prim.String s), loc)
adamc@567 107
adamc@567 108 fun var n = Int.toString (len + inner - n - 1)
adamc@567 109
adamc@567 110 fun patCon pc =
adamc@567 111 case pc of
adamc@567 112 PConVar n => str (Int.toString n)
adamc@567 113 | PConFfi {con, ...} => str ("\"_" ^ con ^ "\"")
adamc@567 114
adamc@567 115 fun isNullable (t, _) =
adamc@567 116 case t of
adamc@567 117 TOption _ => true
adamc@583 118 | TRecord [] => true
adamc@567 119 | _ => false
adamc@567 120
adamc@567 121 fun unsupported s =
adamc@567 122 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript");
adamc@567 123 (str "ERROR", st))
adamc@568 124
adamc@568 125 val strcat = strcat loc
adamc@577 126
adamc@577 127 fun quoteExp (t : typ) e =
adamc@577 128 case #1 t of
adamc@577 129 TSource => strcat [str "s",
adamc@577 130 (EFfiApp ("Basis", "htmlifyInt", [e]), loc)]
adamc@579 131 | TRecord [] => str "null"
adamc@579 132 | TFfi ("Basis", "string") => e
adamc@577 133 | _ => (EM.errorAt loc "Don't know how to embed type in JavaScript";
adamc@579 134 Print.prefaces "Can't embed" [("t", MonoPrint.p_typ MonoEnv.empty t)];
adamc@577 135 str "ERROR")
adamc@567 136 in
adamc@567 137 case #1 e of
adamc@567 138 EPrim (Prim.String s) =>
adamc@567 139 (str ("\""
adamc@567 140 ^ String.translate (fn #"'" =>
adamc@568 141 if mode = Attribute then
adamc@567 142 "\\047"
adamc@567 143 else
adamc@567 144 "'"
adamc@577 145 | #"\"" => "\\\""
adamc@567 146 | #"<" =>
adamc@568 147 if mode = Script then
adamc@567 148 "<"
adamc@567 149 else
adamc@567 150 "\\074"
adamc@567 151 | #"\\" => "\\\\"
adamc@567 152 | ch => String.str ch) s
adamc@567 153 ^ "\""), st)
adamc@567 154 | EPrim p => (str (Prim.toString p), st)
adamc@567 155 | ERel n =>
adamc@567 156 if n < inner then
adamc@583 157 (str ("_" ^ var n), st)
adamc@567 158 else
adamc@577 159 let
adamc@577 160 val n = n - inner
adamc@577 161 in
adamc@579 162 (quoteExp (List.nth (outer, n)) (ERel (n - skip), loc), st)
adamc@577 163 end
adamc@567 164 | ENamed _ => raise Fail "Named"
adamc@567 165 | ECon (_, pc, NONE) => (patCon pc, st)
adamc@567 166 | ECon (_, pc, SOME e) =>
adamc@567 167 let
adamc@567 168 val (s, st) = jsE inner (e, st)
adamc@567 169 in
adamc@567 170 (strcat [str "{n:",
adamc@567 171 patCon pc,
adamc@567 172 str ",v:",
adamc@567 173 s,
adamc@567 174 str "}"], st)
adamc@567 175 end
adamc@567 176 | ENone _ => (str "null", st)
adamc@567 177 | ESome (t, e) =>
adamc@567 178 let
adamc@567 179 val (e, st) = jsE inner (e, st)
adamc@567 180 in
adamc@567 181 (if isNullable t then
adamc@567 182 strcat [str "{v:", e, str "}"]
adamc@567 183 else
adamc@567 184 e, st)
adamc@567 185 end
adamc@567 186
adamc@572 187 | EFfi k =>
adamc@567 188 let
adamc@572 189 val name = case ffi k of
adamc@574 190 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k ^ " in JavaScript");
adamc@572 191 "ERROR")
adamc@572 192 | SOME s => s
adamc@567 193 in
adamc@572 194 (str name, st)
adamc@567 195 end
adamc@572 196 | EFfiApp (m, x, args) =>
adamc@567 197 let
adamc@578 198 val args =
adamc@578 199 case (m, x, args) of
adamc@578 200 ("Basis", "new_client_source", [(EJavaScript (_, e, _), _)]) => [e]
adamc@578 201 | ("Basis", "set_client_source", [e1, (EJavaScript (_, e2, _), _)]) => [e1, e2]
adamc@578 202 | _ => args
adamc@578 203
adamc@572 204 val name = case ffi (m, x) of
adamc@574 205 NONE => (EM.errorAt loc ("Unsupported FFI function " ^ x ^ " in JavaScript");
adamc@572 206 "ERROR")
adamc@572 207 | SOME s => s
adamc@567 208 in
adamc@572 209 case args of
adamc@572 210 [] => (str (name ^ "()"), st)
adamc@572 211 | [e] =>
adamc@572 212 let
adamc@572 213 val (e, st) = jsE inner (e, st)
adamc@572 214 in
adamc@572 215 (strcat [str (name ^ "("),
adamc@572 216 e,
adamc@572 217 str ")"], st)
adamc@572 218 end
adamc@572 219 | e :: es =>
adamc@572 220 let
adamc@572 221 val (e, st) = jsE inner (e, st)
adamc@572 222 val (es, st) = ListUtil.foldlMapConcat
adamc@572 223 (fn (e, st) =>
adamc@572 224 let
adamc@572 225 val (e, st) = jsE inner (e, st)
adamc@572 226 in
adamc@572 227 ([str ",", e], st)
adamc@572 228 end)
adamc@572 229 st es
adamc@572 230 in
adamc@572 231 (strcat (str (name ^ "(")
adamc@572 232 :: e
adamc@572 233 :: es
adamc@572 234 @ [str ")"]), st)
adamc@572 235 end
adamc@567 236 end
adamc@567 237
adamc@567 238 | EApp (e1, e2) =>
adamc@567 239 let
adamc@567 240 val (e1, st) = jsE inner (e1, st)
adamc@567 241 val (e2, st) = jsE inner (e2, st)
adamc@567 242 in
adamc@567 243 (strcat [e1, str "(", e2, str ")"], st)
adamc@567 244 end
adamc@567 245 | EAbs (_, _, _, e) =>
adamc@567 246 let
adamc@567 247 val locals = List.tabulate
adamc@567 248 (varDepth e,
adamc@583 249 fn i => str ("var _" ^ Int.toString (len + inner + i) ^ ";"))
adamc@567 250 val (e, st) = jsE (inner + 1) (e, st)
adamc@567 251 in
adamc@583 252 (strcat (str ("function(_"
adamc@567 253 ^ Int.toString (len + inner)
adamc@567 254 ^ "){")
adamc@567 255 :: locals
adamc@567 256 @ [str "return ",
adamc@567 257 e,
adamc@567 258 str "}"]),
adamc@567 259 st)
adamc@567 260 end
adamc@567 261
adamc@567 262 | EUnop (s, e) =>
adamc@567 263 let
adamc@567 264 val (e, st) = jsE inner (e, st)
adamc@567 265 in
adamc@567 266 (strcat [str ("(" ^ s),
adamc@567 267 e,
adamc@567 268 str ")"],
adamc@567 269 st)
adamc@567 270 end
adamc@567 271 | EBinop (s, e1, e2) =>
adamc@567 272 let
adamc@567 273 val (e1, st) = jsE inner (e1, st)
adamc@567 274 val (e2, st) = jsE inner (e2, st)
adamc@567 275 in
adamc@567 276 (strcat [str "(",
adamc@567 277 e1,
adamc@567 278 str s,
adamc@567 279 e2,
adamc@567 280 str ")"],
adamc@567 281 st)
adamc@567 282 end
adamc@567 283
adamc@567 284 | ERecord [] => (str "null", st)
adamc@567 285 | ERecord [(x, e, _)] =>
adamc@567 286 let
adamc@567 287 val (e, st) = jsE inner (e, st)
adamc@567 288 in
adamc@583 289 (strcat [str "{_x:", e, str "}"], st)
adamc@567 290 end
adamc@567 291 | ERecord ((x, e, _) :: xes) =>
adamc@567 292 let
adamc@567 293 val (e, st) = jsE inner (e, st)
adamc@567 294
adamc@567 295 val (es, st) =
adamc@567 296 foldr (fn ((x, e, _), (es, st)) =>
adamc@567 297 let
adamc@567 298 val (e, st) = jsE inner (e, st)
adamc@567 299 in
adamc@583 300 (str (",_" ^ x ^ ":")
adamc@567 301 :: e
adamc@567 302 :: es,
adamc@567 303 st)
adamc@567 304 end)
adamc@567 305 ([str "}"], st) xes
adamc@567 306 in
adamc@583 307 (strcat (str ("{_" ^ x ^ ":")
adamc@567 308 :: e
adamc@567 309 :: es),
adamc@567 310 st)
adamc@567 311 end
adamc@567 312 | EField (e, x) =>
adamc@567 313 let
adamc@567 314 val (e, st) = jsE inner (e, st)
adamc@567 315 in
adamc@567 316 (strcat [e,
adamc@583 317 str ("._" ^ x)], st)
adamc@567 318 end
adamc@567 319
adamc@567 320 | ECase _ => raise Fail "Jscomp: ECase"
adamc@567 321
adamc@567 322 | EStrcat (e1, e2) =>
adamc@567 323 let
adamc@567 324 val (e1, st) = jsE inner (e1, st)
adamc@567 325 val (e2, st) = jsE inner (e2, st)
adamc@567 326 in
adamc@567 327 (strcat [str "(", e1, str "+", e2, str ")"], st)
adamc@567 328 end
adamc@567 329
adamc@567 330 | EError (e, _) =>
adamc@567 331 let
adamc@567 332 val (e, st) = jsE inner (e, st)
adamc@567 333 in
adamc@567 334 (strcat [str "alert(\"ERROR: \"+", e, str ")"],
adamc@567 335 st)
adamc@567 336 end
adamc@567 337
adamc@568 338 | EWrite e =>
adamc@568 339 let
adamc@568 340 val (e, st) = jsE inner (e, st)
adamc@568 341 in
adamc@568 342 (strcat [str "document.write(",
adamc@568 343 e,
adamc@569 344 str ".v)"], st)
adamc@568 345 end
adamc@567 346
adamc@567 347 | ESeq (e1, e2) =>
adamc@567 348 let
adamc@567 349 val (e1, st) = jsE inner (e1, st)
adamc@567 350 val (e2, st) = jsE inner (e2, st)
adamc@567 351 in
adamc@567 352 (strcat [str "(", e1, str ",", e2, str ")"], st)
adamc@567 353 end
adamc@567 354 | ELet (_, _, e1, e2) =>
adamc@567 355 let
adamc@567 356 val (e1, st) = jsE inner (e1, st)
adamc@567 357 val (e2, st) = jsE (inner + 1) (e2, st)
adamc@567 358 in
adamc@583 359 (strcat [str ("(_" ^ Int.toString (len + inner) ^ "="),
adamc@567 360 e1,
adamc@567 361 str ",",
adamc@567 362 e2,
adamc@567 363 str ")"], st)
adamc@567 364 end
adamc@567 365
adamc@567 366 | EClosure _ => unsupported "EClosure"
adamc@567 367 | EQuery _ => unsupported "Query"
adamc@567 368 | EDml _ => unsupported "DML"
adamc@567 369 | ENextval _ => unsupported "Nextval"
adamc@567 370 | EUnurlify _ => unsupported "EUnurlify"
adamc@567 371 | EJavaScript _ => unsupported "Nested JavaScript"
adamc@568 372 | ESignalReturn e =>
adamc@568 373 let
adamc@568 374 val (e, st) = jsE inner (e, st)
adamc@568 375 in
adamc@572 376 (strcat [str "sr(",
adamc@569 377 e,
adamc@569 378 str ")"],
adamc@568 379 st)
adamc@568 380 end
adamc@572 381 | ESignalBind (e1, e2) =>
adamc@572 382 let
adamc@572 383 val (e1, st) = jsE inner (e1, st)
adamc@572 384 val (e2, st) = jsE inner (e2, st)
adamc@572 385 in
adamc@572 386 (strcat [str "sb(",
adamc@572 387 e1,
adamc@572 388 str ",",
adamc@572 389 e2,
adamc@572 390 str ")"],
adamc@572 391 st)
adamc@572 392 end
adamc@574 393 | ESignalSource e =>
adamc@574 394 let
adamc@574 395 val (e, st) = jsE inner (e, st)
adamc@574 396 in
adamc@574 397 (strcat [str "ss(",
adamc@574 398 e,
adamc@574 399 str ")"],
adamc@574 400 st)
adamc@574 401 end
adamc@567 402 end
adamc@567 403 in
adamc@567 404 jsE
adamc@567 405 end
adamc@567 406
adamc@567 407 val decl : state -> decl -> decl * state =
adamc@567 408 U.Decl.foldMapB {typ = fn x => x,
adamc@567 409 exp = fn (env, e, st) =>
adamc@568 410 let
adamc@579 411 fun doCode m skip env orig e =
adamc@568 412 let
adamc@568 413 val len = length env
adamc@568 414 fun str s = (EPrim (Prim.String s), #2 e)
adamc@568 415
adamc@568 416 val locals = List.tabulate
adamc@568 417 (varDepth e,
adamc@583 418 fn i => str ("var _" ^ Int.toString (len + i) ^ ";"))
adamc@579 419 val (e, st) = jsExp m skip env 0 (e, st)
adamc@568 420 in
adamc@578 421 (EJavaScript (m, orig, SOME (strcat (#2 e) (locals @ [e]))), st)
adamc@568 422 end
adamc@568 423 in
adamc@568 424 case e of
adamc@579 425 EJavaScript (m, orig as (EAbs (_, t, _, e), _), _) => doCode m 1 (t :: env) orig e
adamc@579 426 | EJavaScript (m, e, _) => doCode m 0 env e e
adamc@568 427 | _ => (e, st)
adamc@568 428 end,
adamc@567 429 decl = fn (_, e, st) => (e, st),
adamc@567 430 bind = fn (env, U.Decl.RelE (_, t)) => t :: env
adamc@567 431 | (env, _) => env}
adamc@567 432 []
adamc@567 433
adamc@567 434 fun process file =
adamc@567 435 let
adamc@567 436 fun doDecl (d, st) =
adamc@567 437 let
adamc@567 438 val (d, st) = decl st d
adamc@567 439 in
adamc@567 440 (List.revAppend (#decls st, [d]),
adamc@567 441 {decls = [],
adamc@567 442 script = #script st})
adamc@567 443 end
adamc@567 444
adamc@567 445 val (ds, st) = ListUtil.foldlMapConcat doDecl
adamc@567 446 {decls = [],
adamc@567 447 script = ""}
adamc@567 448 file
adamc@569 449
adamc@569 450 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
adamc@569 451 fun lines acc =
adamc@569 452 case TextIO.inputLine inf of
adamc@569 453 NONE => String.concat (rev acc)
adamc@569 454 | SOME line => lines (line :: acc)
adamc@569 455 val lines = lines []
adamc@567 456 in
adamc@569 457 TextIO.closeIn inf;
adamc@569 458 (DJavaScript lines, ErrorMsg.dummySpan) :: ds
adamc@567 459 end
adamc@567 460
adamc@567 461 end