annotate src/jscomp.sml @ 572:57018f21cd5c

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