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@589
|
36 structure IS = IntBinarySet
|
adamc@589
|
37 structure IM = IntBinaryMap
|
adamc@589
|
38
|
adamc@572
|
39 val funcs = [(("Basis", "alert"), "alert"),
|
adamc@586
|
40 (("Basis", "htmlifyBool"), "bs"),
|
adamc@583
|
41 (("Basis", "htmlifyFloat"), "ts"),
|
adamc@582
|
42 (("Basis", "htmlifyInt"), "ts"),
|
adamc@574
|
43 (("Basis", "htmlifyString"), "escape"),
|
adamc@579
|
44 (("Basis", "new_client_source"), "sc"),
|
adamc@579
|
45 (("Basis", "set_client_source"), "sv")]
|
adamc@572
|
46
|
adamc@572
|
47 structure FM = BinaryMapFn(struct
|
adamc@572
|
48 type ord_key = string * string
|
adamc@572
|
49 fun compare ((m1, x1), (m2, x2)) =
|
adamc@572
|
50 Order.join (String.compare (m1, m2),
|
adamc@572
|
51 fn () => String.compare (x1, x2))
|
adamc@572
|
52 end)
|
adamc@572
|
53
|
adamc@572
|
54 val funcs = foldl (fn ((k, v), m) => FM.insert (m, k, v)) FM.empty funcs
|
adamc@572
|
55
|
adamc@572
|
56 fun ffi k = FM.find (funcs, k)
|
adamc@572
|
57
|
adamc@567
|
58 type state = {
|
adamc@567
|
59 decls : decl list,
|
adamc@589
|
60 script : string list,
|
adamc@589
|
61 included : IS.set
|
adamc@567
|
62 }
|
adamc@567
|
63
|
adamc@567
|
64 fun varDepth (e, _) =
|
adamc@567
|
65 case e of
|
adamc@567
|
66 EPrim _ => 0
|
adamc@567
|
67 | ERel _ => 0
|
adamc@567
|
68 | ENamed _ => 0
|
adamc@567
|
69 | ECon (_, _, NONE) => 0
|
adamc@567
|
70 | ECon (_, _, SOME e) => varDepth e
|
adamc@567
|
71 | ENone _ => 0
|
adamc@567
|
72 | ESome (_, e) => varDepth e
|
adamc@567
|
73 | EFfi _ => 0
|
adamc@567
|
74 | EFfiApp (_, _, es) => foldl Int.max 0 (map varDepth es)
|
adamc@567
|
75 | EApp (e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@567
|
76 | EAbs _ => 0
|
adamc@567
|
77 | EUnop (_, e) => varDepth e
|
adamc@567
|
78 | EBinop (_, e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@567
|
79 | ERecord xes => foldl Int.max 0 (map (fn (_, e, _) => varDepth e) xes)
|
adamc@567
|
80 | EField (e, _) => varDepth e
|
adamc@567
|
81 | ECase (e, pes, _) =>
|
adamc@567
|
82 foldl Int.max (varDepth e)
|
adamc@567
|
83 (map (fn (p, e) => E.patBindsN p + varDepth e) pes)
|
adamc@567
|
84 | EStrcat (e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@567
|
85 | EError (e, _) => varDepth e
|
adamc@567
|
86 | EWrite e => varDepth e
|
adamc@567
|
87 | ESeq (e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@567
|
88 | ELet (_, _, e1, e2) => Int.max (varDepth e1, 1 + varDepth e2)
|
adamc@567
|
89 | EClosure _ => 0
|
adamc@567
|
90 | EQuery _ => 0
|
adamc@567
|
91 | EDml _ => 0
|
adamc@567
|
92 | ENextval _ => 0
|
adamc@567
|
93 | EUnurlify _ => 0
|
adamc@567
|
94 | EJavaScript _ => 0
|
adamc@568
|
95 | ESignalReturn e => varDepth e
|
adamc@572
|
96 | ESignalBind (e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@574
|
97 | ESignalSource e => varDepth e
|
adamc@567
|
98
|
adamc@568
|
99 fun strcat loc es =
|
adamc@568
|
100 case es of
|
adamc@568
|
101 [] => (EPrim (Prim.String ""), loc)
|
adamc@568
|
102 | [x] => x
|
adamc@568
|
103 | x :: es' => (EStrcat (x, strcat loc es'), loc)
|
adamc@568
|
104
|
adamc@590
|
105 exception Unsupported of string * EM.span
|
adamc@590
|
106
|
adamc@589
|
107 fun process file =
|
adamc@567
|
108 let
|
adamc@589
|
109 val nameds = foldl (fn ((DVal (_, n, t, e, _), _), nameds) => IM.insert (nameds, n, e)
|
adamc@589
|
110 | ((DValRec vis, _), nameds) =>
|
adamc@589
|
111 foldl (fn ((_, n, _, e, _), nameds) => IM.insert (nameds, n, e))
|
adamc@589
|
112 nameds vis
|
adamc@589
|
113 | (_, nameds) => nameds)
|
adamc@589
|
114 IM.empty file
|
adamc@567
|
115
|
adamc@590
|
116 fun str loc s = (EPrim (Prim.String s), loc)
|
adamc@590
|
117
|
adamc@590
|
118 fun quoteExp loc (t : typ) e =
|
adamc@590
|
119 case #1 t of
|
adamc@590
|
120 TSource => strcat loc [str loc "s",
|
adamc@590
|
121 (EFfiApp ("Basis", "htmlifyInt", [e]), loc)]
|
adamc@590
|
122 | TRecord [] => str loc "null"
|
adamc@590
|
123
|
adamc@590
|
124 | TFfi ("Basis", "string") => e
|
adamc@590
|
125 | TFfi ("Basis", "int") => (EFfiApp ("Basis", "htmlifyInt", [e]), loc)
|
adamc@590
|
126
|
adamc@590
|
127 | _ => (EM.errorAt loc "Don't know how to embed type in JavaScript";
|
adamc@590
|
128 Print.prefaces "Can't embed" [("t", MonoPrint.p_typ MonoEnv.empty t)];
|
adamc@590
|
129 str loc "ERROR")
|
adamc@590
|
130
|
adamc@589
|
131 fun jsExp mode skip outer =
|
adamc@567
|
132 let
|
adamc@589
|
133 val len = length outer
|
adamc@567
|
134
|
adamc@589
|
135 fun jsE inner (e as (_, loc), st) =
|
adamc@589
|
136 let
|
adamc@590
|
137 val str = str loc
|
adamc@567
|
138
|
adamc@589
|
139 fun var n = Int.toString (len + inner - n - 1)
|
adamc@567
|
140
|
adamc@589
|
141 fun patCon pc =
|
adamc@589
|
142 case pc of
|
adamc@589
|
143 PConVar n => str (Int.toString n)
|
adamc@589
|
144 | PConFfi {mod = "Basis", con = "True", ...} => str "true"
|
adamc@589
|
145 | PConFfi {mod = "Basis", con = "False", ...} => str "false"
|
adamc@589
|
146 | PConFfi {con, ...} => str ("\"_" ^ con ^ "\"")
|
adamc@567
|
147
|
adamc@589
|
148 fun isNullable (t, _) =
|
adamc@589
|
149 case t of
|
adamc@589
|
150 TOption _ => true
|
adamc@589
|
151 | TRecord [] => true
|
adamc@589
|
152 | _ => false
|
adamc@568
|
153
|
adamc@590
|
154 fun unsupported s = raise Unsupported (s, loc)
|
adamc@577
|
155
|
adamc@589
|
156 val strcat = strcat loc
|
adamc@584
|
157
|
adamc@589
|
158 fun jsPrim p =
|
adamc@589
|
159 case p of
|
adamc@589
|
160 Prim.String s =>
|
adamc@589
|
161 str ("\""
|
adamc@589
|
162 ^ String.translate (fn #"'" =>
|
adamc@589
|
163 if mode = Attribute then
|
adamc@589
|
164 "\\047"
|
adamc@589
|
165 else
|
adamc@589
|
166 "'"
|
adamc@589
|
167 | #"\"" => "\\\""
|
adamc@589
|
168 | #"<" =>
|
adamc@589
|
169 if mode = Script then
|
adamc@589
|
170 "<"
|
adamc@589
|
171 else
|
adamc@589
|
172 "\\074"
|
adamc@589
|
173 | #"\\" => "\\\\"
|
adamc@589
|
174 | ch => String.str ch) s
|
adamc@589
|
175 ^ "\"")
|
adamc@589
|
176 | _ => str (Prim.toString p)
|
adamc@589
|
177
|
adamc@589
|
178 fun jsPat depth inner (p, _) succ fail =
|
adamc@589
|
179 case p of
|
adamc@589
|
180 PWild => succ
|
adamc@589
|
181 | PVar _ => strcat [str ("(_" ^ Int.toString (len + inner) ^ "=d"
|
adamc@589
|
182 ^ Int.toString depth ^ ","),
|
adamc@589
|
183 succ,
|
adamc@589
|
184 str ")"]
|
adamc@589
|
185 | PPrim p => strcat [str ("(d" ^ Int.toString depth ^ "=="),
|
adamc@589
|
186 jsPrim p,
|
adamc@589
|
187 str "?",
|
adamc@589
|
188 succ,
|
adamc@589
|
189 str ":",
|
adamc@589
|
190 fail,
|
adamc@589
|
191 str ")"]
|
adamc@589
|
192 | PCon (_, PConFfi {mod = "Basis", con = "True", ...}, NONE) =>
|
adamc@589
|
193 strcat [str ("(d" ^ Int.toString depth ^ "?"),
|
adamc@589
|
194 succ,
|
adamc@589
|
195 str ":",
|
adamc@589
|
196 fail,
|
adamc@589
|
197 str ")"]
|
adamc@589
|
198 | PCon (_, PConFfi {mod = "Basis", con = "False", ...}, NONE) =>
|
adamc@589
|
199 strcat [str ("(d" ^ Int.toString depth ^ "?"),
|
adamc@589
|
200 fail,
|
adamc@589
|
201 str ":",
|
adamc@589
|
202 succ,
|
adamc@589
|
203 str ")"]
|
adamc@589
|
204 | PCon (_, pc, NONE) =>
|
adamc@589
|
205 strcat [str ("(d" ^ Int.toString depth ^ "=="),
|
adamc@589
|
206 patCon pc,
|
adamc@589
|
207 str "?",
|
adamc@589
|
208 succ,
|
adamc@589
|
209 str ":",
|
adamc@589
|
210 fail,
|
adamc@589
|
211 str ")"]
|
adamc@589
|
212 | PCon (_, pc, SOME p) =>
|
adamc@589
|
213 strcat [str ("(d" ^ Int.toString depth ^ ".n=="),
|
adamc@589
|
214 patCon pc,
|
adamc@589
|
215 str ("?(d" ^ Int.toString depth ^ "=d" ^ Int.toString depth ^ ".v,"),
|
adamc@589
|
216 jsPat depth inner p succ fail,
|
adamc@589
|
217 str "):",
|
adamc@589
|
218 fail,
|
adamc@589
|
219 str ")"]
|
adamc@589
|
220 | PRecord xps =>
|
adamc@589
|
221 let
|
adamc@589
|
222 val (_, succ) = foldl
|
adamc@589
|
223 (fn ((x, p, _), (inner, succ)) =>
|
adamc@589
|
224 (inner + E.patBindsN p,
|
adamc@589
|
225 strcat [str ("(d" ^ Int.toString (depth+1) ^ "=d"
|
adamc@589
|
226 ^ Int.toString depth ^ "._" ^ x ^ ","),
|
adamc@589
|
227 jsPat (depth+1) inner p succ fail,
|
adamc@589
|
228 str ")"]))
|
adamc@589
|
229 (inner, succ) xps
|
adamc@589
|
230 in
|
adamc@589
|
231 succ
|
adamc@589
|
232 end
|
adamc@589
|
233 | PNone _ => strcat [str ("(d" ^ Int.toString depth ^ "?"),
|
adamc@589
|
234 fail,
|
adamc@589
|
235 str ":",
|
adamc@589
|
236 succ,
|
adamc@589
|
237 str ")"]
|
adamc@589
|
238 | PSome (_, p) => strcat [str ("(d" ^ Int.toString depth ^ "?"),
|
adamc@589
|
239 jsPat depth inner p succ fail,
|
adamc@589
|
240 str ":",
|
adamc@589
|
241 fail,
|
adamc@589
|
242 str ")"]
|
adamc@589
|
243
|
adamc@589
|
244 fun deStrcat (e, _) =
|
adamc@589
|
245 case e of
|
adamc@589
|
246 EPrim (Prim.String s) => s
|
adamc@589
|
247 | EStrcat (e1, e2) => deStrcat e1 ^ deStrcat e2
|
adamc@589
|
248 | _ => raise Fail "Jscomp: deStrcat"
|
adamc@590
|
249
|
adamc@590
|
250 val quoteExp = quoteExp loc
|
adamc@567
|
251 in
|
adamc@590
|
252 (*Print.prefaces "jsE" [("e", MonoPrint.p_exp MonoEnv.empty e)];*)
|
adamc@590
|
253
|
adamc@589
|
254 case #1 e of
|
adamc@589
|
255 EPrim p => (jsPrim p, st)
|
adamc@589
|
256 | ERel n =>
|
adamc@589
|
257 if n < inner then
|
adamc@589
|
258 (str ("_" ^ var n), st)
|
adamc@589
|
259 else
|
adamc@589
|
260 let
|
adamc@589
|
261 val n = n - inner
|
adamc@589
|
262 in
|
adamc@589
|
263 (quoteExp (List.nth (outer, n)) (ERel (n - skip), loc), st)
|
adamc@589
|
264 end
|
adamc@567
|
265
|
adamc@589
|
266 | ENamed n =>
|
adamc@589
|
267 let
|
adamc@589
|
268 val st =
|
adamc@589
|
269 if IS.member (#included st, n) then
|
adamc@589
|
270 st
|
adamc@589
|
271 else
|
adamc@589
|
272 case IM.find (nameds, n) of
|
adamc@589
|
273 NONE => raise Fail "Jscomp: Unbound ENamed"
|
adamc@589
|
274 | SOME e =>
|
adamc@589
|
275 let
|
adamc@589
|
276 val st = {decls = #decls st,
|
adamc@589
|
277 script = #script st,
|
adamc@589
|
278 included = IS.add (#included st, n)}
|
adamc@578
|
279
|
adamc@589
|
280 val (e, st) = jsExp mode skip [] 0 (e, st)
|
adamc@589
|
281 val e = deStrcat e
|
adamc@589
|
282
|
adamc@589
|
283 val sc = "_n" ^ Int.toString n ^ "=" ^ e ^ ";\n"
|
adamc@589
|
284 in
|
adamc@589
|
285 {decls = #decls st,
|
adamc@589
|
286 script = sc :: #script st,
|
adamc@589
|
287 included = #included st}
|
adamc@589
|
288 end
|
adamc@589
|
289 in
|
adamc@589
|
290 (str ("_n" ^ Int.toString n), st)
|
adamc@589
|
291 end
|
adamc@589
|
292
|
adamc@589
|
293 | ECon (_, pc, NONE) => (patCon pc, st)
|
adamc@589
|
294 | ECon (_, pc, SOME e) =>
|
adamc@589
|
295 let
|
adamc@589
|
296 val (s, st) = jsE inner (e, st)
|
adamc@589
|
297 in
|
adamc@589
|
298 (strcat [str "{n:",
|
adamc@589
|
299 patCon pc,
|
adamc@589
|
300 str ",v:",
|
adamc@589
|
301 s,
|
adamc@589
|
302 str "}"], st)
|
adamc@589
|
303 end
|
adamc@589
|
304 | ENone _ => (str "null", st)
|
adamc@589
|
305 | ESome (t, e) =>
|
adamc@572
|
306 let
|
adamc@572
|
307 val (e, st) = jsE inner (e, st)
|
adamc@572
|
308 in
|
adamc@589
|
309 (if isNullable t then
|
adamc@589
|
310 strcat [str "{v:", e, str "}"]
|
adamc@589
|
311 else
|
adamc@589
|
312 e, st)
|
adamc@589
|
313 end
|
adamc@589
|
314
|
adamc@589
|
315 | EFfi k =>
|
adamc@589
|
316 let
|
adamc@589
|
317 val name = case ffi k of
|
adamc@589
|
318 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k
|
adamc@589
|
319 ^ " in JavaScript");
|
adamc@589
|
320 "ERROR")
|
adamc@589
|
321 | SOME s => s
|
adamc@589
|
322 in
|
adamc@589
|
323 (str name, st)
|
adamc@589
|
324 end
|
adamc@589
|
325 | EFfiApp (m, x, args) =>
|
adamc@589
|
326 let
|
adamc@589
|
327 val args =
|
adamc@589
|
328 case (m, x, args) of
|
adamc@589
|
329 ("Basis", "new_client_source", [(EJavaScript (_, e, _), _)]) => [e]
|
adamc@589
|
330 | ("Basis", "set_client_source", [e1, (EJavaScript (_, e2, _), _)]) => [e1, e2]
|
adamc@589
|
331 | _ => args
|
adamc@589
|
332
|
adamc@589
|
333 val name = case ffi (m, x) of
|
adamc@589
|
334 NONE => (EM.errorAt loc ("Unsupported FFI function "
|
adamc@589
|
335 ^ x ^ " in JavaScript");
|
adamc@589
|
336 "ERROR")
|
adamc@589
|
337 | SOME s => s
|
adamc@589
|
338 in
|
adamc@589
|
339 case args of
|
adamc@589
|
340 [] => (str (name ^ "()"), st)
|
adamc@589
|
341 | [e] =>
|
adamc@589
|
342 let
|
adamc@589
|
343 val (e, st) = jsE inner (e, st)
|
adamc@589
|
344 in
|
adamc@589
|
345 (strcat [str (name ^ "("),
|
adamc@589
|
346 e,
|
adamc@589
|
347 str ")"], st)
|
adamc@589
|
348 end
|
adamc@589
|
349 | e :: es =>
|
adamc@589
|
350 let
|
adamc@589
|
351 val (e, st) = jsE inner (e, st)
|
adamc@589
|
352 val (es, st) = ListUtil.foldlMapConcat
|
adamc@589
|
353 (fn (e, st) =>
|
adamc@589
|
354 let
|
adamc@589
|
355 val (e, st) = jsE inner (e, st)
|
adamc@589
|
356 in
|
adamc@589
|
357 ([str ",", e], st)
|
adamc@589
|
358 end)
|
adamc@589
|
359 st es
|
adamc@589
|
360 in
|
adamc@589
|
361 (strcat (str (name ^ "(")
|
adamc@589
|
362 :: e
|
adamc@589
|
363 :: es
|
adamc@589
|
364 @ [str ")"]), st)
|
adamc@589
|
365 end
|
adamc@589
|
366 end
|
adamc@589
|
367
|
adamc@589
|
368 | EApp (e1, e2) =>
|
adamc@589
|
369 let
|
adamc@589
|
370 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
371 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
372 in
|
adamc@589
|
373 (strcat [e1, str "(", e2, str ")"], st)
|
adamc@589
|
374 end
|
adamc@589
|
375 | EAbs (_, _, _, e) =>
|
adamc@589
|
376 let
|
adamc@589
|
377 val locals = List.tabulate
|
adamc@589
|
378 (varDepth e,
|
adamc@589
|
379 fn i => str ("var _" ^ Int.toString (len + inner + i + 1) ^ ";"))
|
adamc@589
|
380 val (e, st) = jsE (inner + 1) (e, st)
|
adamc@589
|
381 in
|
adamc@589
|
382 (strcat (str ("function(_"
|
adamc@589
|
383 ^ Int.toString (len + inner)
|
adamc@589
|
384 ^ "){")
|
adamc@589
|
385 :: locals
|
adamc@589
|
386 @ [str "return ",
|
adamc@589
|
387 e,
|
adamc@589
|
388 str "}"]),
|
adamc@589
|
389 st)
|
adamc@589
|
390 end
|
adamc@589
|
391
|
adamc@589
|
392 | EUnop (s, e) =>
|
adamc@589
|
393 let
|
adamc@589
|
394 val (e, st) = jsE inner (e, st)
|
adamc@589
|
395 in
|
adamc@589
|
396 (strcat [str ("(" ^ s),
|
adamc@572
|
397 e,
|
adamc@589
|
398 str ")"],
|
adamc@589
|
399 st)
|
adamc@589
|
400 end
|
adamc@589
|
401 | EBinop (s, e1, e2) =>
|
adamc@589
|
402 let
|
adamc@589
|
403 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
404 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
405 in
|
adamc@589
|
406 (strcat [str "(",
|
adamc@589
|
407 e1,
|
adamc@589
|
408 str s,
|
adamc@589
|
409 e2,
|
adamc@589
|
410 str ")"],
|
adamc@589
|
411 st)
|
adamc@589
|
412 end
|
adamc@589
|
413
|
adamc@589
|
414 | ERecord [] => (str "null", st)
|
adamc@589
|
415 | ERecord [(x, e, _)] =>
|
adamc@589
|
416 let
|
adamc@589
|
417 val (e, st) = jsE inner (e, st)
|
adamc@589
|
418 in
|
adamc@589
|
419 (strcat [str "{_x:", e, str "}"], st)
|
adamc@589
|
420 end
|
adamc@589
|
421 | ERecord ((x, e, _) :: xes) =>
|
adamc@589
|
422 let
|
adamc@589
|
423 val (e, st) = jsE inner (e, st)
|
adamc@589
|
424
|
adamc@589
|
425 val (es, st) =
|
adamc@589
|
426 foldr (fn ((x, e, _), (es, st)) =>
|
adamc@589
|
427 let
|
adamc@589
|
428 val (e, st) = jsE inner (e, st)
|
adamc@589
|
429 in
|
adamc@589
|
430 (str (",_" ^ x ^ ":")
|
adamc@589
|
431 :: e
|
adamc@589
|
432 :: es,
|
adamc@589
|
433 st)
|
adamc@589
|
434 end)
|
adamc@589
|
435 ([str "}"], st) xes
|
adamc@589
|
436 in
|
adamc@589
|
437 (strcat (str ("{_" ^ x ^ ":")
|
adamc@589
|
438 :: e
|
adamc@589
|
439 :: es),
|
adamc@589
|
440 st)
|
adamc@589
|
441 end
|
adamc@589
|
442 | EField (e, x) =>
|
adamc@589
|
443 let
|
adamc@589
|
444 val (e, st) = jsE inner (e, st)
|
adamc@589
|
445 in
|
adamc@589
|
446 (strcat [e,
|
adamc@589
|
447 str ("._" ^ x)], st)
|
adamc@589
|
448 end
|
adamc@589
|
449
|
adamc@589
|
450 | ECase (e, pes, _) =>
|
adamc@589
|
451 let
|
adamc@589
|
452 val plen = length pes
|
adamc@589
|
453
|
adamc@589
|
454 val (cases, st) = ListUtil.foldliMap
|
adamc@589
|
455 (fn (i, (p, e), st) =>
|
adamc@589
|
456 let
|
adamc@589
|
457 val (e, st) = jsE (inner + E.patBindsN p) (e, st)
|
adamc@589
|
458 val fail =
|
adamc@589
|
459 if i = plen - 1 then
|
adamc@589
|
460 str "pf()"
|
adamc@589
|
461 else
|
adamc@589
|
462 str ("c" ^ Int.toString (i+1) ^ "()")
|
adamc@589
|
463 val c = jsPat 0 inner p e fail
|
adamc@589
|
464 in
|
adamc@589
|
465 (strcat [str ("c" ^ Int.toString i ^ "=function(){return "),
|
adamc@589
|
466 c,
|
adamc@589
|
467 str "},"],
|
adamc@589
|
468 st)
|
adamc@589
|
469 end)
|
adamc@589
|
470 st pes
|
adamc@589
|
471
|
adamc@589
|
472 val (e, st) = jsE inner (e, st)
|
adamc@589
|
473 in
|
adamc@589
|
474 (strcat (str "("
|
adamc@589
|
475 :: List.revAppend (cases,
|
adamc@589
|
476 [str "d0=",
|
adamc@589
|
477 e,
|
adamc@589
|
478 str ",c0())"])), st)
|
adamc@589
|
479 end
|
adamc@589
|
480
|
adamc@589
|
481 | EStrcat (e1, e2) =>
|
adamc@589
|
482 let
|
adamc@589
|
483 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
484 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
485 in
|
adamc@589
|
486 (strcat [str "(", e1, str "+", e2, str ")"], st)
|
adamc@589
|
487 end
|
adamc@589
|
488
|
adamc@589
|
489 | EError (e, _) =>
|
adamc@589
|
490 let
|
adamc@589
|
491 val (e, st) = jsE inner (e, st)
|
adamc@589
|
492 in
|
adamc@589
|
493 (strcat [str "alert(\"ERROR: \"+", e, str ")"],
|
adamc@589
|
494 st)
|
adamc@589
|
495 end
|
adamc@589
|
496
|
adamc@589
|
497 | EWrite e =>
|
adamc@589
|
498 let
|
adamc@589
|
499 val (e, st) = jsE inner (e, st)
|
adamc@589
|
500 in
|
adamc@589
|
501 (strcat [str "document.write(",
|
adamc@589
|
502 e,
|
adamc@589
|
503 str ".v)"], st)
|
adamc@589
|
504 end
|
adamc@589
|
505
|
adamc@589
|
506 | ESeq (e1, e2) =>
|
adamc@589
|
507 let
|
adamc@589
|
508 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
509 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
510 in
|
adamc@589
|
511 (strcat [str "(", e1, str ",", e2, str ")"], st)
|
adamc@589
|
512 end
|
adamc@589
|
513 | ELet (_, _, e1, e2) =>
|
adamc@589
|
514 let
|
adamc@589
|
515 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
516 val (e2, st) = jsE (inner + 1) (e2, st)
|
adamc@589
|
517 in
|
adamc@589
|
518 (strcat [str ("(_" ^ Int.toString (len + inner) ^ "="),
|
adamc@589
|
519 e1,
|
adamc@589
|
520 str ",",
|
adamc@589
|
521 e2,
|
adamc@572
|
522 str ")"], st)
|
adamc@572
|
523 end
|
adamc@589
|
524
|
adamc@590
|
525 | EJavaScript (_, _, SOME e) => (e, st)
|
adamc@590
|
526
|
adamc@589
|
527 | EClosure _ => unsupported "EClosure"
|
adamc@589
|
528 | EQuery _ => unsupported "Query"
|
adamc@589
|
529 | EDml _ => unsupported "DML"
|
adamc@589
|
530 | ENextval _ => unsupported "Nextval"
|
adamc@589
|
531 | EUnurlify _ => unsupported "EUnurlify"
|
adamc@590
|
532 | EJavaScript (_, e, _) => unsupported "Nested JavaScript"
|
adamc@590
|
533
|
adamc@589
|
534 | ESignalReturn e =>
|
adamc@572
|
535 let
|
adamc@572
|
536 val (e, st) = jsE inner (e, st)
|
adamc@572
|
537 in
|
adamc@589
|
538 (strcat [str "sr(",
|
adamc@589
|
539 e,
|
adamc@589
|
540 str ")"],
|
adamc@589
|
541 st)
|
adamc@589
|
542 end
|
adamc@589
|
543 | ESignalBind (e1, e2) =>
|
adamc@589
|
544 let
|
adamc@589
|
545 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
546 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
547 in
|
adamc@589
|
548 (strcat [str "sb(",
|
adamc@589
|
549 e1,
|
adamc@589
|
550 str ",",
|
adamc@589
|
551 e2,
|
adamc@589
|
552 str ")"],
|
adamc@589
|
553 st)
|
adamc@589
|
554 end
|
adamc@589
|
555 | ESignalSource e =>
|
adamc@589
|
556 let
|
adamc@589
|
557 val (e, st) = jsE inner (e, st)
|
adamc@589
|
558 in
|
adamc@589
|
559 (strcat [str "ss(",
|
adamc@589
|
560 e,
|
adamc@589
|
561 str ")"],
|
adamc@589
|
562 st)
|
adamc@572
|
563 end
|
adamc@567
|
564 end
|
adamc@589
|
565 in
|
adamc@589
|
566 jsE
|
adamc@589
|
567 end
|
adamc@567
|
568
|
adamc@589
|
569 val decl : state -> decl -> decl * state =
|
adamc@589
|
570 U.Decl.foldMapB {typ = fn x => x,
|
adamc@589
|
571 exp = fn (env, e, st) =>
|
adamc@589
|
572 let
|
adamc@589
|
573 fun doCode m skip env orig e =
|
adamc@589
|
574 let
|
adamc@589
|
575 val len = length env
|
adamc@589
|
576 fun str s = (EPrim (Prim.String s), #2 e)
|
adamc@567
|
577
|
adamc@589
|
578 val locals = List.tabulate
|
adamc@589
|
579 (varDepth e,
|
adamc@589
|
580 fn i => str ("var _" ^ Int.toString (len + i) ^ ";"))
|
adamc@589
|
581 val (e, st) = jsExp m skip env 0 (e, st)
|
adamc@589
|
582 in
|
adamc@589
|
583 (EJavaScript (m, orig, SOME (strcat (#2 e) (locals @ [e]))), st)
|
adamc@589
|
584 end
|
adamc@589
|
585 in
|
adamc@589
|
586 case e of
|
adamc@590
|
587 EJavaScript (m as Source t, orig, _) =>
|
adamc@590
|
588 (doCode m 0 env orig orig
|
adamc@590
|
589 handle Unsupported (s, loc) =>
|
adamc@590
|
590 let
|
adamc@590
|
591 val e = ELet ("js", t, orig, quoteExp (#2 orig) t
|
adamc@590
|
592 (ERel 0, #2 orig))
|
adamc@590
|
593 in
|
adamc@590
|
594 (EJavaScript (m, orig, SOME (e, #2 orig)), st)
|
adamc@590
|
595 end)
|
adamc@590
|
596
|
adamc@590
|
597 | EJavaScript (m, orig as (EAbs (_, t, _, e), _), _) =>
|
adamc@590
|
598 (doCode m 1 (t :: env) orig e
|
adamc@590
|
599 handle Unsupported (s, loc) =>
|
adamc@590
|
600 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript");
|
adamc@590
|
601 (EPrim (Prim.String "ERROR"), st)))
|
adamc@590
|
602
|
adamc@590
|
603 | EJavaScript (m, orig, _) =>
|
adamc@590
|
604 (doCode m 0 env orig orig
|
adamc@590
|
605 handle Unsupported (s, loc) =>
|
adamc@590
|
606 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript");
|
adamc@590
|
607 (EPrim (Prim.String "ERROR"), st)))
|
adamc@590
|
608
|
adamc@589
|
609 | _ => (e, st)
|
adamc@589
|
610 end,
|
adamc@589
|
611 decl = fn (_, e, st) => (e, st),
|
adamc@589
|
612 bind = fn (env, U.Decl.RelE (_, t)) => t :: env
|
adamc@589
|
613 | (env, _) => env}
|
adamc@589
|
614 []
|
adamc@567
|
615
|
adamc@567
|
616 fun doDecl (d, st) =
|
adamc@567
|
617 let
|
adamc@567
|
618 val (d, st) = decl st d
|
adamc@567
|
619 in
|
adamc@567
|
620 (List.revAppend (#decls st, [d]),
|
adamc@567
|
621 {decls = [],
|
adamc@589
|
622 script = #script st,
|
adamc@589
|
623 included = #included st})
|
adamc@567
|
624 end
|
adamc@567
|
625
|
adamc@567
|
626 val (ds, st) = ListUtil.foldlMapConcat doDecl
|
adamc@567
|
627 {decls = [],
|
adamc@589
|
628 script = [],
|
adamc@589
|
629 included = IS.empty}
|
adamc@567
|
630 file
|
adamc@569
|
631
|
adamc@569
|
632 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
|
adamc@569
|
633 fun lines acc =
|
adamc@569
|
634 case TextIO.inputLine inf of
|
adamc@569
|
635 NONE => String.concat (rev acc)
|
adamc@569
|
636 | SOME line => lines (line :: acc)
|
adamc@569
|
637 val lines = lines []
|
adamc@567
|
638 in
|
adamc@569
|
639 TextIO.closeIn inf;
|
adamc@589
|
640 (DJavaScript (lines ^ String.concat (rev (#script st))), ErrorMsg.dummySpan) :: ds
|
adamc@567
|
641 end
|
adamc@567
|
642
|
adamc@567
|
643 end
|