annotate src/jscomp.sml @ 567:1901db85acb4

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