adamc@29
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@29
|
2 * All rights reserved.
|
adamc@29
|
3 *
|
adamc@29
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@29
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@29
|
6 *
|
adamc@29
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@29
|
8 * this list of conditions and the following disclaimer.
|
adamc@29
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@29
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@29
|
11 * and/or other materials provided with the distribution.
|
adamc@29
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@29
|
13 * derived from this software without specific prior written permission.
|
adamc@29
|
14 *
|
adamc@29
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@29
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@29
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@29
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@29
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@29
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@29
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@29
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@29
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@29
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@29
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@29
|
26 *)
|
adamc@29
|
27
|
adamc@29
|
28 structure Cjrize :> CJRIZE = struct
|
adamc@29
|
29
|
adamc@109
|
30 structure L = Mono
|
adamc@29
|
31 structure L' = Cjr
|
adamc@29
|
32
|
adamc@196
|
33 structure IM = IntBinaryMap
|
adamc@196
|
34
|
adamc@29
|
35 structure Sm :> sig
|
adamc@29
|
36 type t
|
adamc@29
|
37
|
adamc@29
|
38 val empty : t
|
adamc@29
|
39 val find : t * (string * L.typ) list * (string * L'.typ) list -> t * int
|
adamc@757
|
40 val findList : t * L.typ * L'.typ -> t * int
|
adamc@29
|
41
|
adamc@29
|
42 val declares : t -> (int * (string * L'.typ) list) list
|
adamc@453
|
43 val clearDeclares : t -> t
|
adamc@29
|
44 end = struct
|
adamc@29
|
45
|
adamc@29
|
46 structure FM = BinaryMapFn(struct
|
adamc@29
|
47 type ord_key = L.typ
|
adamc@109
|
48 val compare = MonoUtil.Typ.compare
|
adamc@29
|
49 end)
|
adamc@29
|
50
|
adamc@757
|
51 type t = {
|
adamc@757
|
52 count : int,
|
adamc@757
|
53 normal : int FM.map,
|
adamc@757
|
54 lists : int FM.map,
|
adamc@757
|
55 decls : (int * (string * L'.typ) list) list
|
adamc@757
|
56 }
|
adamc@29
|
57
|
adamc@757
|
58 val empty : t = {
|
adamc@757
|
59 count = 1,
|
adamc@757
|
60 normal = FM.insert (FM.empty, (L.TRecord [], ErrorMsg.dummySpan), 0),
|
adamc@757
|
61 lists = FM.empty,
|
adamc@757
|
62 decls = []
|
adamc@757
|
63 }
|
adamc@29
|
64
|
adamc@757
|
65 fun find (v as {count, normal, decls, lists}, xts, xts') =
|
adamc@29
|
66 let
|
adamc@29
|
67 val t = (L.TRecord xts, ErrorMsg.dummySpan)
|
adamc@29
|
68 in
|
adamc@757
|
69 case FM.find (normal, t) of
|
adamc@757
|
70 SOME i => (v, i)
|
adamc@757
|
71 | NONE => ({count = count+1,
|
adamc@757
|
72 normal = FM.insert (normal, t, count),
|
adamc@757
|
73 lists = lists,
|
adamc@757
|
74 decls = (count, xts') :: decls},
|
adamc@757
|
75 count)
|
adamc@29
|
76 end
|
adamc@29
|
77
|
adamc@757
|
78 fun findList (v as {count, normal, decls, lists}, t, t') =
|
adamc@757
|
79 case FM.find (lists, t) of
|
adamc@757
|
80 SOME i => (v, i)
|
adamc@757
|
81 | NONE =>
|
adamc@757
|
82 let
|
adamc@757
|
83 val xts = [("1", t), ("2", (L.TList t, #2 t))]
|
adamc@757
|
84 val xts' = [("1", t'), ("2", (L'.TList (t', count), #2 t'))]
|
adamc@757
|
85 in
|
adamc@757
|
86 ({count = count+1,
|
adamc@757
|
87 normal = FM.insert (normal, (L.TRecord xts, ErrorMsg.dummySpan), count),
|
adamc@757
|
88 lists = FM.insert (lists, t, count),
|
adamc@757
|
89 decls = (count, xts') :: decls},
|
adamc@757
|
90 count)
|
adamc@757
|
91 end
|
adamc@29
|
92
|
adamc@757
|
93 fun declares (v : t) = #decls v
|
adamc@757
|
94
|
adamc@757
|
95 fun clearDeclares (v : t) = {count = #count v,
|
adamc@757
|
96 normal = #normal v,
|
adamc@757
|
97 lists = #lists v,
|
adamc@757
|
98 decls = []}
|
adamc@453
|
99
|
adamc@29
|
100 end
|
adamc@29
|
101
|
adamc@196
|
102 fun cifyTyp x =
|
adamc@196
|
103 let
|
adamc@196
|
104 fun cify dtmap ((t, loc), sm) =
|
adamc@196
|
105 case t of
|
adamc@196
|
106 L.TFun (t1, t2) =>
|
adamc@196
|
107 let
|
adamc@196
|
108 val (t1, sm) = cify dtmap (t1, sm)
|
adamc@196
|
109 val (t2, sm) = cify dtmap (t2, sm)
|
adamc@196
|
110 in
|
adamc@196
|
111 ((L'.TFun (t1, t2), loc), sm)
|
adamc@196
|
112 end
|
adamc@196
|
113 | L.TRecord xts =>
|
adamc@196
|
114 let
|
adamc@196
|
115 val old_xts = xts
|
adamc@196
|
116 val (xts, sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
|
adamc@196
|
117 let
|
adamc@196
|
118 val (t, sm) = cify dtmap (t, sm)
|
adamc@196
|
119 in
|
adamc@196
|
120 ((x, t), sm)
|
adamc@196
|
121 end)
|
adamc@196
|
122 sm xts
|
adamc@196
|
123 val (sm, si) = Sm.find (sm, old_xts, xts)
|
adamc@196
|
124 in
|
adamc@196
|
125 ((L'.TRecord si, loc), sm)
|
adamc@196
|
126 end
|
adamc@196
|
127 | L.TDatatype (n, ref (dk, xncs)) =>
|
adamc@196
|
128 (case IM.find (dtmap, n) of
|
adamc@196
|
129 SOME r => ((L'.TDatatype (dk, n, r), loc), sm)
|
adamc@196
|
130 | NONE =>
|
adamc@196
|
131 let
|
adamc@196
|
132 val r = ref []
|
adamc@196
|
133 val dtmap = IM.insert (dtmap, n, r)
|
adamc@196
|
134
|
adamc@196
|
135 val (xncs, sm) = ListUtil.foldlMap (fn ((x, n, to), sm) =>
|
adamc@196
|
136 case to of
|
adamc@196
|
137 NONE => ((x, n, NONE), sm)
|
adamc@196
|
138 | SOME t =>
|
adamc@196
|
139 let
|
adamc@196
|
140 val (t, sm) = cify dtmap (t, sm)
|
adamc@196
|
141 in
|
adamc@196
|
142 ((x, n, SOME t), sm)
|
adamc@196
|
143 end)
|
adamc@196
|
144 sm xncs
|
adamc@196
|
145 in
|
adamc@196
|
146 r := xncs;
|
adamc@196
|
147 ((L'.TDatatype (dk, n, r), loc), sm)
|
adamc@196
|
148 end)
|
adamc@196
|
149 | L.TFfi mx => ((L'.TFfi mx, loc), sm)
|
adamc@288
|
150 | L.TOption t =>
|
adamc@288
|
151 let
|
adamc@288
|
152 val (t, sm) = cify dtmap (t, sm)
|
adamc@288
|
153 in
|
adamc@288
|
154 ((L'.TOption t, loc), sm)
|
adamc@288
|
155 end
|
adamc@757
|
156 | L.TList t =>
|
adamc@757
|
157 let
|
adamc@757
|
158 val (t', sm) = cify dtmap (t, sm)
|
adamc@757
|
159 val (sm, si) = Sm.findList (sm, t, t')
|
adamc@757
|
160 in
|
adamc@757
|
161 ((L'.TList (t', si), loc), sm)
|
adamc@757
|
162 end
|
adamc@577
|
163 | L.TSource => ((L'.TFfi ("Basis", "int"), loc), sm)
|
adamc@568
|
164 | L.TSignal _ => raise Fail "Cjrize: TSignal remains"
|
adamc@196
|
165 in
|
adamc@196
|
166 cify IM.empty x
|
adamc@196
|
167 end
|
adamc@29
|
168
|
adamc@109
|
169 val dummye = (L'.EPrim (Prim.Int 0), ErrorMsg.dummySpan)
|
adamc@109
|
170
|
adamc@186
|
171 fun cifyPatCon (pc, sm) =
|
adamc@181
|
172 case pc of
|
adamc@186
|
173 L.PConVar n => (L'.PConVar n, sm)
|
adamc@186
|
174 | L.PConFfi {mod = m, datatyp, con, arg} =>
|
adamc@186
|
175 let
|
adamc@186
|
176 val (arg, sm) =
|
adamc@186
|
177 case arg of
|
adamc@186
|
178 NONE => (NONE, sm)
|
adamc@186
|
179 | SOME t =>
|
adamc@186
|
180 let
|
adamc@186
|
181 val (t, sm) = cifyTyp (t, sm)
|
adamc@186
|
182 in
|
adamc@186
|
183 (SOME t, sm)
|
adamc@186
|
184 end
|
adamc@186
|
185 in
|
adamc@186
|
186 (L'.PConFfi {mod = m, datatyp = datatyp, con = con, arg = arg}, sm)
|
adamc@186
|
187 end
|
adamc@181
|
188
|
adamc@182
|
189 fun cifyPat ((p, loc), sm) =
|
adamc@181
|
190 case p of
|
adamc@182
|
191 L.PWild => ((L'.PWild, loc), sm)
|
adamc@182
|
192 | L.PVar (x, t) =>
|
adamc@182
|
193 let
|
adamc@182
|
194 val (t, sm) = cifyTyp (t, sm)
|
adamc@182
|
195 in
|
adamc@182
|
196 ((L'.PVar (x, t), loc), sm)
|
adamc@182
|
197 end
|
adamc@182
|
198 | L.PPrim p => ((L'.PPrim p, loc), sm)
|
adamc@188
|
199 | L.PCon (dk, pc, NONE) =>
|
adamc@186
|
200 let
|
adamc@186
|
201 val (pc, sm) = cifyPatCon (pc, sm)
|
adamc@186
|
202 in
|
adamc@188
|
203 ((L'.PCon (dk, pc, NONE), loc), sm)
|
adamc@186
|
204 end
|
adamc@188
|
205 | L.PCon (dk, pc, SOME p) =>
|
adamc@182
|
206 let
|
adamc@186
|
207 val (pc, sm) = cifyPatCon (pc, sm)
|
adamc@182
|
208 val (p, sm) = cifyPat (p, sm)
|
adamc@182
|
209 in
|
adamc@188
|
210 ((L'.PCon (dk, pc, SOME p), loc), sm)
|
adamc@182
|
211 end
|
adamc@182
|
212 | L.PRecord xps =>
|
adamc@182
|
213 let
|
adamc@182
|
214 val (xps, sm) = ListUtil.foldlMap (fn ((x, p, t), sm) =>
|
adamc@182
|
215 let
|
adamc@182
|
216 val (p, sm) = cifyPat (p, sm)
|
adamc@182
|
217 val (t, sm) = cifyTyp (t, sm)
|
adamc@182
|
218 in
|
adamc@182
|
219 ((x, p, t), sm)
|
adamc@182
|
220 end) sm xps
|
adamc@182
|
221 in
|
adamc@182
|
222 ((L'.PRecord xps, loc), sm)
|
adamc@182
|
223 end
|
adamc@288
|
224 | L.PNone t =>
|
adamc@288
|
225 let
|
adamc@288
|
226 val (t, sm) = cifyTyp (t, sm)
|
adamc@288
|
227 in
|
adamc@288
|
228 ((L'.PNone t, loc), sm)
|
adamc@288
|
229 end
|
adamc@288
|
230 | L.PSome (t, p) =>
|
adamc@288
|
231 let
|
adamc@288
|
232 val (t, sm) = cifyTyp (t, sm)
|
adamc@288
|
233 val (p, sm) = cifyPat (p, sm)
|
adamc@288
|
234 in
|
adamc@288
|
235 ((L'.PSome (t, p), loc), sm)
|
adamc@288
|
236 end
|
adamc@288
|
237
|
adamc@181
|
238
|
adamc@280
|
239 fun cifyExp (eAll as (e, loc), sm) =
|
adamc@29
|
240 case e of
|
adamc@29
|
241 L.EPrim p => ((L'.EPrim p, loc), sm)
|
adamc@29
|
242 | L.ERel n => ((L'.ERel n, loc), sm)
|
adamc@29
|
243 | L.ENamed n => ((L'.ENamed n, loc), sm)
|
adamc@188
|
244 | L.ECon (dk, pc, eo) =>
|
adamc@181
|
245 let
|
adamc@181
|
246 val (eo, sm) =
|
adamc@181
|
247 case eo of
|
adamc@181
|
248 NONE => (NONE, sm)
|
adamc@181
|
249 | SOME e =>
|
adamc@181
|
250 let
|
adamc@181
|
251 val (e, sm) = cifyExp (e, sm)
|
adamc@181
|
252 in
|
adamc@181
|
253 (SOME e, sm)
|
adamc@181
|
254 end
|
adamc@186
|
255 val (pc, sm) = cifyPatCon (pc, sm)
|
adamc@181
|
256 in
|
adamc@188
|
257 ((L'.ECon (dk, pc, eo), loc), sm)
|
adamc@181
|
258 end
|
adamc@297
|
259 | L.ENone t =>
|
adamc@297
|
260 let
|
adamc@297
|
261 val (t, sm) = cifyTyp (t, sm)
|
adamc@297
|
262 in
|
adamc@297
|
263 ((L'.ENone t, loc), sm)
|
adamc@297
|
264 end
|
adamc@290
|
265 | L.ESome (t, e) =>
|
adamc@290
|
266 let
|
adamc@290
|
267 val (t, sm) = cifyTyp (t, sm)
|
adamc@290
|
268 val (e, sm) = cifyExp (e, sm)
|
adamc@290
|
269 in
|
adamc@290
|
270 ((L'.ESome (t, e), loc), sm)
|
adamc@290
|
271 end
|
adamc@53
|
272 | L.EFfi mx => ((L'.EFfi mx, loc), sm)
|
adamc@53
|
273 | L.EFfiApp (m, x, es) =>
|
adamc@53
|
274 let
|
adamc@53
|
275 val (es, sm) = ListUtil.foldlMap cifyExp sm es
|
adamc@53
|
276 in
|
adamc@53
|
277 ((L'.EFfiApp (m, x, es), loc), sm)
|
adamc@53
|
278 end
|
adamc@29
|
279 | L.EApp (e1, e2) =>
|
adamc@29
|
280 let
|
adamc@316
|
281 fun unravel (e, args) =
|
adamc@316
|
282 case e of
|
adamc@316
|
283 (L.EApp (e1, e2), _) => unravel (e1, e2 :: args)
|
adamc@316
|
284 | _ => (e, args)
|
adamc@316
|
285
|
adamc@316
|
286 val (f, es) = unravel (e1, [e2])
|
adamc@316
|
287
|
adamc@316
|
288 val (f, sm) = cifyExp (f, sm)
|
adamc@316
|
289 val (es, sm) = ListUtil.foldlMap cifyExp sm es
|
adamc@29
|
290 in
|
adamc@316
|
291 ((L'.EApp (f, es), loc), sm)
|
adamc@29
|
292 end
|
adamc@109
|
293 | L.EAbs _ => (ErrorMsg.errorAt loc "Anonymous function remains at code generation";
|
adamc@280
|
294 Print.prefaces' [("Function", MonoPrint.p_exp MonoEnv.empty eAll)];
|
adamc@109
|
295 (dummye, sm))
|
adamc@29
|
296
|
adamc@387
|
297 | L.EUnop (s, e1) =>
|
adamc@387
|
298 let
|
adamc@387
|
299 val (e1, sm) = cifyExp (e1, sm)
|
adamc@387
|
300 in
|
adamc@387
|
301 ((L'.EUnop (s, e1), loc), sm)
|
adamc@387
|
302 end
|
adamc@387
|
303 | L.EBinop (s, e1, e2) =>
|
adamc@387
|
304 let
|
adamc@387
|
305 val (e1, sm) = cifyExp (e1, sm)
|
adamc@387
|
306 val (e2, sm) = cifyExp (e2, sm)
|
adamc@387
|
307 in
|
adamc@387
|
308 ((L'.EBinop (s, e1, e2), loc), sm)
|
adamc@387
|
309 end
|
adamc@387
|
310
|
adamc@29
|
311 | L.ERecord xes =>
|
adamc@29
|
312 let
|
adamc@29
|
313 val old_xts = map (fn (x, _, t) => (x, t)) xes
|
adamc@29
|
314
|
adamc@29
|
315 val (xets, sm) = ListUtil.foldlMap (fn ((x, e, t), sm) =>
|
adamc@29
|
316 let
|
adamc@29
|
317 val (e, sm) = cifyExp (e, sm)
|
adamc@29
|
318 val (t, sm) = cifyTyp (t, sm)
|
adamc@29
|
319 in
|
adamc@29
|
320 ((x, e, t), sm)
|
adamc@29
|
321 end)
|
adamc@29
|
322 sm xes
|
adamc@29
|
323
|
adamc@29
|
324 val (sm, si) = Sm.find (sm, old_xts, map (fn (x, _, t) => (x, t)) xets)
|
adamc@29
|
325
|
adamc@29
|
326 val xes = map (fn (x, e, _) => (x, e)) xets
|
adamc@29
|
327 val xes = ListMergeSort.sort (fn ((x1, _), (x2, _)) => String.compare (x1, x2) = GREATER) xes
|
adamc@29
|
328 in
|
adamc@29
|
329 ((L'.ERecord (si, xes), loc), sm)
|
adamc@29
|
330 end
|
adamc@29
|
331 | L.EField (e, x) =>
|
adamc@29
|
332 let
|
adamc@29
|
333 val (e, sm) = cifyExp (e, sm)
|
adamc@29
|
334 in
|
adamc@29
|
335 ((L'.EField (e, x), loc), sm)
|
adamc@29
|
336 end
|
adamc@29
|
337
|
adamc@182
|
338 | L.ECase (e, pes, {disc, result}) =>
|
adamc@181
|
339 let
|
adamc@181
|
340 val (e, sm) = cifyExp (e, sm)
|
adamc@181
|
341 val (pes, sm) = ListUtil.foldlMap
|
adamc@181
|
342 (fn ((p, e), sm) =>
|
adamc@181
|
343 let
|
adamc@181
|
344 val (e, sm) = cifyExp (e, sm)
|
adamc@182
|
345 val (p, sm) = cifyPat (p, sm)
|
adamc@181
|
346 in
|
adamc@182
|
347 ((p, e), sm)
|
adamc@181
|
348 end) sm pes
|
adamc@182
|
349 val (disc, sm) = cifyTyp (disc, sm)
|
adamc@182
|
350 val (result, sm) = cifyTyp (result, sm)
|
adamc@181
|
351 in
|
adamc@182
|
352 ((L'.ECase (e, pes, {disc = disc, result = result}), loc), sm)
|
adamc@181
|
353 end
|
adamc@178
|
354
|
adamc@283
|
355 | L.EError (e, t) =>
|
adamc@283
|
356 let
|
adamc@283
|
357 val (e, sm) = cifyExp (e, sm)
|
adamc@283
|
358 val (t, sm) = cifyTyp (t, sm)
|
adamc@283
|
359 in
|
adamc@283
|
360 ((L'.EError (e, t), loc), sm)
|
adamc@283
|
361 end
|
adamc@741
|
362 | L.EReturnBlob {blob, mimeType, t} =>
|
adamc@741
|
363 let
|
adamc@741
|
364 val (blob, sm) = cifyExp (blob, sm)
|
adamc@741
|
365 val (mimeType, sm) = cifyExp (mimeType, sm)
|
adamc@741
|
366 val (t, sm) = cifyTyp (t, sm)
|
adamc@741
|
367 in
|
adamc@741
|
368 ((L'.EReturnBlob {blob = blob, mimeType = mimeType, t = t}, loc), sm)
|
adamc@741
|
369 end
|
adamc@283
|
370
|
adamc@180
|
371 | L.EStrcat (e1, e2) =>
|
adamc@180
|
372 let
|
adamc@180
|
373 val (e1, sm) = cifyExp (e1, sm)
|
adamc@180
|
374 val (e2, sm) = cifyExp (e2, sm)
|
adamc@180
|
375 in
|
adamc@180
|
376 ((L'.EFfiApp ("Basis", "strcat", [e1, e2]), loc), sm)
|
adamc@180
|
377 end
|
adamc@102
|
378
|
adamc@102
|
379 | L.EWrite e =>
|
adamc@102
|
380 let
|
adamc@102
|
381 val (e, sm) = cifyExp (e, sm)
|
adamc@102
|
382 in
|
adamc@102
|
383 ((L'.EWrite e, loc), sm)
|
adamc@102
|
384 end
|
adamc@102
|
385
|
adamc@106
|
386 | L.ESeq (e1, e2) =>
|
adamc@106
|
387 let
|
adamc@106
|
388 val (e1, sm) = cifyExp (e1, sm)
|
adamc@106
|
389 val (e2, sm) = cifyExp (e2, sm)
|
adamc@106
|
390 in
|
adamc@106
|
391 ((L'.ESeq (e1, e2), loc), sm)
|
adamc@106
|
392 end
|
adamc@106
|
393
|
adamc@269
|
394 | L.ELet (x, t, e1, e2) =>
|
adamc@269
|
395 let
|
adamc@269
|
396 val (t, sm) = cifyTyp (t, sm)
|
adamc@269
|
397 val (e1, sm) = cifyExp (e1, sm)
|
adamc@269
|
398 val (e2, sm) = cifyExp (e2, sm)
|
adamc@269
|
399 in
|
adamc@269
|
400 ((L'.ELet (x, t, e1, e2), loc), sm)
|
adamc@269
|
401 end
|
adamc@251
|
402
|
adamc@111
|
403 | L.EClosure _ => (ErrorMsg.errorAt loc "Nested closure remains in code generation";
|
adamc@111
|
404 (dummye, sm))
|
adamc@111
|
405
|
adamc@269
|
406 | L.EQuery {exps, tables, state, query, body, initial} =>
|
adamc@269
|
407 let
|
adamc@269
|
408 val (exps', sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
|
adamc@269
|
409 let
|
adamc@269
|
410 val (t, sm) = cifyTyp (t, sm)
|
adamc@269
|
411 in
|
adamc@269
|
412 ((x, t), sm)
|
adamc@269
|
413 end) sm exps
|
adamc@269
|
414 val (tables', sm) = ListUtil.foldlMap (fn ((x, xts), sm) =>
|
adamc@269
|
415 let
|
adamc@269
|
416 val (xts, sm) = ListUtil.foldlMap
|
adamc@269
|
417 (fn ((x, t), sm) =>
|
adamc@269
|
418 let
|
adamc@269
|
419 val (t, sm) = cifyTyp (t, sm)
|
adamc@269
|
420 in
|
adamc@269
|
421 ((x, t), sm)
|
adamc@269
|
422 end) sm xts
|
adamc@269
|
423 in
|
adamc@269
|
424 ((x, xts), sm)
|
adamc@269
|
425 end) sm tables
|
adamc@269
|
426
|
adamc@269
|
427 val row = exps @ map (fn (x, xts) => (x, (L.TRecord xts, loc))) tables
|
adamc@269
|
428 val row = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) row
|
adamc@269
|
429
|
adamc@269
|
430 val (tableRows, sm) = ListUtil.foldlMap (fn (((x, xts), (_, xts')), sm) =>
|
adamc@269
|
431 let
|
adamc@269
|
432 val (sm, rnum) = Sm.find (sm, xts, xts')
|
adamc@269
|
433 in
|
adamc@269
|
434 ((x, rnum), sm)
|
adamc@269
|
435 end)
|
adamc@269
|
436 sm (ListPair.zip (tables, tables'))
|
adamc@269
|
437 val row' = exps' @ map (fn (x, n) => (x, (L'.TRecord n, loc))) tableRows
|
adamc@269
|
438 val row' = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) row'
|
adamc@269
|
439
|
adamc@269
|
440 val (sm, rnum) = Sm.find (sm, row, row')
|
adamc@269
|
441
|
adamc@269
|
442 val (state, sm) = cifyTyp (state, sm)
|
adamc@269
|
443 val (query, sm) = cifyExp (query, sm)
|
adamc@269
|
444 val (body, sm) = cifyExp (body, sm)
|
adamc@269
|
445 val (initial, sm) = cifyExp (initial, sm)
|
adamc@269
|
446 in
|
adamc@269
|
447 ((L'.EQuery {exps = exps', tables = tables', rnum = rnum, state = state,
|
adamc@282
|
448 query = query, body = body, initial = initial, prepared = NONE}, loc), sm)
|
adamc@269
|
449 end
|
adamc@269
|
450
|
adamc@307
|
451 | L.EDml e =>
|
adamc@307
|
452 let
|
adamc@307
|
453 val (e, sm) = cifyExp (e, sm)
|
adamc@307
|
454 in
|
adamc@307
|
455 ((L'.EDml {dml = e, prepared = NONE}, loc), sm)
|
adamc@307
|
456 end
|
adamc@307
|
457
|
adamc@338
|
458 | L.ENextval e =>
|
adamc@338
|
459 let
|
adamc@338
|
460 val (e, sm) = cifyExp (e, sm)
|
adamc@338
|
461 in
|
adamc@338
|
462 ((L'.ENextval {seq = e, prepared = NONE}, loc), sm)
|
adamc@338
|
463 end
|
adamc@338
|
464
|
adamc@463
|
465 | L.EUnurlify (e, t) =>
|
adamc@463
|
466 let
|
adamc@463
|
467 val (e, sm) = cifyExp (e, sm)
|
adamc@463
|
468 val (t, sm) = cifyTyp (t, sm)
|
adamc@463
|
469 in
|
adamc@463
|
470 ((L'.EUnurlify (e, t), loc), sm)
|
adamc@463
|
471 end
|
adamc@252
|
472
|
adamc@578
|
473 | L.EJavaScript (_, _, SOME e) => cifyExp (e, sm)
|
adamc@568
|
474 | L.EJavaScript _ => raise Fail "Cjrize: EJavaScript remains"
|
adamc@578
|
475
|
adamc@568
|
476 | L.ESignalReturn _ => raise Fail "Cjrize: ESignalReturn remains"
|
adamc@572
|
477 | L.ESignalBind _ => raise Fail "Cjrize: ESignalBind remains"
|
adamc@574
|
478 | L.ESignalSource _ => raise Fail "Cjrize: ESignalSource remains"
|
adamc@566
|
479
|
adamc@608
|
480 | L.EServerCall _ => raise Fail "Cjrize EServerCall"
|
adamc@670
|
481 | L.ERecv _ => raise Fail "Cjrize ERecv"
|
adamc@695
|
482 | L.ESleep _ => raise Fail "Cjrize ESleep"
|
adamc@608
|
483
|
adamc@29
|
484 fun cifyDecl ((d, loc), sm) =
|
adamc@29
|
485 case d of
|
adamc@809
|
486 L.DDatatype dts =>
|
adamc@165
|
487 let
|
adamc@809
|
488 val (dts, sm) = ListUtil.foldlMap
|
adamc@809
|
489 (fn ((x, n, xncs), sm) =>
|
adamc@809
|
490 let
|
adamc@809
|
491 val dk = ElabUtil.classifyDatatype xncs
|
adamc@809
|
492 val (xncs, sm) = ListUtil.foldlMap (fn ((x, n, to), sm) =>
|
adamc@809
|
493 case to of
|
adamc@809
|
494 NONE => ((x, n, NONE), sm)
|
adamc@809
|
495 | SOME t =>
|
adamc@809
|
496 let
|
adamc@809
|
497 val (t, sm) = cifyTyp (t, sm)
|
adamc@809
|
498 in
|
adamc@809
|
499 ((x, n, SOME t), sm)
|
adamc@809
|
500 end) sm xncs
|
adamc@809
|
501 in
|
adamc@809
|
502 ((dk, x, n, xncs), sm)
|
adamc@809
|
503 end)
|
adamc@809
|
504 sm dts
|
adamc@165
|
505 in
|
adamc@809
|
506 (SOME (L'.DDatatype dts, loc), NONE, sm)
|
adamc@809
|
507 end
|
adamc@164
|
508
|
adamc@164
|
509 | L.DVal (x, n, t, e, _) =>
|
adamc@29
|
510 let
|
adamc@29
|
511 val (t, sm) = cifyTyp (t, sm)
|
adamc@109
|
512
|
adamc@109
|
513 val (d, sm) = case #1 t of
|
adamc@121
|
514 L'.TFun _ =>
|
adamc@121
|
515 let
|
adamc@121
|
516 fun unravel (tAll as (t, _), eAll as (e, _)) =
|
adamc@121
|
517 case (t, e) of
|
adamc@121
|
518 (L'.TFun (dom, ran), L.EAbs (ax, _, _, e)) =>
|
adamc@121
|
519 let
|
adamc@121
|
520 val (args, t, e) = unravel (ran, e)
|
adamc@121
|
521 in
|
adamc@121
|
522 ((ax, dom) :: args, t, e)
|
adamc@121
|
523 end
|
adamc@121
|
524 | (L'.TFun _, _) =>
|
adamc@121
|
525 (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
|
adamc@121
|
526 ([], tAll, eAll))
|
adamc@121
|
527 | _ => ([], tAll, eAll)
|
adamc@121
|
528
|
adamc@121
|
529 val (args, ran, e) = unravel (t, e)
|
adamc@121
|
530 val (e, sm) = cifyExp (e, sm)
|
adamc@121
|
531 in
|
adamc@121
|
532 (L'.DFun (x, n, args, ran, e), sm)
|
adamc@121
|
533 end
|
adamc@121
|
534
|
adamc@109
|
535 | _ =>
|
adamc@109
|
536 let
|
adamc@109
|
537 val (e, sm) = cifyExp (e, sm)
|
adamc@109
|
538 in
|
adamc@109
|
539 (L'.DVal (x, n, t, e), sm)
|
adamc@109
|
540 end
|
adamc@29
|
541 in
|
adamc@109
|
542 (SOME (d, loc), NONE, sm)
|
adamc@29
|
543 end
|
adamc@129
|
544 | L.DValRec vis =>
|
adamc@129
|
545 let
|
adamc@129
|
546 val (vis, sm) = ListUtil.foldlMap
|
adamc@129
|
547 (fn ((x, n, t, e, _), sm) =>
|
adamc@129
|
548 let
|
adamc@129
|
549 val (t, sm) = cifyTyp (t, sm)
|
adamc@129
|
550
|
adamc@129
|
551 fun unravel (tAll as (t, _), eAll as (e, _)) =
|
adamc@129
|
552 case (t, e) of
|
adamc@129
|
553 (L'.TFun (dom, ran), L.EAbs (ax, _, _, e)) =>
|
adamc@129
|
554 let
|
adamc@129
|
555 val (args, t, e) = unravel (ran, e)
|
adamc@129
|
556 in
|
adamc@129
|
557 ((ax, dom) :: args, t, e)
|
adamc@129
|
558 end
|
adamc@129
|
559 | (L'.TFun _, _) =>
|
adamc@129
|
560 (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
|
adamc@129
|
561 ([], tAll, eAll))
|
adamc@129
|
562 | _ => ([], tAll, eAll)
|
adamc@129
|
563
|
adamc@129
|
564 val (args, ran, e) = unravel (t, e)
|
adamc@129
|
565 val (e, sm) = cifyExp (e, sm)
|
adamc@129
|
566 in
|
adamc@129
|
567 ((x, n, args, ran, e), sm)
|
adamc@129
|
568 end)
|
adamc@129
|
569 sm vis
|
adamc@129
|
570 in
|
adamc@129
|
571 (SOME (L'.DFunRec vis, loc), NONE, sm)
|
adamc@129
|
572 end
|
adamc@129
|
573
|
adamc@609
|
574 | L.DExport (ek, s, n, ts, t) =>
|
adamc@120
|
575 let
|
adamc@120
|
576 val (ts, sm) = ListUtil.foldlMap cifyTyp sm ts
|
adamc@609
|
577 val (t, sm) = cifyTyp (t, sm)
|
adamc@120
|
578 in
|
adamc@693
|
579 (NONE, SOME (ek, "/" ^ s, n, ts, t, L'.ServerAndPullAndPush), sm)
|
adamc@120
|
580 end
|
adamc@29
|
581
|
adamc@707
|
582 | L.DTable (s, xts, pe, ce) =>
|
adamc@273
|
583 let
|
adamc@273
|
584 val (xts, sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
|
adamc@273
|
585 let
|
adamc@273
|
586 val (t, sm) = cifyTyp (t, sm)
|
adamc@273
|
587 in
|
adamc@273
|
588 ((x, t), sm)
|
adamc@273
|
589 end) sm xts
|
adamc@704
|
590
|
adamc@704
|
591 fun flatten e =
|
adamc@704
|
592 case #1 e of
|
adamc@704
|
593 L.ERecord [] => []
|
adamc@704
|
594 | L.ERecord [(x, (L.EPrim (Prim.String v), _), _)] => [(x, v)]
|
adamc@704
|
595 | L.EStrcat (e1, e2) => flatten e1 @ flatten e2
|
adamc@704
|
596 | _ => (ErrorMsg.errorAt loc "Constraint has not been fully determined";
|
adamc@704
|
597 Print.prefaces "Undetermined constraint"
|
adamc@707
|
598 [("e", MonoPrint.p_exp MonoEnv.empty e)];
|
adamc@704
|
599 [])
|
adamc@707
|
600
|
adamc@707
|
601 val pe = case #1 pe of
|
adamc@707
|
602 L.EPrim (Prim.String s) => s
|
adamc@707
|
603 | _ => (ErrorMsg.errorAt loc "Primary key has not been fully determined";
|
adamc@707
|
604 Print.prefaces "Undetermined constraint"
|
adamc@707
|
605 [("e", MonoPrint.p_exp MonoEnv.empty pe)];
|
adamc@707
|
606 "")
|
adamc@273
|
607 in
|
adamc@707
|
608 (SOME (L'.DTable (s, xts, pe, flatten ce), loc), NONE, sm)
|
adamc@273
|
609 end
|
adamc@338
|
610 | L.DSequence s =>
|
adamc@338
|
611 (SOME (L'.DSequence s, loc), NONE, sm)
|
adamc@754
|
612 | L.DView (s, xts, e) =>
|
adamc@754
|
613 let
|
adamc@754
|
614 val (xts, sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
|
adamc@754
|
615 let
|
adamc@754
|
616 val (t, sm) = cifyTyp (t, sm)
|
adamc@754
|
617 in
|
adamc@754
|
618 ((x, t), sm)
|
adamc@754
|
619 end) sm xts
|
adamc@754
|
620
|
adamc@754
|
621 fun flatten e =
|
adamc@754
|
622 case #1 e of
|
adamc@754
|
623 L.ERecord [] => []
|
adamc@754
|
624 | L.ERecord [(x, (L.EPrim (Prim.String v), _), _)] => [(x, v)]
|
adamc@754
|
625 | L.EStrcat (e1, e2) => flatten e1 @ flatten e2
|
adamc@754
|
626 | _ => (ErrorMsg.errorAt loc "Constraint has not been fully determined";
|
adamc@754
|
627 Print.prefaces "Undetermined constraint"
|
adamc@754
|
628 [("e", MonoPrint.p_exp MonoEnv.empty e)];
|
adamc@754
|
629 [])
|
adamc@754
|
630
|
adamc@754
|
631 val e = case #1 e of
|
adamc@754
|
632 L.EPrim (Prim.String s) => s
|
adamc@754
|
633 | _ => (ErrorMsg.errorAt loc "VIEW query has not been fully determined";
|
adamc@754
|
634 Print.prefaces "Undetermined VIEW query"
|
adamc@754
|
635 [("e", MonoPrint.p_exp MonoEnv.empty e)];
|
adamc@754
|
636 "")
|
adamc@754
|
637 in
|
adamc@754
|
638 (SOME (L'.DView (s, xts, e), loc), NONE, sm)
|
adamc@754
|
639 end
|
adamc@271
|
640 | L.DDatabase s => (SOME (L'.DDatabase s, loc), NONE, sm)
|
adamc@569
|
641 | L.DJavaScript s => (SOME (L'.DJavaScript s, loc), NONE, sm)
|
adamc@725
|
642 | L.DCookie args => (SOME (L'.DCookie args, loc), NONE, sm)
|
adamc@718
|
643 | L.DStyle args => (SOME (L'.DStyle args, loc), NONE, sm)
|
adamc@271
|
644
|
adamc@29
|
645 fun cjrize ds =
|
adamc@29
|
646 let
|
adamc@196
|
647 val (dsF, ds, ps, sm) = foldl (fn (d, (dsF, ds, ps, sm)) =>
|
adamc@196
|
648 let
|
adamc@196
|
649 val (dop, pop, sm) = cifyDecl (d, sm)
|
adamc@453
|
650
|
adamc@640
|
651 val dsF = case dop of
|
adamc@809
|
652 SOME (L'.DDatatype dts, loc) =>
|
adamc@809
|
653 map (fn (dk, x, n, _) =>
|
adamc@809
|
654 (L'.DDatatypeForward (dk, x, n), loc)) dts @ dsF
|
adamc@640
|
655 | _ => dsF
|
adamc@640
|
656
|
adamc@640
|
657 val dsF = map (fn v => (L'.DStruct v, ErrorMsg.dummySpan)) (Sm.declares sm)
|
adamc@640
|
658 @ dsF
|
adamc@640
|
659
|
adamc@196
|
660 val (dsF, ds) = case dop of
|
adamc@196
|
661 NONE => (dsF, ds)
|
adamc@453
|
662 | SOME (d as (L'.DDatatype _, loc)) =>
|
adamc@453
|
663 (d :: dsF, ds)
|
adamc@196
|
664 | SOME d => (dsF, d :: ds)
|
adamc@453
|
665
|
adamc@196
|
666 val ps = case pop of
|
adamc@196
|
667 NONE => ps
|
adamc@196
|
668 | SOME p => p :: ps
|
adamc@196
|
669 in
|
adamc@453
|
670 (dsF, ds, ps, Sm.clearDeclares sm)
|
adamc@196
|
671 end)
|
adamc@196
|
672 ([], [], [], Sm.empty) ds
|
adamc@29
|
673 in
|
adamc@453
|
674 (List.revAppend (dsF, rev ds),
|
adamc@101
|
675 ps)
|
adamc@29
|
676 end
|
adamc@29
|
677
|
adamc@29
|
678 end
|