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