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@800
|
39 structure TM = BinaryMapFn(struct
|
adamc@800
|
40 type ord_key = typ
|
adamc@800
|
41 val compare = U.Typ.compare
|
adamc@800
|
42 end)
|
adamc@800
|
43
|
adamc@567
|
44 type state = {
|
adamc@567
|
45 decls : decl list,
|
adamc@589
|
46 script : string list,
|
adamc@595
|
47 included : IS.set,
|
adamc@595
|
48 injectors : int IM.map,
|
adamc@800
|
49 listInjectors : int TM.map,
|
adamc@638
|
50 decoders : int IM.map,
|
adamc@595
|
51 maxName : int
|
adamc@567
|
52 }
|
adamc@567
|
53
|
adamc@567
|
54 fun varDepth (e, _) =
|
adamc@567
|
55 case e of
|
adamc@567
|
56 EPrim _ => 0
|
adamc@567
|
57 | ERel _ => 0
|
adamc@567
|
58 | ENamed _ => 0
|
adamc@567
|
59 | ECon (_, _, NONE) => 0
|
adamc@567
|
60 | ECon (_, _, SOME e) => varDepth e
|
adamc@567
|
61 | ENone _ => 0
|
adamc@567
|
62 | ESome (_, e) => varDepth e
|
adamc@567
|
63 | EFfi _ => 0
|
adamc@567
|
64 | EFfiApp (_, _, es) => foldl Int.max 0 (map varDepth es)
|
adamc@567
|
65 | EApp (e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@567
|
66 | EAbs _ => 0
|
adamc@567
|
67 | EUnop (_, e) => varDepth e
|
adamc@567
|
68 | EBinop (_, e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@567
|
69 | ERecord xes => foldl Int.max 0 (map (fn (_, e, _) => varDepth e) xes)
|
adamc@567
|
70 | EField (e, _) => varDepth e
|
adamc@567
|
71 | ECase (e, pes, _) =>
|
adamc@567
|
72 foldl Int.max (varDepth e)
|
adamc@567
|
73 (map (fn (p, e) => E.patBindsN p + varDepth e) pes)
|
adamc@567
|
74 | EStrcat (e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@567
|
75 | EError (e, _) => varDepth e
|
adamc@741
|
76 | EReturnBlob {blob = e1, mimeType = e2, ...} => Int.max (varDepth e1, varDepth e2)
|
adamc@567
|
77 | EWrite e => varDepth e
|
adamc@567
|
78 | ESeq (e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@567
|
79 | ELet (_, _, e1, e2) => Int.max (varDepth e1, 1 + varDepth e2)
|
adamc@567
|
80 | EClosure _ => 0
|
adamc@567
|
81 | EQuery _ => 0
|
adamc@567
|
82 | EDml _ => 0
|
adamc@567
|
83 | ENextval _ => 0
|
adamc@567
|
84 | EUnurlify _ => 0
|
adamc@567
|
85 | EJavaScript _ => 0
|
adamc@568
|
86 | ESignalReturn e => varDepth e
|
adamc@572
|
87 | ESignalBind (e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@574
|
88 | ESignalSource e => varDepth e
|
adamc@736
|
89 | EServerCall (e, ek, _, _) => Int.max (varDepth e, varDepth ek)
|
adamc@670
|
90 | ERecv (e, ek, _) => Int.max (varDepth e, varDepth ek)
|
adamc@695
|
91 | ESleep (e, ek) => Int.max (varDepth e, varDepth ek)
|
adamc@567
|
92
|
adamc@591
|
93 fun closedUpto d =
|
adamc@591
|
94 let
|
adamc@591
|
95 fun cu inner (e, _) =
|
adamc@591
|
96 case e of
|
adamc@591
|
97 EPrim _ => true
|
adamc@591
|
98 | ERel n => n < inner orelse n - inner >= d
|
adamc@591
|
99 | ENamed _ => true
|
adamc@591
|
100 | ECon (_, _, NONE) => true
|
adamc@591
|
101 | ECon (_, _, SOME e) => cu inner e
|
adamc@591
|
102 | ENone _ => true
|
adamc@591
|
103 | ESome (_, e) => cu inner e
|
adamc@591
|
104 | EFfi _ => true
|
adamc@591
|
105 | EFfiApp (_, _, es) => List.all (cu inner) es
|
adamc@591
|
106 | EApp (e1, e2) => cu inner e1 andalso cu inner e2
|
adamc@591
|
107 | EAbs (_, _, _, e) => cu (inner + 1) e
|
adamc@591
|
108 | EUnop (_, e) => cu inner e
|
adamc@591
|
109 | EBinop (_, e1, e2) => cu inner e1 andalso cu inner e2
|
adamc@591
|
110 | ERecord xes => List.all (fn (_, e, _) => cu inner e) xes
|
adamc@591
|
111 | EField (e, _) => cu inner e
|
adamc@591
|
112 | ECase (e, pes, _) =>
|
adamc@591
|
113 cu inner e
|
adamc@591
|
114 andalso List.all (fn (p, e) => cu (inner + E.patBindsN p) e) pes
|
adamc@591
|
115 | EStrcat (e1, e2) => cu inner e1 andalso cu inner e2
|
adamc@591
|
116 | EError (e, _) => cu inner e
|
adamc@741
|
117 | EReturnBlob {blob = e1, mimeType = e2, ...} => cu inner e1 andalso cu inner e2
|
adamc@591
|
118 | EWrite e => cu inner e
|
adamc@591
|
119 | ESeq (e1, e2) => cu inner e1 andalso cu inner e2
|
adamc@591
|
120 | ELet (_, _, e1, e2) => cu inner e1 andalso cu (inner + 1) e2
|
adamc@591
|
121 | EClosure (_, es) => List.all (cu inner) es
|
adamc@591
|
122 | EQuery {query, body, initial, ...} =>
|
adamc@591
|
123 cu inner query
|
adamc@591
|
124 andalso cu (inner + 2) body
|
adamc@591
|
125 andalso cu inner initial
|
adamc@591
|
126 | EDml e => cu inner e
|
adamc@591
|
127 | ENextval e => cu inner e
|
adamc@591
|
128 | EUnurlify (e, _) => cu inner e
|
adamc@815
|
129 | EJavaScript (_, e) => cu inner e
|
adamc@591
|
130 | ESignalReturn e => cu inner e
|
adamc@591
|
131 | ESignalBind (e1, e2) => cu inner e1 andalso cu inner e2
|
adamc@591
|
132 | ESignalSource e => cu inner e
|
adamc@736
|
133 | EServerCall (e, ek, _, _) => cu inner e andalso cu inner ek
|
adamc@670
|
134 | ERecv (e, ek, _) => cu inner e andalso cu inner ek
|
adamc@695
|
135 | ESleep (e, ek) => cu inner e andalso cu inner ek
|
adamc@591
|
136 in
|
adamc@591
|
137 cu 0
|
adamc@591
|
138 end
|
adamc@591
|
139
|
adamc@568
|
140 fun strcat loc es =
|
adamc@568
|
141 case es of
|
adamc@568
|
142 [] => (EPrim (Prim.String ""), loc)
|
adamc@568
|
143 | [x] => x
|
adamc@568
|
144 | x :: es' => (EStrcat (x, strcat loc es'), loc)
|
adamc@568
|
145
|
adamc@801
|
146 fun patDepth (p, _) =
|
adamc@801
|
147 case p of
|
adamc@801
|
148 PWild => 0
|
adamc@801
|
149 | PVar _ => 0
|
adamc@801
|
150 | PPrim _ => 0
|
adamc@801
|
151 | PCon (_, _, NONE) => 0
|
adamc@801
|
152 | PCon (_, _, SOME p) => 1 + patDepth p
|
adamc@801
|
153 | PRecord xpts => foldl Int.max 0 (map (fn (_, p, _) => 1 + patDepth p) xpts)
|
adamc@801
|
154 | PNone _ => 0
|
adamc@801
|
155 | PSome (_, p) => 1 + patDepth p
|
adamc@801
|
156
|
adamc@801
|
157 val compact =
|
adamc@801
|
158 U.Exp.mapB {typ = fn t => t,
|
adamc@801
|
159 exp = fn inner => fn e =>
|
adamc@801
|
160 case e of
|
adamc@801
|
161 ERel n =>
|
adamc@801
|
162 if n >= inner then
|
adamc@801
|
163 ERel (n - inner)
|
adamc@801
|
164 else
|
adamc@801
|
165 e
|
adamc@801
|
166 | _ => e,
|
adamc@801
|
167 bind = fn (inner, b) =>
|
adamc@801
|
168 case b of
|
adamc@801
|
169 U.Exp.RelE _ => inner+1
|
adamc@801
|
170 | _ => inner}
|
adamc@813
|
171
|
adamc@815
|
172 exception CantEmbed of typ
|
adamc@815
|
173
|
adamc@589
|
174 fun process file =
|
adamc@567
|
175 let
|
adamc@596
|
176 val (someTs, nameds) =
|
adamc@596
|
177 foldl (fn ((DVal (_, n, t, e, _), _), (someTs, nameds)) => (someTs, IM.insert (nameds, n, e))
|
adamc@596
|
178 | ((DValRec vis, _), (someTs, nameds)) =>
|
adamc@596
|
179 (someTs, foldl (fn ((_, n, _, e, _), nameds) => IM.insert (nameds, n, e))
|
adamc@596
|
180 nameds vis)
|
adamc@808
|
181 | ((DDatatype dts, _), state as (someTs, nameds)) =>
|
adamc@808
|
182 (foldl (fn ((_, _, cs), someTs) =>
|
adamc@808
|
183 if ElabUtil.classifyDatatype cs = Option then
|
adamc@808
|
184 foldl (fn ((_, n, SOME t), someTs) => IM.insert (someTs, n, t)
|
adamc@808
|
185 | (_, someTs) => someTs) someTs cs
|
adamc@808
|
186 else
|
adamc@808
|
187 someTs) someTs dts,
|
adamc@808
|
188 nameds)
|
adamc@595
|
189 | (_, state) => state)
|
adamc@596
|
190 (IM.empty, IM.empty) file
|
adamc@567
|
191
|
adamc@590
|
192 fun str loc s = (EPrim (Prim.String s), loc)
|
adamc@590
|
193
|
adamc@594
|
194 fun isNullable (t, _) =
|
adamc@594
|
195 case t of
|
adamc@594
|
196 TOption _ => true
|
adamc@594
|
197 | TRecord [] => true
|
adamc@594
|
198 | _ => false
|
adamc@594
|
199
|
adamc@593
|
200 fun quoteExp loc (t : typ) (e, st) =
|
adamc@590
|
201 case #1 t of
|
adamc@593
|
202 TSource => (strcat loc [str loc "s",
|
adamc@593
|
203 (EFfiApp ("Basis", "htmlifyInt", [e]), loc)], st)
|
adamc@590
|
204
|
adamc@593
|
205 | TRecord [] => (str loc "null", st)
|
adamc@593
|
206 | TRecord [(x, t)] =>
|
adamc@593
|
207 let
|
adamc@593
|
208 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
|
adamc@593
|
209 in
|
adamc@593
|
210 (strcat loc [str loc ("{_" ^ x ^ ":"),
|
adamc@593
|
211 e,
|
adamc@593
|
212 str loc "}"], st)
|
adamc@593
|
213 end
|
adamc@593
|
214 | TRecord ((x, t) :: xts) =>
|
adamc@593
|
215 let
|
adamc@593
|
216 val (e', st) = quoteExp loc t ((EField (e, x), loc), st)
|
adamc@593
|
217 val (es, st) = ListUtil.foldlMap
|
adamc@593
|
218 (fn ((x, t), st) =>
|
adamc@593
|
219 let
|
adamc@593
|
220 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
|
adamc@593
|
221 in
|
adamc@593
|
222 (strcat loc [str loc (",_" ^ x ^ ":"), e], st)
|
adamc@593
|
223 end)
|
adamc@593
|
224 st xts
|
adamc@593
|
225 in
|
adamc@593
|
226 (strcat loc (str loc ("{_" ^ x ^ ":")
|
adamc@593
|
227 :: e'
|
adamc@593
|
228 :: es
|
adamc@593
|
229 @ [str loc "}"]), st)
|
adamc@593
|
230 end
|
adamc@590
|
231
|
adamc@593
|
232 | TFfi ("Basis", "string") => ((EFfiApp ("Basis", "jsifyString", [e]), loc), st)
|
adamc@593
|
233 | TFfi ("Basis", "int") => ((EFfiApp ("Basis", "htmlifyInt", [e]), loc), st)
|
adamc@593
|
234 | TFfi ("Basis", "float") => ((EFfiApp ("Basis", "htmlifyFloat", [e]), loc), st)
|
adamc@682
|
235 | TFfi ("Basis", "channel") => ((EFfiApp ("Basis", "jsifyChannel", [e]), loc), st)
|
adamc@593
|
236
|
adamc@593
|
237 | TFfi ("Basis", "bool") => ((ECase (e,
|
adamc@593
|
238 [((PCon (Enum, PConFfi {mod = "Basis",
|
adamc@593
|
239 datatyp = "bool",
|
adamc@593
|
240 con = "True",
|
adamc@593
|
241 arg = NONE}, NONE), loc),
|
adamc@593
|
242 str loc "true"),
|
adamc@593
|
243 ((PCon (Enum, PConFfi {mod = "Basis",
|
adamc@593
|
244 datatyp = "bool",
|
adamc@593
|
245 con = "False",
|
adamc@593
|
246 arg = NONE}, NONE), loc),
|
adamc@593
|
247 str loc "false")],
|
adamc@593
|
248 {disc = (TFfi ("Basis", "bool"), loc),
|
adamc@593
|
249 result = (TFfi ("Basis", "string"), loc)}), loc),
|
adamc@593
|
250 st)
|
adamc@592
|
251
|
adamc@594
|
252 | TOption t =>
|
adamc@594
|
253 let
|
adamc@594
|
254 val (e', st) = quoteExp loc t ((ERel 0, loc), st)
|
adamc@594
|
255 in
|
adamc@813
|
256 (case #1 e' of
|
adamc@813
|
257 EPrim (Prim.String "ERROR") => raise Fail "UHOH"
|
adamc@813
|
258 | _ =>
|
adamc@813
|
259 (ECase (e,
|
adamc@813
|
260 [((PNone t, loc),
|
adamc@813
|
261 str loc "null"),
|
adamc@813
|
262 ((PSome (t, (PVar ("x", t), loc)), loc),
|
adamc@813
|
263 if isNullable t then
|
adamc@813
|
264 strcat loc [str loc "{v:", e', str loc "}"]
|
adamc@813
|
265 else
|
adamc@813
|
266 e')],
|
adamc@813
|
267 {disc = (TOption t, loc),
|
adamc@813
|
268 result = (TFfi ("Basis", "string"), loc)}), loc),
|
adamc@594
|
269 st)
|
adamc@594
|
270 end
|
adamc@594
|
271
|
adamc@800
|
272 | TList t' =>
|
adamc@800
|
273 (case TM.find (#listInjectors st, t') of
|
adamc@800
|
274 SOME n' => ((EApp ((ENamed n', loc), e), loc), st)
|
adamc@800
|
275 | NONE =>
|
adamc@800
|
276 let
|
adamc@800
|
277 val rt = (TRecord [("1", t'), ("2", t)], loc)
|
adamc@800
|
278
|
adamc@800
|
279 val n' = #maxName st
|
adamc@800
|
280 val st = {decls = #decls st,
|
adamc@800
|
281 script = #script st,
|
adamc@800
|
282 included = #included st,
|
adamc@800
|
283 injectors = #injectors st,
|
adamc@800
|
284 listInjectors = TM.insert (#listInjectors st, t', n'),
|
adamc@800
|
285 decoders = #decoders st,
|
adamc@800
|
286 maxName = n' + 1}
|
adamc@800
|
287
|
adamc@800
|
288 val s = (TFfi ("Basis", "string"), loc)
|
adamc@801
|
289 val (e', st) = quoteExp loc t' ((EField ((ERel 0, loc), "1"), loc), st)
|
adamc@800
|
290
|
adamc@800
|
291 val body = (ECase ((ERel 0, loc),
|
adamc@800
|
292 [((PNone rt, loc),
|
adamc@800
|
293 str loc "null"),
|
adamc@800
|
294 ((PSome (rt, (PVar ("x", rt), loc)), loc),
|
adamc@800
|
295 strcat loc [str loc "{v:{_1:",
|
adamc@800
|
296 e',
|
adamc@800
|
297 str loc ",_2:",
|
adamc@800
|
298 (EApp ((ENamed n', loc),
|
adamc@800
|
299 (EField ((ERel 0, loc), "2"), loc)), loc),
|
adamc@800
|
300 str loc "}}"])],
|
adamc@800
|
301 {disc = t, result = s}), loc)
|
adamc@800
|
302 val body = (EAbs ("x", t, s, body), loc)
|
adamc@800
|
303
|
adamc@800
|
304 val st = {decls = (DValRec [("jsify", n', (TFun (t, s), loc),
|
adamc@800
|
305 body, "jsify")], loc) :: #decls st,
|
adamc@800
|
306 script = #script st,
|
adamc@800
|
307 included = #included st,
|
adamc@800
|
308 injectors = #injectors st,
|
adamc@800
|
309 listInjectors = #listInjectors st,
|
adamc@800
|
310 decoders= #decoders st,
|
adamc@800
|
311 maxName = #maxName st}
|
adamc@800
|
312
|
adamc@800
|
313
|
adamc@800
|
314 in
|
adamc@800
|
315 ((EApp ((ENamed n', loc), e), loc), st)
|
adamc@800
|
316 end)
|
adamc@800
|
317
|
adamc@595
|
318 | TDatatype (n, ref (dk, cs)) =>
|
adamc@595
|
319 (case IM.find (#injectors st, n) of
|
adamc@595
|
320 SOME n' => ((EApp ((ENamed n', loc), e), loc), st)
|
adamc@595
|
321 | NONE =>
|
adamc@595
|
322 let
|
adamc@595
|
323 val n' = #maxName st
|
adamc@595
|
324 val st = {decls = #decls st,
|
adamc@595
|
325 script = #script st,
|
adamc@595
|
326 included = #included st,
|
adamc@595
|
327 injectors = IM.insert (#injectors st, n, n'),
|
adamc@800
|
328 listInjectors = #listInjectors st,
|
adamc@638
|
329 decoders = #decoders st,
|
adamc@595
|
330 maxName = n' + 1}
|
adamc@595
|
331
|
adamc@595
|
332 val (pes, st) = ListUtil.foldlMap
|
adamc@595
|
333 (fn ((_, cn, NONE), st) =>
|
adamc@595
|
334 (((PCon (dk, PConVar cn, NONE), loc),
|
adamc@596
|
335 case dk of
|
adamc@596
|
336 Option => str loc "null"
|
adamc@596
|
337 | _ => str loc (Int.toString cn)),
|
adamc@595
|
338 st)
|
adamc@595
|
339 | ((_, cn, SOME t), st) =>
|
adamc@595
|
340 let
|
adamc@595
|
341 val (e, st) = quoteExp loc t ((ERel 0, loc), st)
|
adamc@595
|
342 in
|
adamc@595
|
343 (((PCon (dk, PConVar cn, SOME (PVar ("x", t), loc)), loc),
|
adamc@596
|
344 case dk of
|
adamc@596
|
345 Option =>
|
adamc@596
|
346 if isNullable t then
|
adamc@638
|
347 strcat loc [str loc "{v:",
|
adamc@596
|
348 e,
|
adamc@596
|
349 str loc "}"]
|
adamc@596
|
350 else
|
adamc@596
|
351 e
|
adamc@597
|
352 | _ => strcat loc [str loc ("{n:" ^ Int.toString cn
|
adamc@597
|
353 ^ ",v:"),
|
adamc@597
|
354 e,
|
adamc@597
|
355 str loc "}"]),
|
adamc@595
|
356 st)
|
adamc@595
|
357 end)
|
adamc@595
|
358 st cs
|
adamc@595
|
359
|
adamc@595
|
360 val s = (TFfi ("Basis", "string"), loc)
|
adamc@595
|
361 val body = (ECase ((ERel 0, loc), pes,
|
adamc@595
|
362 {disc = t, result = s}), loc)
|
adamc@595
|
363 val body = (EAbs ("x", t, s, body), loc)
|
adamc@595
|
364
|
adamc@595
|
365 val st = {decls = (DValRec [("jsify", n', (TFun (t, s), loc),
|
adamc@595
|
366 body, "jsify")], loc) :: #decls st,
|
adamc@595
|
367 script = #script st,
|
adamc@595
|
368 included = #included st,
|
adamc@595
|
369 injectors = #injectors st,
|
adamc@800
|
370 listInjectors = #listInjectors st,
|
adamc@638
|
371 decoders= #decoders st,
|
adamc@595
|
372 maxName = #maxName st}
|
adamc@595
|
373 in
|
adamc@595
|
374 ((EApp ((ENamed n', loc), e), loc), st)
|
adamc@595
|
375 end)
|
adamc@595
|
376
|
adamc@815
|
377 | _ => raise CantEmbed t
|
adamc@815
|
378 (*(EM.errorAt loc "Don't know how to embed type in JavaScript";
|
adamc@590
|
379 Print.prefaces "Can't embed" [("t", MonoPrint.p_typ MonoEnv.empty t)];
|
adamc@815
|
380 (str loc "ERROR", st))*)
|
adamc@590
|
381
|
adamc@613
|
382 fun unurlifyExp loc (t : typ, st) =
|
adamc@613
|
383 case #1 t of
|
adamc@613
|
384 TRecord [] => ("null", st)
|
adamc@613
|
385 | TRecord [(x, t)] =>
|
adamc@613
|
386 let
|
adamc@613
|
387 val (e, st) = unurlifyExp loc (t, st)
|
adamc@613
|
388 in
|
adamc@613
|
389 ("{_" ^ x ^ ":" ^ e ^ "}",
|
adamc@613
|
390 st)
|
adamc@613
|
391 end
|
adamc@613
|
392 | TRecord ((x, t) :: xts) =>
|
adamc@613
|
393 let
|
adamc@613
|
394 val (e', st) = unurlifyExp loc (t, st)
|
adamc@613
|
395 val (es, st) = ListUtil.foldlMap
|
adamc@638
|
396 (fn ((x, t), st) =>
|
adamc@638
|
397 let
|
adamc@638
|
398 val (e, st) = unurlifyExp loc (t, st)
|
adamc@638
|
399 in
|
adamc@638
|
400 (",_" ^ x ^ ":" ^ e, st)
|
adamc@638
|
401 end)
|
adamc@638
|
402 st xts
|
adamc@613
|
403 in
|
adamc@613
|
404 (String.concat ("{_"
|
adamc@613
|
405 :: x
|
adamc@613
|
406 :: ":"
|
adamc@613
|
407 :: e'
|
adamc@613
|
408 :: es
|
adamc@613
|
409 @ ["}"]), st)
|
adamc@613
|
410 end
|
adamc@613
|
411
|
adamc@679
|
412 | TFfi ("Basis", "string") => ("uu(t[i++])", st)
|
adamc@613
|
413 | TFfi ("Basis", "int") => ("parseInt(t[i++])", st)
|
adamc@613
|
414 | TFfi ("Basis", "float") => ("parseFloat(t[i++])", st)
|
adamc@682
|
415 | TFfi ("Basis", "channel") => ("(t[i++].length > 0 ? parseInt(t[i]) : null)", st)
|
adamc@613
|
416
|
adamc@613
|
417 | TFfi ("Basis", "bool") => ("t[i++] == \"True\"", st)
|
adamc@613
|
418
|
adamc@638
|
419 | TOption t =>
|
adamc@613
|
420 let
|
adamc@638
|
421 val (e, st) = unurlifyExp loc (t, st)
|
adamc@638
|
422 val e = if isNullable t then
|
adamc@638
|
423 "{v:" ^ e ^ "}"
|
adamc@638
|
424 else
|
adamc@638
|
425 e
|
adamc@613
|
426 in
|
adamc@703
|
427 ("(t[i++]==\"Some\"?" ^ e ^ ":null)", st)
|
adamc@638
|
428 end
|
adamc@613
|
429
|
adamc@638
|
430 | TDatatype (n, ref (dk, cs)) =>
|
adamc@638
|
431 (case IM.find (#decoders st, n) of
|
adamc@638
|
432 SOME n' => ("(tmp=_n" ^ Int.toString n' ^ "(t,i),i=tmp._1,tmp._2)", st)
|
adamc@613
|
433 | NONE =>
|
adamc@613
|
434 let
|
adamc@613
|
435 val n' = #maxName st
|
adamc@613
|
436 val st = {decls = #decls st,
|
adamc@613
|
437 script = #script st,
|
adamc@613
|
438 included = #included st,
|
adamc@638
|
439 injectors = #injectors st,
|
adamc@800
|
440 listInjectors = #listInjectors st,
|
adamc@638
|
441 decoders = IM.insert (#decoders st, n, n'),
|
adamc@613
|
442 maxName = n' + 1}
|
adamc@613
|
443
|
adamc@638
|
444 val (e, st) = foldl (fn ((x, cn, NONE), (e, st)) =>
|
adamc@638
|
445 ("x==\"" ^ x ^ "\"?"
|
adamc@638
|
446 ^ (case dk of
|
adamc@638
|
447 Option => "null"
|
adamc@638
|
448 | _ => Int.toString cn)
|
adamc@638
|
449 ^ ":" ^ e,
|
adamc@613
|
450 st)
|
adamc@638
|
451 | ((x, cn, SOME t), (e, st)) =>
|
adamc@613
|
452 let
|
adamc@638
|
453 val (e', st) = unurlifyExp loc (t, st)
|
adamc@613
|
454 in
|
adamc@638
|
455 ("x==\"" ^ x ^ "\"?"
|
adamc@638
|
456 ^ (case dk of
|
adamc@638
|
457 Option =>
|
adamc@638
|
458 if isNullable t then
|
adamc@638
|
459 "{v:" ^ e' ^ "}"
|
adamc@638
|
460 else
|
adamc@638
|
461 e'
|
adamc@638
|
462 | _ => "{n:" ^ Int.toString cn ^ ",v:" ^ e' ^ "}")
|
adamc@638
|
463 ^ ":" ^ e,
|
adamc@613
|
464 st)
|
adamc@613
|
465 end)
|
adamc@810
|
466 ("pf(\"" ^ ErrorMsg.spanToString loc ^ "\")", st) cs
|
adamc@613
|
467
|
adamc@638
|
468 val body = "function _n" ^ Int.toString n' ^ "(t,i){var x=t[i++];var r="
|
adamc@638
|
469 ^ e ^ ";return {_1:i,_2:r}}\n\n"
|
adamc@613
|
470
|
adamc@638
|
471 val st = {decls = #decls st,
|
adamc@638
|
472 script = body :: #script st,
|
adamc@613
|
473 included = #included st,
|
adamc@613
|
474 injectors = #injectors st,
|
adamc@800
|
475 listInjectors = #listInjectors st,
|
adamc@638
|
476 decoders = #decoders st,
|
adamc@613
|
477 maxName = #maxName st}
|
adamc@613
|
478 in
|
adamc@638
|
479 ("(tmp=_n" ^ Int.toString n' ^ "(t,i),i=tmp._1,tmp._2)", st)
|
adamc@638
|
480 end)
|
adamc@613
|
481
|
adamc@613
|
482 | _ => (EM.errorAt loc "Don't know how to unurlify type in JavaScript";
|
adamc@613
|
483 Print.prefaces "Can't unurlify" [("t", MonoPrint.p_typ MonoEnv.empty t)];
|
adamc@613
|
484 ("ERROR", st))
|
adamc@613
|
485
|
adamc@646
|
486 fun padWith (ch, s, len) =
|
adamc@646
|
487 if size s < len then
|
adamc@646
|
488 padWith (ch, String.str ch ^ s, len - 1)
|
adamc@646
|
489 else
|
adamc@646
|
490 s
|
adamc@646
|
491
|
adamc@794
|
492 val foundJavaScript = ref false
|
adamc@794
|
493
|
adamc@800
|
494 fun jsExp mode outer =
|
adamc@567
|
495 let
|
adamc@589
|
496 val len = length outer
|
adamc@567
|
497
|
adamc@589
|
498 fun jsE inner (e as (_, loc), st) =
|
adamc@589
|
499 let
|
adamc@590
|
500 val str = str loc
|
adamc@567
|
501
|
adamc@589
|
502 fun var n = Int.toString (len + inner - n - 1)
|
adamc@567
|
503
|
adamc@589
|
504 fun patCon pc =
|
adamc@589
|
505 case pc of
|
adamc@589
|
506 PConVar n => str (Int.toString n)
|
adamc@589
|
507 | PConFfi {mod = "Basis", con = "True", ...} => str "true"
|
adamc@589
|
508 | PConFfi {mod = "Basis", con = "False", ...} => str "false"
|
adamc@589
|
509 | PConFfi {con, ...} => str ("\"_" ^ con ^ "\"")
|
adamc@567
|
510
|
adamc@591
|
511 fun unsupported s =
|
adamc@591
|
512 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript[2]");
|
adamc@591
|
513 (str "ERROR", st))
|
adamc@577
|
514
|
adamc@589
|
515 val strcat = strcat loc
|
adamc@584
|
516
|
adamc@589
|
517 fun jsPrim p =
|
adamc@589
|
518 case p of
|
adamc@589
|
519 Prim.String s =>
|
adamc@589
|
520 str ("\""
|
adamc@589
|
521 ^ String.translate (fn #"'" =>
|
adamc@589
|
522 if mode = Attribute then
|
adamc@589
|
523 "\\047"
|
adamc@589
|
524 else
|
adamc@589
|
525 "'"
|
adamc@589
|
526 | #"\"" => "\\\""
|
adamc@589
|
527 | #"<" =>
|
adamc@589
|
528 if mode = Script then
|
adamc@589
|
529 "<"
|
adamc@589
|
530 else
|
adamc@589
|
531 "\\074"
|
adamc@589
|
532 | #"\\" => "\\\\"
|
adamc@646
|
533 | #"\n" => "\\n"
|
adamc@646
|
534 | #"\r" => "\\r"
|
adamc@646
|
535 | #"\t" => "\\t"
|
adamc@646
|
536 | ch =>
|
adamc@646
|
537 if Char.isPrint ch then
|
adamc@646
|
538 String.str ch
|
adamc@646
|
539 else
|
adamc@646
|
540 "\\" ^ padWith (#"0",
|
adamc@646
|
541 Int.fmt StringCvt.OCT (ord ch),
|
adamc@646
|
542 3)) s
|
adamc@589
|
543 ^ "\"")
|
adamc@821
|
544 | Prim.Char ch => str ("'" ^ String.str ch ^ "'")
|
adamc@589
|
545 | _ => str (Prim.toString p)
|
adamc@589
|
546
|
adamc@589
|
547 fun jsPat depth inner (p, _) succ fail =
|
adamc@589
|
548 case p of
|
adamc@589
|
549 PWild => succ
|
adamc@589
|
550 | PVar _ => strcat [str ("(_" ^ Int.toString (len + inner) ^ "=d"
|
adamc@589
|
551 ^ Int.toString depth ^ ","),
|
adamc@589
|
552 succ,
|
adamc@589
|
553 str ")"]
|
adamc@589
|
554 | PPrim p => strcat [str ("(d" ^ Int.toString depth ^ "=="),
|
adamc@589
|
555 jsPrim p,
|
adamc@589
|
556 str "?",
|
adamc@589
|
557 succ,
|
adamc@589
|
558 str ":",
|
adamc@589
|
559 fail,
|
adamc@589
|
560 str ")"]
|
adamc@589
|
561 | PCon (_, PConFfi {mod = "Basis", con = "True", ...}, NONE) =>
|
adamc@589
|
562 strcat [str ("(d" ^ Int.toString depth ^ "?"),
|
adamc@589
|
563 succ,
|
adamc@589
|
564 str ":",
|
adamc@589
|
565 fail,
|
adamc@589
|
566 str ")"]
|
adamc@589
|
567 | PCon (_, PConFfi {mod = "Basis", con = "False", ...}, NONE) =>
|
adamc@589
|
568 strcat [str ("(d" ^ Int.toString depth ^ "?"),
|
adamc@589
|
569 fail,
|
adamc@589
|
570 str ":",
|
adamc@589
|
571 succ,
|
adamc@589
|
572 str ")"]
|
adamc@596
|
573 | PCon (Option, _, NONE) =>
|
adamc@596
|
574 strcat [str ("(d" ^ Int.toString depth ^ "?"),
|
adamc@596
|
575 fail,
|
adamc@596
|
576 str ":",
|
adamc@596
|
577 succ,
|
adamc@596
|
578 str ")"]
|
adamc@596
|
579 | PCon (Option, PConVar n, SOME p) =>
|
adamc@596
|
580 (case IM.find (someTs, n) of
|
adamc@596
|
581 NONE => raise Fail "Jscomp: Not in someTs"
|
adamc@596
|
582 | SOME t =>
|
adamc@810
|
583 strcat [str ("(d" ^ Int.toString depth ^ "?(d"
|
adamc@810
|
584 ^ Int.toString (depth+1) ^ "=d" ^ Int.toString depth
|
adamc@596
|
585 ^ (if isNullable t then
|
adamc@810
|
586 ".v,"
|
adamc@596
|
587 else
|
adamc@813
|
588 "")
|
adamc@813
|
589 ^ ","),
|
adamc@810
|
590 jsPat (depth+1) inner p succ fail,
|
adamc@596
|
591 str "):",
|
adamc@596
|
592 fail,
|
adamc@596
|
593 str ")"])
|
adamc@589
|
594 | PCon (_, pc, NONE) =>
|
adamc@589
|
595 strcat [str ("(d" ^ Int.toString depth ^ "=="),
|
adamc@589
|
596 patCon pc,
|
adamc@589
|
597 str "?",
|
adamc@589
|
598 succ,
|
adamc@589
|
599 str ":",
|
adamc@589
|
600 fail,
|
adamc@589
|
601 str ")"]
|
adamc@589
|
602 | PCon (_, pc, SOME p) =>
|
adamc@589
|
603 strcat [str ("(d" ^ Int.toString depth ^ ".n=="),
|
adamc@589
|
604 patCon pc,
|
adamc@810
|
605 str ("?(d" ^ Int.toString (depth+1) ^ "=d" ^ Int.toString depth ^ ".v,"),
|
adamc@810
|
606 jsPat (depth+1) inner p succ fail,
|
adamc@589
|
607 str "):",
|
adamc@589
|
608 fail,
|
adamc@589
|
609 str ")"]
|
adamc@589
|
610 | PRecord xps =>
|
adamc@589
|
611 let
|
adamc@589
|
612 val (_, succ) = foldl
|
adamc@589
|
613 (fn ((x, p, _), (inner, succ)) =>
|
adamc@589
|
614 (inner + E.patBindsN p,
|
adamc@589
|
615 strcat [str ("(d" ^ Int.toString (depth+1) ^ "=d"
|
adamc@589
|
616 ^ Int.toString depth ^ "._" ^ x ^ ","),
|
adamc@589
|
617 jsPat (depth+1) inner p succ fail,
|
adamc@589
|
618 str ")"]))
|
adamc@589
|
619 (inner, succ) xps
|
adamc@589
|
620 in
|
adamc@589
|
621 succ
|
adamc@589
|
622 end
|
adamc@589
|
623 | PNone _ => strcat [str ("(d" ^ Int.toString depth ^ "?"),
|
adamc@589
|
624 fail,
|
adamc@589
|
625 str ":",
|
adamc@589
|
626 succ,
|
adamc@589
|
627 str ")"]
|
adamc@594
|
628 | PSome (t, p) => strcat (str ("(d" ^ Int.toString depth ^ "?")
|
adamc@594
|
629 :: (if isNullable t then
|
adamc@594
|
630 [str ("d" ^ Int.toString depth
|
adamc@594
|
631 ^ "=d" ^ Int.toString depth ^ ".v")]
|
adamc@594
|
632 else
|
adamc@594
|
633 [])
|
adamc@594
|
634 @ [jsPat depth inner p succ fail,
|
adamc@594
|
635 str ":",
|
adamc@594
|
636 fail,
|
adamc@594
|
637 str ")"])
|
adamc@589
|
638
|
adamc@601
|
639 val jsifyString = String.translate (fn #"\"" => "\\\""
|
adamc@601
|
640 | #"\\" => "\\\\"
|
adamc@601
|
641 | ch => String.str ch)
|
adamc@601
|
642
|
adamc@601
|
643 fun jsifyStringMulti (n, s) =
|
adamc@601
|
644 case n of
|
adamc@601
|
645 0 => s
|
adamc@601
|
646 | _ => jsifyStringMulti (n - 1, jsifyString s)
|
adamc@601
|
647
|
adamc@601
|
648 fun deStrcat level (all as (e, _)) =
|
adamc@589
|
649 case e of
|
adamc@601
|
650 EPrim (Prim.String s) => jsifyStringMulti (level, s)
|
adamc@601
|
651 | EStrcat (e1, e2) => deStrcat level e1 ^ deStrcat level e2
|
adamc@601
|
652 | EFfiApp ("Basis", "jsifyString", [e]) => "\"" ^ deStrcat (level + 1) e ^ "\""
|
adamc@601
|
653 | _ => (Print.prefaces "deStrcat" [("e", MonoPrint.p_exp MonoEnv.empty all)];
|
adamc@601
|
654 raise Fail "Jscomp: deStrcat")
|
adamc@590
|
655
|
adamc@590
|
656 val quoteExp = quoteExp loc
|
adamc@567
|
657 in
|
adamc@801
|
658 (*Print.prefaces "jsE" [("e", MonoPrint.p_exp MonoEnv.empty e),
|
adamc@801
|
659 ("inner", Print.PD.string (Int.toString inner))];*)
|
adamc@590
|
660
|
adamc@589
|
661 case #1 e of
|
adamc@589
|
662 EPrim p => (jsPrim p, st)
|
adamc@589
|
663 | ERel n =>
|
adamc@589
|
664 if n < inner then
|
adamc@589
|
665 (str ("_" ^ var n), st)
|
adamc@589
|
666 else
|
adamc@589
|
667 let
|
adamc@589
|
668 val n = n - inner
|
adamc@813
|
669 (*val () = Print.prefaces "quote" [("t", MonoPrint.p_typ MonoEnv.empty
|
adamc@813
|
670 (List.nth (outer, n)))]*)
|
adamc@589
|
671 in
|
adamc@800
|
672 quoteExp (List.nth (outer, n)) ((ERel n, loc), st)
|
adamc@589
|
673 end
|
adamc@567
|
674
|
adamc@589
|
675 | ENamed n =>
|
adamc@589
|
676 let
|
adamc@589
|
677 val st =
|
adamc@589
|
678 if IS.member (#included st, n) then
|
adamc@589
|
679 st
|
adamc@589
|
680 else
|
adamc@589
|
681 case IM.find (nameds, n) of
|
adamc@589
|
682 NONE => raise Fail "Jscomp: Unbound ENamed"
|
adamc@589
|
683 | SOME e =>
|
adamc@589
|
684 let
|
adamc@589
|
685 val st = {decls = #decls st,
|
adamc@589
|
686 script = #script st,
|
adamc@595
|
687 included = IS.add (#included st, n),
|
adamc@595
|
688 injectors = #injectors st,
|
adamc@800
|
689 listInjectors = #listInjectors st,
|
adamc@638
|
690 decoders = #decoders st,
|
adamc@595
|
691 maxName = #maxName st}
|
adamc@578
|
692
|
adamc@801
|
693 val old = e
|
adamc@800
|
694 val (e, st) = jsExp mode [] 0 (e, st)
|
adamc@801
|
695 val new = e
|
adamc@601
|
696 val e = deStrcat 0 e
|
adamc@589
|
697
|
adamc@589
|
698 val sc = "_n" ^ Int.toString n ^ "=" ^ e ^ ";\n"
|
adamc@589
|
699 in
|
adamc@801
|
700 (*Print.prefaces "jsify'" [("old", MonoPrint.p_exp MonoEnv.empty old),
|
adamc@801
|
701 ("new", MonoPrint.p_exp MonoEnv.empty new)];*)
|
adamc@589
|
702 {decls = #decls st,
|
adamc@589
|
703 script = sc :: #script st,
|
adamc@595
|
704 included = #included st,
|
adamc@595
|
705 injectors = #injectors st,
|
adamc@800
|
706 listInjectors = #listInjectors st,
|
adamc@638
|
707 decoders= #decoders st,
|
adamc@595
|
708 maxName = #maxName st}
|
adamc@589
|
709 end
|
adamc@589
|
710 in
|
adamc@589
|
711 (str ("_n" ^ Int.toString n), st)
|
adamc@589
|
712 end
|
adamc@589
|
713
|
adamc@596
|
714 | ECon (Option, _, NONE) => (str "null", st)
|
adamc@596
|
715 | ECon (Option, PConVar n, SOME e) =>
|
adamc@596
|
716 let
|
adamc@596
|
717 val (e, st) = jsE inner (e, st)
|
adamc@596
|
718 in
|
adamc@596
|
719 case IM.find (someTs, n) of
|
adamc@596
|
720 NONE => raise Fail "Jscomp: Not in someTs [2]"
|
adamc@596
|
721 | SOME t =>
|
adamc@596
|
722 (if isNullable t then
|
adamc@596
|
723 strcat [str "{v:",
|
adamc@596
|
724 e,
|
adamc@596
|
725 str "}"]
|
adamc@596
|
726 else
|
adamc@596
|
727 e, st)
|
adamc@596
|
728 end
|
adamc@596
|
729
|
adamc@589
|
730 | ECon (_, pc, NONE) => (patCon pc, st)
|
adamc@589
|
731 | ECon (_, pc, SOME e) =>
|
adamc@589
|
732 let
|
adamc@589
|
733 val (s, st) = jsE inner (e, st)
|
adamc@589
|
734 in
|
adamc@589
|
735 (strcat [str "{n:",
|
adamc@589
|
736 patCon pc,
|
adamc@589
|
737 str ",v:",
|
adamc@589
|
738 s,
|
adamc@589
|
739 str "}"], st)
|
adamc@589
|
740 end
|
adamc@596
|
741
|
adamc@589
|
742 | ENone _ => (str "null", st)
|
adamc@589
|
743 | ESome (t, e) =>
|
adamc@572
|
744 let
|
adamc@572
|
745 val (e, st) = jsE inner (e, st)
|
adamc@572
|
746 in
|
adamc@589
|
747 (if isNullable t then
|
adamc@589
|
748 strcat [str "{v:", e, str "}"]
|
adamc@589
|
749 else
|
adamc@589
|
750 e, st)
|
adamc@589
|
751 end
|
adamc@589
|
752
|
adamc@589
|
753 | EFfi k =>
|
adamc@589
|
754 let
|
adamc@765
|
755 val name = case Settings.jsFunc k of
|
adamc@589
|
756 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k
|
adamc@589
|
757 ^ " in JavaScript");
|
adamc@589
|
758 "ERROR")
|
adamc@589
|
759 | SOME s => s
|
adamc@589
|
760 in
|
adamc@589
|
761 (str name, st)
|
adamc@589
|
762 end
|
adamc@589
|
763 | EFfiApp (m, x, args) =>
|
adamc@589
|
764 let
|
adamc@765
|
765 val name = case Settings.jsFunc (m, x) of
|
adamc@589
|
766 NONE => (EM.errorAt loc ("Unsupported FFI function "
|
adamc@589
|
767 ^ x ^ " in JavaScript");
|
adamc@589
|
768 "ERROR")
|
adamc@589
|
769 | SOME s => s
|
adamc@589
|
770 in
|
adamc@589
|
771 case args of
|
adamc@589
|
772 [] => (str (name ^ "()"), st)
|
adamc@589
|
773 | [e] =>
|
adamc@589
|
774 let
|
adamc@589
|
775 val (e, st) = jsE inner (e, st)
|
adamc@589
|
776 in
|
adamc@589
|
777 (strcat [str (name ^ "("),
|
adamc@589
|
778 e,
|
adamc@589
|
779 str ")"], st)
|
adamc@589
|
780 end
|
adamc@589
|
781 | e :: es =>
|
adamc@589
|
782 let
|
adamc@589
|
783 val (e, st) = jsE inner (e, st)
|
adamc@589
|
784 val (es, st) = ListUtil.foldlMapConcat
|
adamc@589
|
785 (fn (e, st) =>
|
adamc@589
|
786 let
|
adamc@589
|
787 val (e, st) = jsE inner (e, st)
|
adamc@589
|
788 in
|
adamc@589
|
789 ([str ",", e], st)
|
adamc@589
|
790 end)
|
adamc@589
|
791 st es
|
adamc@589
|
792 in
|
adamc@589
|
793 (strcat (str (name ^ "(")
|
adamc@589
|
794 :: e
|
adamc@589
|
795 :: es
|
adamc@589
|
796 @ [str ")"]), st)
|
adamc@589
|
797 end
|
adamc@589
|
798 end
|
adamc@589
|
799
|
adamc@589
|
800 | EApp (e1, e2) =>
|
adamc@589
|
801 let
|
adamc@589
|
802 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
803 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
804 in
|
adamc@589
|
805 (strcat [e1, str "(", e2, str ")"], st)
|
adamc@589
|
806 end
|
adamc@589
|
807 | EAbs (_, _, _, e) =>
|
adamc@589
|
808 let
|
adamc@589
|
809 val locals = List.tabulate
|
adamc@589
|
810 (varDepth e,
|
adamc@589
|
811 fn i => str ("var _" ^ Int.toString (len + inner + i + 1) ^ ";"))
|
adamc@589
|
812 val (e, st) = jsE (inner + 1) (e, st)
|
adamc@589
|
813 in
|
adamc@589
|
814 (strcat (str ("function(_"
|
adamc@589
|
815 ^ Int.toString (len + inner)
|
adamc@589
|
816 ^ "){")
|
adamc@589
|
817 :: locals
|
adamc@589
|
818 @ [str "return ",
|
adamc@589
|
819 e,
|
adamc@589
|
820 str "}"]),
|
adamc@589
|
821 st)
|
adamc@589
|
822 end
|
adamc@589
|
823
|
adamc@589
|
824 | EUnop (s, e) =>
|
adamc@589
|
825 let
|
adamc@589
|
826 val (e, st) = jsE inner (e, st)
|
adamc@589
|
827 in
|
adamc@589
|
828 (strcat [str ("(" ^ s),
|
adamc@572
|
829 e,
|
adamc@589
|
830 str ")"],
|
adamc@589
|
831 st)
|
adamc@589
|
832 end
|
adamc@589
|
833 | EBinop (s, e1, e2) =>
|
adamc@589
|
834 let
|
adamc@729
|
835 val s =
|
adamc@729
|
836 case s of
|
adamc@729
|
837 "!strcmp" => "=="
|
adamc@729
|
838 | _ => s
|
adamc@729
|
839
|
adamc@589
|
840 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
841 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
842 in
|
adamc@589
|
843 (strcat [str "(",
|
adamc@589
|
844 e1,
|
adamc@589
|
845 str s,
|
adamc@589
|
846 e2,
|
adamc@589
|
847 str ")"],
|
adamc@589
|
848 st)
|
adamc@589
|
849 end
|
adamc@589
|
850
|
adamc@589
|
851 | ERecord [] => (str "null", st)
|
adamc@589
|
852 | ERecord [(x, e, _)] =>
|
adamc@589
|
853 let
|
adamc@589
|
854 val (e, st) = jsE inner (e, st)
|
adamc@589
|
855 in
|
adamc@589
|
856 (strcat [str "{_x:", e, str "}"], st)
|
adamc@589
|
857 end
|
adamc@589
|
858 | ERecord ((x, e, _) :: xes) =>
|
adamc@589
|
859 let
|
adamc@589
|
860 val (e, st) = jsE inner (e, st)
|
adamc@589
|
861
|
adamc@589
|
862 val (es, st) =
|
adamc@589
|
863 foldr (fn ((x, e, _), (es, st)) =>
|
adamc@589
|
864 let
|
adamc@589
|
865 val (e, st) = jsE inner (e, st)
|
adamc@589
|
866 in
|
adamc@589
|
867 (str (",_" ^ x ^ ":")
|
adamc@589
|
868 :: e
|
adamc@589
|
869 :: es,
|
adamc@589
|
870 st)
|
adamc@589
|
871 end)
|
adamc@589
|
872 ([str "}"], st) xes
|
adamc@589
|
873 in
|
adamc@589
|
874 (strcat (str ("{_" ^ x ^ ":")
|
adamc@589
|
875 :: e
|
adamc@589
|
876 :: es),
|
adamc@589
|
877 st)
|
adamc@589
|
878 end
|
adamc@589
|
879 | EField (e, x) =>
|
adamc@589
|
880 let
|
adamc@589
|
881 val (e, st) = jsE inner (e, st)
|
adamc@589
|
882 in
|
adamc@589
|
883 (strcat [e,
|
adamc@589
|
884 str ("._" ^ x)], st)
|
adamc@589
|
885 end
|
adamc@589
|
886
|
adamc@591
|
887 | ECase (e', pes, {result, ...}) =>
|
adamc@801
|
888 let
|
adamc@801
|
889 val plen = length pes
|
adamc@589
|
890
|
adamc@801
|
891 val (cases, st) = ListUtil.foldliMap
|
adamc@801
|
892 (fn (i, (p, e), st) =>
|
adamc@801
|
893 let
|
adamc@801
|
894 val (e, st) = jsE (inner + E.patBindsN p) (e, st)
|
adamc@801
|
895 val fail =
|
adamc@801
|
896 if i = plen - 1 then
|
adamc@810
|
897 str ("pf(\"" ^ ErrorMsg.spanToString loc ^ "\")")
|
adamc@801
|
898 else
|
adamc@801
|
899 str ("c" ^ Int.toString (i+1) ^ "()")
|
adamc@801
|
900 val c = jsPat 0 inner p e fail
|
adamc@801
|
901 in
|
adamc@801
|
902 (strcat [str ("c" ^ Int.toString i ^ "=function(){return "),
|
adamc@801
|
903 c,
|
adamc@801
|
904 str "},"],
|
adamc@801
|
905 st)
|
adamc@801
|
906 end)
|
adamc@801
|
907 st pes
|
adamc@589
|
908
|
adamc@801
|
909 val depth = foldl Int.max 0 (map (fn (p, _) => 1 + patDepth p) pes)
|
adamc@801
|
910 val normalDepth = foldl Int.max 0 (map (fn (_, e) => 1 + varDepth e) pes)
|
adamc@801
|
911 val (e, st) = jsE inner (e', st)
|
adamc@801
|
912
|
adamc@801
|
913 val len = inner + len
|
adamc@801
|
914 val normalVars = List.tabulate (normalDepth, fn n => "_" ^ Int.toString (n + len))
|
adamc@801
|
915 val patVars = List.tabulate (depth, fn n => "d" ^ Int.toString n)
|
adamc@827
|
916 val caseVars = ListUtil.mapi (fn (i, _) => "c" ^ Int.toString i) pes
|
adamc@801
|
917 in
|
adamc@801
|
918 (strcat (str "(function (){ var "
|
adamc@827
|
919 :: str (String.concatWith "," (normalVars @ patVars @ caseVars) ^ ";d0=")
|
adamc@801
|
920 :: e
|
adamc@801
|
921 :: str ";\nreturn ("
|
adamc@801
|
922 :: List.revAppend (cases,
|
adamc@801
|
923 [str "c0()) } ())"])), st)
|
adamc@801
|
924 end
|
adamc@589
|
925
|
adamc@589
|
926 | EStrcat (e1, e2) =>
|
adamc@589
|
927 let
|
adamc@589
|
928 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
929 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
930 in
|
adamc@693
|
931 (strcat [str "cat(", e1, str ",", e2, str ")"], st)
|
adamc@589
|
932 end
|
adamc@589
|
933
|
adamc@589
|
934 | EError (e, _) =>
|
adamc@589
|
935 let
|
adamc@589
|
936 val (e, st) = jsE inner (e, st)
|
adamc@589
|
937 in
|
adamc@726
|
938 (strcat [str "er(", e, str ")"],
|
adamc@589
|
939 st)
|
adamc@589
|
940 end
|
adamc@589
|
941
|
adamc@589
|
942 | EWrite e =>
|
adamc@589
|
943 let
|
adamc@589
|
944 val (e, st) = jsE inner (e, st)
|
adamc@589
|
945 in
|
adamc@589
|
946 (strcat [str "document.write(",
|
adamc@589
|
947 e,
|
adamc@589
|
948 str ".v)"], st)
|
adamc@589
|
949 end
|
adamc@589
|
950
|
adamc@589
|
951 | ESeq (e1, e2) =>
|
adamc@589
|
952 let
|
adamc@589
|
953 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
954 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
955 in
|
adamc@589
|
956 (strcat [str "(", e1, str ",", e2, str ")"], st)
|
adamc@589
|
957 end
|
adamc@589
|
958 | ELet (_, _, e1, e2) =>
|
adamc@589
|
959 let
|
adamc@589
|
960 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
961 val (e2, st) = jsE (inner + 1) (e2, st)
|
adamc@589
|
962 in
|
adamc@589
|
963 (strcat [str ("(_" ^ Int.toString (len + inner) ^ "="),
|
adamc@589
|
964 e1,
|
adamc@589
|
965 str ",",
|
adamc@589
|
966 e2,
|
adamc@572
|
967 str ")"], st)
|
adamc@572
|
968 end
|
adamc@589
|
969
|
adamc@815
|
970 | EJavaScript (Source _, e) =>
|
adamc@794
|
971 (foundJavaScript := true;
|
adamc@815
|
972 jsE inner (e, st))
|
adamc@815
|
973 | EJavaScript (_, e) =>
|
adamc@815
|
974 let
|
adamc@815
|
975 val (e, st) = jsE inner (e, st)
|
adamc@815
|
976 in
|
adamc@815
|
977 foundJavaScript := true;
|
adamc@815
|
978 (strcat [str "cs(function(){return ",
|
adamc@815
|
979 compact inner e,
|
adamc@815
|
980 str "})"],
|
adamc@815
|
981 st)
|
adamc@815
|
982 end
|
adamc@590
|
983
|
adamc@589
|
984 | EClosure _ => unsupported "EClosure"
|
adamc@589
|
985 | EQuery _ => unsupported "Query"
|
adamc@589
|
986 | EDml _ => unsupported "DML"
|
adamc@589
|
987 | ENextval _ => unsupported "Nextval"
|
adamc@589
|
988 | EUnurlify _ => unsupported "EUnurlify"
|
adamc@741
|
989 | EReturnBlob _ => unsupported "EUnurlify"
|
adamc@590
|
990
|
adamc@589
|
991 | ESignalReturn e =>
|
adamc@572
|
992 let
|
adamc@572
|
993 val (e, st) = jsE inner (e, st)
|
adamc@572
|
994 in
|
adamc@589
|
995 (strcat [str "sr(",
|
adamc@589
|
996 e,
|
adamc@589
|
997 str ")"],
|
adamc@589
|
998 st)
|
adamc@589
|
999 end
|
adamc@589
|
1000 | ESignalBind (e1, e2) =>
|
adamc@589
|
1001 let
|
adamc@589
|
1002 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
1003 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
1004 in
|
adamc@589
|
1005 (strcat [str "sb(",
|
adamc@589
|
1006 e1,
|
adamc@589
|
1007 str ",",
|
adamc@589
|
1008 e2,
|
adamc@589
|
1009 str ")"],
|
adamc@589
|
1010 st)
|
adamc@589
|
1011 end
|
adamc@589
|
1012 | ESignalSource e =>
|
adamc@589
|
1013 let
|
adamc@589
|
1014 val (e, st) = jsE inner (e, st)
|
adamc@589
|
1015 in
|
adamc@589
|
1016 (strcat [str "ss(",
|
adamc@589
|
1017 e,
|
adamc@589
|
1018 str ")"],
|
adamc@589
|
1019 st)
|
adamc@572
|
1020 end
|
adamc@608
|
1021
|
adamc@736
|
1022 | EServerCall (e, ek, t, eff) =>
|
adamc@609
|
1023 let
|
adamc@614
|
1024 val (e, st) = jsE inner (e, st)
|
adamc@609
|
1025 val (ek, st) = jsE inner (ek, st)
|
adamc@613
|
1026 val (unurl, st) = unurlifyExp loc (t, st)
|
adamc@609
|
1027 in
|
adamc@764
|
1028 (strcat [str ("rc(cat(\"" ^ Settings.getUrlPrefix () ^ "\","),
|
adamc@614
|
1029 e,
|
adamc@703
|
1030 str ("), function(s){var t=s.split(\"/\");var i=0;return "
|
adamc@613
|
1031 ^ unurl ^ "},"),
|
adamc@609
|
1032 ek,
|
adamc@736
|
1033 str (","
|
adamc@736
|
1034 ^ (case eff of
|
adamc@736
|
1035 ReadCookieWrite => "true"
|
adamc@736
|
1036 | _ => "false")
|
adamc@736
|
1037 ^ ")")],
|
adamc@609
|
1038 st)
|
adamc@609
|
1039 end
|
adamc@670
|
1040
|
adamc@670
|
1041 | ERecv (e, ek, t) =>
|
adamc@670
|
1042 let
|
adamc@670
|
1043 val (e, st) = jsE inner (e, st)
|
adamc@670
|
1044 val (ek, st) = jsE inner (ek, st)
|
adamc@670
|
1045 val (unurl, st) = unurlifyExp loc (t, st)
|
adamc@670
|
1046 in
|
adamc@670
|
1047 (strcat [str "rv(",
|
adamc@670
|
1048 e,
|
adamc@670
|
1049 str (", function(s){var t=s.split(\"/\");var i=0;return "
|
adamc@670
|
1050 ^ unurl ^ "},"),
|
adamc@670
|
1051 ek,
|
adamc@670
|
1052 str ")"],
|
adamc@670
|
1053 st)
|
adamc@670
|
1054 end
|
adamc@695
|
1055
|
adamc@695
|
1056 | ESleep (e, ek) =>
|
adamc@695
|
1057 let
|
adamc@695
|
1058 val (e, st) = jsE inner (e, st)
|
adamc@695
|
1059 val (ek, st) = jsE inner (ek, st)
|
adamc@695
|
1060 in
|
adamc@695
|
1061 (strcat [str "window.setTimeout(",
|
adamc@695
|
1062 ek,
|
adamc@695
|
1063 str ", ",
|
adamc@695
|
1064 e,
|
adamc@695
|
1065 str ")"],
|
adamc@695
|
1066 st)
|
adamc@695
|
1067 end
|
adamc@567
|
1068 end
|
adamc@589
|
1069 in
|
adamc@589
|
1070 jsE
|
adamc@589
|
1071 end
|
adamc@567
|
1072
|
adamc@567
|
1073
|
adamc@815
|
1074 fun patBinds ((p, _), env) =
|
adamc@815
|
1075 case p of
|
adamc@815
|
1076 PWild => env
|
adamc@815
|
1077 | PVar (_, t) => t :: env
|
adamc@815
|
1078 | PPrim _ => env
|
adamc@815
|
1079 | PCon (_, _, NONE) => env
|
adamc@815
|
1080 | PCon (_, _, SOME p) => patBinds (p, env)
|
adamc@815
|
1081 | PRecord xpts => foldl (fn ((_, p, _), env) => patBinds (p, env)) env xpts
|
adamc@815
|
1082 | PNone _ => env
|
adamc@815
|
1083 | PSome (_, p) => patBinds (p, env)
|
adamc@815
|
1084
|
adamc@815
|
1085 fun exp outer (e as (_, loc), st) =
|
adamc@815
|
1086 ((*Print.preface ("exp", MonoPrint.p_exp MonoEnv.empty e);*)
|
adamc@815
|
1087 case #1 e of
|
adamc@815
|
1088 EPrim _ => (e, st)
|
adamc@815
|
1089 | ERel _ => (e, st)
|
adamc@815
|
1090 | ENamed _ => (e, st)
|
adamc@815
|
1091 | ECon (_, _, NONE) => (e, st)
|
adamc@815
|
1092 | ECon (dk, pc, SOME e) =>
|
adamc@815
|
1093 let
|
adamc@815
|
1094 val (e, st) = exp outer (e, st)
|
adamc@815
|
1095 in
|
adamc@815
|
1096 ((ECon (dk, pc, SOME e), loc), st)
|
adamc@815
|
1097 end
|
adamc@815
|
1098 | ENone _ => (e, st)
|
adamc@815
|
1099 | ESome (t, e) =>
|
adamc@815
|
1100 let
|
adamc@815
|
1101 val (e, st) = exp outer (e, st)
|
adamc@815
|
1102 in
|
adamc@815
|
1103 ((ESome (t, e), loc), st)
|
adamc@815
|
1104 end
|
adamc@815
|
1105 | EFfi _ => (e, st)
|
adamc@815
|
1106 | EFfiApp (m, x, es) =>
|
adamc@815
|
1107 let
|
adamc@815
|
1108 val (es, st) = ListUtil.foldlMap (exp outer) st es
|
adamc@815
|
1109 in
|
adamc@815
|
1110 ((EFfiApp (m, x, es), loc), st)
|
adamc@815
|
1111 end
|
adamc@815
|
1112 | EApp (e1, e2) =>
|
adamc@815
|
1113 let
|
adamc@815
|
1114 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1115 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1116 in
|
adamc@815
|
1117 ((EApp (e1, e2), loc), st)
|
adamc@815
|
1118 end
|
adamc@815
|
1119 | EAbs (x, dom, ran, e) =>
|
adamc@815
|
1120 let
|
adamc@815
|
1121 val (e, st) = exp (dom :: outer) (e, st)
|
adamc@815
|
1122 in
|
adamc@815
|
1123 ((EAbs (x, dom, ran, e), loc), st)
|
adamc@815
|
1124 end
|
adamc@815
|
1125
|
adamc@815
|
1126 | EUnop (s, e) =>
|
adamc@815
|
1127 let
|
adamc@815
|
1128 val (e, st) = exp outer (e, st)
|
adamc@815
|
1129 in
|
adamc@815
|
1130 ((EUnop (s, e), loc), st)
|
adamc@815
|
1131 end
|
adamc@815
|
1132 | EBinop (s, e1, e2) =>
|
adamc@815
|
1133 let
|
adamc@815
|
1134 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1135 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1136 in
|
adamc@815
|
1137 ((EBinop (s, e1, e2), loc), st)
|
adamc@815
|
1138 end
|
adamc@815
|
1139
|
adamc@815
|
1140 | ERecord xets =>
|
adamc@815
|
1141 let
|
adamc@815
|
1142 val (xets, st) = ListUtil.foldlMap (fn ((x, e, t), st) =>
|
adamc@815
|
1143 let
|
adamc@815
|
1144 val (e, st) = exp outer (e, st)
|
adamc@815
|
1145 in
|
adamc@815
|
1146 ((x, e, t), st)
|
adamc@815
|
1147 end) st xets
|
adamc@815
|
1148 in
|
adamc@815
|
1149 ((ERecord xets, loc), st)
|
adamc@815
|
1150 end
|
adamc@815
|
1151 | EField (e, s) =>
|
adamc@815
|
1152 let
|
adamc@815
|
1153 val (e, st) = exp outer (e, st)
|
adamc@815
|
1154 in
|
adamc@815
|
1155 ((EField (e, s), loc), st)
|
adamc@815
|
1156 end
|
adamc@815
|
1157
|
adamc@815
|
1158 | ECase (e, pes, ts) =>
|
adamc@815
|
1159 let
|
adamc@815
|
1160 val (e, st) = exp outer (e, st)
|
adamc@815
|
1161 val (pes, st) = ListUtil.foldlMap (fn ((p, e), st) =>
|
adamc@815
|
1162 let
|
adamc@815
|
1163 val (e, st) = exp (patBinds (p, outer)) (e, st)
|
adamc@815
|
1164 in
|
adamc@815
|
1165 ((p, e), st)
|
adamc@815
|
1166 end) st pes
|
adamc@815
|
1167 in
|
adamc@815
|
1168 ((ECase (e, pes, ts), loc), st)
|
adamc@815
|
1169 end
|
adamc@815
|
1170
|
adamc@815
|
1171 | EStrcat (e1, e2) =>
|
adamc@815
|
1172 let
|
adamc@815
|
1173 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1174 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1175 in
|
adamc@815
|
1176 ((EStrcat (e1, e2), loc), st)
|
adamc@815
|
1177 end
|
adamc@815
|
1178
|
adamc@815
|
1179 | EError (e, t) =>
|
adamc@815
|
1180 let
|
adamc@815
|
1181 val (e, st) = exp outer (e, st)
|
adamc@815
|
1182 in
|
adamc@815
|
1183 ((EError (e, t), loc), st)
|
adamc@815
|
1184 end
|
adamc@815
|
1185 | EReturnBlob {blob, mimeType, t} =>
|
adamc@815
|
1186 let
|
adamc@815
|
1187 val (blob, st) = exp outer (blob, st)
|
adamc@815
|
1188 val (mimeType, st) = exp outer (mimeType, st)
|
adamc@815
|
1189 in
|
adamc@815
|
1190 ((EReturnBlob {blob = blob, mimeType = mimeType, t = t}, loc), st)
|
adamc@815
|
1191 end
|
adamc@815
|
1192
|
adamc@815
|
1193 | EWrite e =>
|
adamc@815
|
1194 let
|
adamc@815
|
1195 val (e, st) = exp outer (e, st)
|
adamc@815
|
1196 in
|
adamc@815
|
1197 ((EWrite e, loc), st)
|
adamc@815
|
1198 end
|
adamc@815
|
1199 | ESeq (e1, e2) =>
|
adamc@815
|
1200 let
|
adamc@815
|
1201 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1202 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1203 in
|
adamc@815
|
1204 ((ESeq (e1, e2), loc), st)
|
adamc@815
|
1205 end
|
adamc@815
|
1206 | ELet (x, t, e1, e2) =>
|
adamc@815
|
1207 let
|
adamc@815
|
1208 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1209 val (e2, st) = exp (t :: outer) (e2, st)
|
adamc@815
|
1210 in
|
adamc@815
|
1211 ((ELet (x, t, e1, e2), loc), st)
|
adamc@815
|
1212 end
|
adamc@815
|
1213
|
adamc@815
|
1214 | EClosure (n, es) =>
|
adamc@815
|
1215 let
|
adamc@815
|
1216 val (es, st) = ListUtil.foldlMap (exp outer) st es
|
adamc@815
|
1217 in
|
adamc@815
|
1218 ((EClosure (n, es), loc), st)
|
adamc@815
|
1219 end
|
adamc@815
|
1220
|
adamc@815
|
1221 | EQuery {exps, tables, state, query, body, initial} =>
|
adamc@815
|
1222 let
|
adamc@815
|
1223 val (query, st) = exp outer (query, st)
|
adamc@815
|
1224 val (body, st) = exp outer (body, st)
|
adamc@815
|
1225 val (initial, st) = exp outer (initial, st)
|
adamc@815
|
1226 in
|
adamc@815
|
1227 ((EQuery {exps = exps, tables = tables, state = state,
|
adamc@815
|
1228 query = query, body = body, initial = initial}, loc), st)
|
adamc@815
|
1229 end
|
adamc@815
|
1230 | EDml e =>
|
adamc@815
|
1231 let
|
adamc@815
|
1232 val (e, st) = exp outer (e, st)
|
adamc@815
|
1233 in
|
adamc@815
|
1234 ((EDml e, loc), st)
|
adamc@815
|
1235 end
|
adamc@815
|
1236 | ENextval e =>
|
adamc@815
|
1237 let
|
adamc@815
|
1238 val (e, st) = exp outer (e, st)
|
adamc@815
|
1239 in
|
adamc@815
|
1240 ((ENextval e, loc), st)
|
adamc@815
|
1241 end
|
adamc@815
|
1242
|
adamc@815
|
1243 | EUnurlify (e, t) =>
|
adamc@815
|
1244 let
|
adamc@815
|
1245 val (e, st) = exp outer (e, st)
|
adamc@815
|
1246 in
|
adamc@815
|
1247 ((EUnurlify (e, t), loc), st)
|
adamc@815
|
1248 end
|
adamc@815
|
1249
|
adamc@815
|
1250 | EJavaScript (m, e') =>
|
adamc@815
|
1251 (let
|
adamc@815
|
1252 val len = length outer
|
adamc@815
|
1253 fun str s = (EPrim (Prim.String s), #2 e')
|
adamc@815
|
1254
|
adamc@815
|
1255 val locals = List.tabulate
|
adamc@815
|
1256 (varDepth e',
|
adamc@815
|
1257 fn i => str ("var _" ^ Int.toString (len + i) ^ ";"))
|
adamc@815
|
1258
|
adamc@815
|
1259 val (e', st) = jsExp m outer 0 (e', st)
|
adamc@815
|
1260
|
adamc@815
|
1261 val e' =
|
adamc@815
|
1262 case locals of
|
adamc@815
|
1263 [] => e'
|
adamc@815
|
1264 | _ =>
|
adamc@815
|
1265 strcat (#2 e') (str "(function(){"
|
adamc@815
|
1266 :: locals
|
adamc@815
|
1267 @ [str "return ",
|
adamc@815
|
1268 e',
|
adamc@815
|
1269 str "}())"])
|
adamc@815
|
1270 in
|
adamc@815
|
1271 (e', st)
|
adamc@815
|
1272 end handle CantEmbed _ => (e, st))
|
adamc@815
|
1273
|
adamc@815
|
1274 | ESignalReturn e =>
|
adamc@815
|
1275 let
|
adamc@815
|
1276 val (e, st) = exp outer (e, st)
|
adamc@815
|
1277 in
|
adamc@815
|
1278 ((ESignalReturn e, loc), st)
|
adamc@815
|
1279 end
|
adamc@815
|
1280 | ESignalBind (e1, e2) =>
|
adamc@815
|
1281 let
|
adamc@815
|
1282 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1283 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1284 in
|
adamc@815
|
1285 ((ESignalBind (e1, e2), loc), st)
|
adamc@815
|
1286 end
|
adamc@815
|
1287 | ESignalSource e =>
|
adamc@815
|
1288 let
|
adamc@815
|
1289 val (e, st) = exp outer (e, st)
|
adamc@815
|
1290 in
|
adamc@815
|
1291 ((ESignalSource e, loc), st)
|
adamc@815
|
1292 end
|
adamc@815
|
1293
|
adamc@815
|
1294 | EServerCall (e1, e2, t, ef) =>
|
adamc@815
|
1295 let
|
adamc@815
|
1296 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1297 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1298 in
|
adamc@815
|
1299 ((EServerCall (e1, e2, t, ef), loc), st)
|
adamc@815
|
1300 end
|
adamc@815
|
1301 | ERecv (e1, e2, t) =>
|
adamc@815
|
1302 let
|
adamc@815
|
1303 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1304 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1305 in
|
adamc@815
|
1306 ((ERecv (e1, e2, t), loc), st)
|
adamc@815
|
1307 end
|
adamc@815
|
1308 | ESleep (e1, e2) =>
|
adamc@815
|
1309 let
|
adamc@815
|
1310 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1311 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1312 in
|
adamc@815
|
1313 ((ESleep (e1, e2), loc), st)
|
adamc@815
|
1314 end)
|
adamc@815
|
1315
|
adamc@815
|
1316 fun decl (d as (_, loc), st) =
|
adamc@815
|
1317 case #1 d of
|
adamc@815
|
1318 DVal (x, n, t, e, s) =>
|
adamc@815
|
1319 let
|
adamc@815
|
1320 val (e, st) = exp [] (e, st)
|
adamc@815
|
1321 in
|
adamc@815
|
1322 ((DVal (x, n, t, e, s), loc), st)
|
adamc@815
|
1323 end
|
adamc@815
|
1324 | DValRec vis =>
|
adamc@815
|
1325 let
|
adamc@815
|
1326 val (vis, st) = ListUtil.foldlMap (fn ((x, n, t, e, s), st) =>
|
adamc@815
|
1327 let
|
adamc@815
|
1328 val (e, st) = exp [] (e, st)
|
adamc@815
|
1329 in
|
adamc@815
|
1330 ((x, n, t, e, s), st)
|
adamc@815
|
1331 end) st vis
|
adamc@815
|
1332 in
|
adamc@815
|
1333 ((DValRec vis, loc), st)
|
adamc@815
|
1334 end
|
adamc@815
|
1335 | _ => (d, st)
|
adamc@567
|
1336
|
adamc@567
|
1337 fun doDecl (d, st) =
|
adamc@567
|
1338 let
|
adamc@815
|
1339 (*val () = Print.preface ("doDecl", MonoPrint.p_decl MonoEnv.empty d)*)
|
adamc@815
|
1340 val (d, st) = decl (d, st)
|
adamc@567
|
1341 in
|
adamc@567
|
1342 (List.revAppend (#decls st, [d]),
|
adamc@567
|
1343 {decls = [],
|
adamc@589
|
1344 script = #script st,
|
adamc@595
|
1345 included = #included st,
|
adamc@595
|
1346 injectors = #injectors st,
|
adamc@800
|
1347 listInjectors = #listInjectors st,
|
adamc@638
|
1348 decoders = #decoders st,
|
adamc@595
|
1349 maxName = #maxName st})
|
adamc@567
|
1350 end
|
adamc@567
|
1351
|
adamc@567
|
1352 val (ds, st) = ListUtil.foldlMapConcat doDecl
|
adamc@567
|
1353 {decls = [],
|
adamc@589
|
1354 script = [],
|
adamc@595
|
1355 included = IS.empty,
|
adamc@595
|
1356 injectors = IM.empty,
|
adamc@800
|
1357 listInjectors = TM.empty,
|
adamc@638
|
1358 decoders = IM.empty,
|
adamc@595
|
1359 maxName = U.File.maxName file + 1}
|
adamc@815
|
1360 file
|
adamc@569
|
1361
|
adamc@569
|
1362 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
|
adamc@569
|
1363 fun lines acc =
|
adamc@569
|
1364 case TextIO.inputLine inf of
|
adamc@569
|
1365 NONE => String.concat (rev acc)
|
adamc@569
|
1366 | SOME line => lines (line :: acc)
|
adamc@569
|
1367 val lines = lines []
|
adamc@794
|
1368
|
adamc@794
|
1369 val script =
|
adamc@794
|
1370 if !foundJavaScript then
|
adamc@794
|
1371 lines ^ String.concat (rev (#script st))
|
adamc@794
|
1372 else
|
adamc@794
|
1373 ""
|
adamc@567
|
1374 in
|
adamc@569
|
1375 TextIO.closeIn inf;
|
adamc@794
|
1376 (DJavaScript script, ErrorMsg.dummySpan) :: ds
|
adamc@567
|
1377 end
|
adamc@567
|
1378
|
adamc@567
|
1379 end
|