annotate src/jscomp.sml @ 568:55fc747a67dc

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