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