annotate src/jscomp.sml @ 576:813f1e78d9d0

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