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@29
|
40
|
adamc@29
|
41 val declares : t -> (int * (string * L'.typ) list) list
|
adamc@453
|
42 val clearDeclares : t -> t
|
adamc@29
|
43 end = struct
|
adamc@29
|
44
|
adamc@29
|
45 structure FM = BinaryMapFn(struct
|
adamc@29
|
46 type ord_key = L.typ
|
adamc@109
|
47 val compare = MonoUtil.Typ.compare
|
adamc@29
|
48 end)
|
adamc@29
|
49
|
adamc@29
|
50 type t = int * int FM.map * (int * (string * L'.typ) list) list
|
adamc@29
|
51
|
adamc@102
|
52 val empty : t = (1, FM.insert (FM.empty, (L.TRecord [], ErrorMsg.dummySpan), 0), [])
|
adamc@29
|
53
|
adamc@29
|
54 fun find ((n, m, ds), xts, xts') =
|
adamc@29
|
55 let
|
adamc@29
|
56 val t = (L.TRecord xts, ErrorMsg.dummySpan)
|
adamc@29
|
57 in
|
adamc@29
|
58 case FM.find (m, t) of
|
adamc@29
|
59 NONE => ((n+1, FM.insert (m, t, n), (n, xts') :: ds), n)
|
adamc@29
|
60 | SOME i => ((n, m, ds), i)
|
adamc@29
|
61 end
|
adamc@29
|
62
|
adamc@29
|
63 fun declares (_, _, ds) = ds
|
adamc@29
|
64
|
adamc@453
|
65 fun clearDeclares (n, m, _) = (n, m, [])
|
adamc@453
|
66
|
adamc@29
|
67 end
|
adamc@29
|
68
|
adamc@196
|
69 fun cifyTyp x =
|
adamc@196
|
70 let
|
adamc@196
|
71 fun cify dtmap ((t, loc), sm) =
|
adamc@196
|
72 case t of
|
adamc@196
|
73 L.TFun (t1, t2) =>
|
adamc@196
|
74 let
|
adamc@196
|
75 val (t1, sm) = cify dtmap (t1, sm)
|
adamc@196
|
76 val (t2, sm) = cify dtmap (t2, sm)
|
adamc@196
|
77 in
|
adamc@196
|
78 ((L'.TFun (t1, t2), loc), sm)
|
adamc@196
|
79 end
|
adamc@196
|
80 | L.TRecord xts =>
|
adamc@196
|
81 let
|
adamc@196
|
82 val old_xts = xts
|
adamc@196
|
83 val (xts, sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
|
adamc@196
|
84 let
|
adamc@196
|
85 val (t, sm) = cify dtmap (t, sm)
|
adamc@196
|
86 in
|
adamc@196
|
87 ((x, t), sm)
|
adamc@196
|
88 end)
|
adamc@196
|
89 sm xts
|
adamc@196
|
90 val (sm, si) = Sm.find (sm, old_xts, xts)
|
adamc@196
|
91 in
|
adamc@196
|
92 ((L'.TRecord si, loc), sm)
|
adamc@196
|
93 end
|
adamc@196
|
94 | L.TDatatype (n, ref (dk, xncs)) =>
|
adamc@196
|
95 (case IM.find (dtmap, n) of
|
adamc@196
|
96 SOME r => ((L'.TDatatype (dk, n, r), loc), sm)
|
adamc@196
|
97 | NONE =>
|
adamc@196
|
98 let
|
adamc@196
|
99 val r = ref []
|
adamc@196
|
100 val dtmap = IM.insert (dtmap, n, r)
|
adamc@196
|
101
|
adamc@196
|
102 val (xncs, sm) = ListUtil.foldlMap (fn ((x, n, to), sm) =>
|
adamc@196
|
103 case to of
|
adamc@196
|
104 NONE => ((x, n, NONE), sm)
|
adamc@196
|
105 | SOME t =>
|
adamc@196
|
106 let
|
adamc@196
|
107 val (t, sm) = cify dtmap (t, sm)
|
adamc@196
|
108 in
|
adamc@196
|
109 ((x, n, SOME t), sm)
|
adamc@196
|
110 end)
|
adamc@196
|
111 sm xncs
|
adamc@196
|
112 in
|
adamc@196
|
113 r := xncs;
|
adamc@196
|
114 ((L'.TDatatype (dk, n, r), loc), sm)
|
adamc@196
|
115 end)
|
adamc@196
|
116 | L.TFfi mx => ((L'.TFfi mx, loc), sm)
|
adamc@288
|
117 | L.TOption t =>
|
adamc@288
|
118 let
|
adamc@288
|
119 val (t, sm) = cify dtmap (t, sm)
|
adamc@288
|
120 in
|
adamc@288
|
121 ((L'.TOption t, loc), sm)
|
adamc@288
|
122 end
|
adamc@577
|
123 | L.TSource => ((L'.TFfi ("Basis", "int"), loc), sm)
|
adamc@568
|
124 | L.TSignal _ => raise Fail "Cjrize: TSignal remains"
|
adamc@196
|
125 in
|
adamc@196
|
126 cify IM.empty x
|
adamc@196
|
127 end
|
adamc@29
|
128
|
adamc@109
|
129 val dummye = (L'.EPrim (Prim.Int 0), ErrorMsg.dummySpan)
|
adamc@109
|
130
|
adamc@186
|
131 fun cifyPatCon (pc, sm) =
|
adamc@181
|
132 case pc of
|
adamc@186
|
133 L.PConVar n => (L'.PConVar n, sm)
|
adamc@186
|
134 | L.PConFfi {mod = m, datatyp, con, arg} =>
|
adamc@186
|
135 let
|
adamc@186
|
136 val (arg, sm) =
|
adamc@186
|
137 case arg of
|
adamc@186
|
138 NONE => (NONE, sm)
|
adamc@186
|
139 | SOME t =>
|
adamc@186
|
140 let
|
adamc@186
|
141 val (t, sm) = cifyTyp (t, sm)
|
adamc@186
|
142 in
|
adamc@186
|
143 (SOME t, sm)
|
adamc@186
|
144 end
|
adamc@186
|
145 in
|
adamc@186
|
146 (L'.PConFfi {mod = m, datatyp = datatyp, con = con, arg = arg}, sm)
|
adamc@186
|
147 end
|
adamc@181
|
148
|
adamc@182
|
149 fun cifyPat ((p, loc), sm) =
|
adamc@181
|
150 case p of
|
adamc@182
|
151 L.PWild => ((L'.PWild, loc), sm)
|
adamc@182
|
152 | L.PVar (x, t) =>
|
adamc@182
|
153 let
|
adamc@182
|
154 val (t, sm) = cifyTyp (t, sm)
|
adamc@182
|
155 in
|
adamc@182
|
156 ((L'.PVar (x, t), loc), sm)
|
adamc@182
|
157 end
|
adamc@182
|
158 | L.PPrim p => ((L'.PPrim p, loc), sm)
|
adamc@188
|
159 | L.PCon (dk, pc, NONE) =>
|
adamc@186
|
160 let
|
adamc@186
|
161 val (pc, sm) = cifyPatCon (pc, sm)
|
adamc@186
|
162 in
|
adamc@188
|
163 ((L'.PCon (dk, pc, NONE), loc), sm)
|
adamc@186
|
164 end
|
adamc@188
|
165 | L.PCon (dk, pc, SOME p) =>
|
adamc@182
|
166 let
|
adamc@186
|
167 val (pc, sm) = cifyPatCon (pc, sm)
|
adamc@182
|
168 val (p, sm) = cifyPat (p, sm)
|
adamc@182
|
169 in
|
adamc@188
|
170 ((L'.PCon (dk, pc, SOME p), loc), sm)
|
adamc@182
|
171 end
|
adamc@182
|
172 | L.PRecord xps =>
|
adamc@182
|
173 let
|
adamc@182
|
174 val (xps, sm) = ListUtil.foldlMap (fn ((x, p, t), sm) =>
|
adamc@182
|
175 let
|
adamc@182
|
176 val (p, sm) = cifyPat (p, sm)
|
adamc@182
|
177 val (t, sm) = cifyTyp (t, sm)
|
adamc@182
|
178 in
|
adamc@182
|
179 ((x, p, t), sm)
|
adamc@182
|
180 end) sm xps
|
adamc@182
|
181 in
|
adamc@182
|
182 ((L'.PRecord xps, loc), sm)
|
adamc@182
|
183 end
|
adamc@288
|
184 | L.PNone t =>
|
adamc@288
|
185 let
|
adamc@288
|
186 val (t, sm) = cifyTyp (t, sm)
|
adamc@288
|
187 in
|
adamc@288
|
188 ((L'.PNone t, loc), sm)
|
adamc@288
|
189 end
|
adamc@288
|
190 | L.PSome (t, p) =>
|
adamc@288
|
191 let
|
adamc@288
|
192 val (t, sm) = cifyTyp (t, sm)
|
adamc@288
|
193 val (p, sm) = cifyPat (p, sm)
|
adamc@288
|
194 in
|
adamc@288
|
195 ((L'.PSome (t, p), loc), sm)
|
adamc@288
|
196 end
|
adamc@288
|
197
|
adamc@181
|
198
|
adamc@280
|
199 fun cifyExp (eAll as (e, loc), sm) =
|
adamc@29
|
200 case e of
|
adamc@29
|
201 L.EPrim p => ((L'.EPrim p, loc), sm)
|
adamc@29
|
202 | L.ERel n => ((L'.ERel n, loc), sm)
|
adamc@29
|
203 | L.ENamed n => ((L'.ENamed n, loc), sm)
|
adamc@188
|
204 | L.ECon (dk, pc, eo) =>
|
adamc@181
|
205 let
|
adamc@181
|
206 val (eo, sm) =
|
adamc@181
|
207 case eo of
|
adamc@181
|
208 NONE => (NONE, sm)
|
adamc@181
|
209 | SOME e =>
|
adamc@181
|
210 let
|
adamc@181
|
211 val (e, sm) = cifyExp (e, sm)
|
adamc@181
|
212 in
|
adamc@181
|
213 (SOME e, sm)
|
adamc@181
|
214 end
|
adamc@186
|
215 val (pc, sm) = cifyPatCon (pc, sm)
|
adamc@181
|
216 in
|
adamc@188
|
217 ((L'.ECon (dk, pc, eo), loc), sm)
|
adamc@181
|
218 end
|
adamc@297
|
219 | L.ENone t =>
|
adamc@297
|
220 let
|
adamc@297
|
221 val (t, sm) = cifyTyp (t, sm)
|
adamc@297
|
222 in
|
adamc@297
|
223 ((L'.ENone t, loc), sm)
|
adamc@297
|
224 end
|
adamc@290
|
225 | L.ESome (t, e) =>
|
adamc@290
|
226 let
|
adamc@290
|
227 val (t, sm) = cifyTyp (t, sm)
|
adamc@290
|
228 val (e, sm) = cifyExp (e, sm)
|
adamc@290
|
229 in
|
adamc@290
|
230 ((L'.ESome (t, e), loc), sm)
|
adamc@290
|
231 end
|
adamc@53
|
232 | L.EFfi mx => ((L'.EFfi mx, loc), sm)
|
adamc@53
|
233 | L.EFfiApp (m, x, es) =>
|
adamc@53
|
234 let
|
adamc@53
|
235 val (es, sm) = ListUtil.foldlMap cifyExp sm es
|
adamc@53
|
236 in
|
adamc@53
|
237 ((L'.EFfiApp (m, x, es), loc), sm)
|
adamc@53
|
238 end
|
adamc@29
|
239 | L.EApp (e1, e2) =>
|
adamc@29
|
240 let
|
adamc@316
|
241 fun unravel (e, args) =
|
adamc@316
|
242 case e of
|
adamc@316
|
243 (L.EApp (e1, e2), _) => unravel (e1, e2 :: args)
|
adamc@316
|
244 | _ => (e, args)
|
adamc@316
|
245
|
adamc@316
|
246 val (f, es) = unravel (e1, [e2])
|
adamc@316
|
247
|
adamc@316
|
248 val (f, sm) = cifyExp (f, sm)
|
adamc@316
|
249 val (es, sm) = ListUtil.foldlMap cifyExp sm es
|
adamc@29
|
250 in
|
adamc@316
|
251 ((L'.EApp (f, es), loc), sm)
|
adamc@29
|
252 end
|
adamc@109
|
253 | L.EAbs _ => (ErrorMsg.errorAt loc "Anonymous function remains at code generation";
|
adamc@280
|
254 Print.prefaces' [("Function", MonoPrint.p_exp MonoEnv.empty eAll)];
|
adamc@109
|
255 (dummye, sm))
|
adamc@29
|
256
|
adamc@387
|
257 | L.EUnop (s, e1) =>
|
adamc@387
|
258 let
|
adamc@387
|
259 val (e1, sm) = cifyExp (e1, sm)
|
adamc@387
|
260 in
|
adamc@387
|
261 ((L'.EUnop (s, e1), loc), sm)
|
adamc@387
|
262 end
|
adamc@387
|
263 | L.EBinop (s, e1, e2) =>
|
adamc@387
|
264 let
|
adamc@387
|
265 val (e1, sm) = cifyExp (e1, sm)
|
adamc@387
|
266 val (e2, sm) = cifyExp (e2, sm)
|
adamc@387
|
267 in
|
adamc@387
|
268 ((L'.EBinop (s, e1, e2), loc), sm)
|
adamc@387
|
269 end
|
adamc@387
|
270
|
adamc@29
|
271 | L.ERecord xes =>
|
adamc@29
|
272 let
|
adamc@29
|
273 val old_xts = map (fn (x, _, t) => (x, t)) xes
|
adamc@29
|
274
|
adamc@29
|
275 val (xets, sm) = ListUtil.foldlMap (fn ((x, e, t), sm) =>
|
adamc@29
|
276 let
|
adamc@29
|
277 val (e, sm) = cifyExp (e, sm)
|
adamc@29
|
278 val (t, sm) = cifyTyp (t, sm)
|
adamc@29
|
279 in
|
adamc@29
|
280 ((x, e, t), sm)
|
adamc@29
|
281 end)
|
adamc@29
|
282 sm xes
|
adamc@29
|
283
|
adamc@29
|
284 val (sm, si) = Sm.find (sm, old_xts, map (fn (x, _, t) => (x, t)) xets)
|
adamc@29
|
285
|
adamc@29
|
286 val xes = map (fn (x, e, _) => (x, e)) xets
|
adamc@29
|
287 val xes = ListMergeSort.sort (fn ((x1, _), (x2, _)) => String.compare (x1, x2) = GREATER) xes
|
adamc@29
|
288 in
|
adamc@29
|
289 ((L'.ERecord (si, xes), loc), sm)
|
adamc@29
|
290 end
|
adamc@29
|
291 | L.EField (e, x) =>
|
adamc@29
|
292 let
|
adamc@29
|
293 val (e, sm) = cifyExp (e, sm)
|
adamc@29
|
294 in
|
adamc@29
|
295 ((L'.EField (e, x), loc), sm)
|
adamc@29
|
296 end
|
adamc@29
|
297
|
adamc@182
|
298 | L.ECase (e, pes, {disc, result}) =>
|
adamc@181
|
299 let
|
adamc@181
|
300 val (e, sm) = cifyExp (e, sm)
|
adamc@181
|
301 val (pes, sm) = ListUtil.foldlMap
|
adamc@181
|
302 (fn ((p, e), sm) =>
|
adamc@181
|
303 let
|
adamc@181
|
304 val (e, sm) = cifyExp (e, sm)
|
adamc@182
|
305 val (p, sm) = cifyPat (p, sm)
|
adamc@181
|
306 in
|
adamc@182
|
307 ((p, e), sm)
|
adamc@181
|
308 end) sm pes
|
adamc@182
|
309 val (disc, sm) = cifyTyp (disc, sm)
|
adamc@182
|
310 val (result, sm) = cifyTyp (result, sm)
|
adamc@181
|
311 in
|
adamc@182
|
312 ((L'.ECase (e, pes, {disc = disc, result = result}), loc), sm)
|
adamc@181
|
313 end
|
adamc@178
|
314
|
adamc@283
|
315 | L.EError (e, t) =>
|
adamc@283
|
316 let
|
adamc@283
|
317 val (e, sm) = cifyExp (e, sm)
|
adamc@283
|
318 val (t, sm) = cifyTyp (t, sm)
|
adamc@283
|
319 in
|
adamc@283
|
320 ((L'.EError (e, t), loc), sm)
|
adamc@283
|
321 end
|
adamc@283
|
322
|
adamc@180
|
323 | L.EStrcat (e1, e2) =>
|
adamc@180
|
324 let
|
adamc@180
|
325 val (e1, sm) = cifyExp (e1, sm)
|
adamc@180
|
326 val (e2, sm) = cifyExp (e2, sm)
|
adamc@180
|
327 in
|
adamc@180
|
328 ((L'.EFfiApp ("Basis", "strcat", [e1, e2]), loc), sm)
|
adamc@180
|
329 end
|
adamc@102
|
330
|
adamc@102
|
331 | L.EWrite e =>
|
adamc@102
|
332 let
|
adamc@102
|
333 val (e, sm) = cifyExp (e, sm)
|
adamc@102
|
334 in
|
adamc@102
|
335 ((L'.EWrite e, loc), sm)
|
adamc@102
|
336 end
|
adamc@102
|
337
|
adamc@106
|
338 | L.ESeq (e1, e2) =>
|
adamc@106
|
339 let
|
adamc@106
|
340 val (e1, sm) = cifyExp (e1, sm)
|
adamc@106
|
341 val (e2, sm) = cifyExp (e2, sm)
|
adamc@106
|
342 in
|
adamc@106
|
343 ((L'.ESeq (e1, e2), loc), sm)
|
adamc@106
|
344 end
|
adamc@106
|
345
|
adamc@269
|
346 | L.ELet (x, t, e1, e2) =>
|
adamc@269
|
347 let
|
adamc@269
|
348 val (t, sm) = cifyTyp (t, sm)
|
adamc@269
|
349 val (e1, sm) = cifyExp (e1, sm)
|
adamc@269
|
350 val (e2, sm) = cifyExp (e2, sm)
|
adamc@269
|
351 in
|
adamc@269
|
352 ((L'.ELet (x, t, e1, e2), loc), sm)
|
adamc@269
|
353 end
|
adamc@251
|
354
|
adamc@111
|
355 | L.EClosure _ => (ErrorMsg.errorAt loc "Nested closure remains in code generation";
|
adamc@111
|
356 (dummye, sm))
|
adamc@111
|
357
|
adamc@269
|
358 | L.EQuery {exps, tables, state, query, body, initial} =>
|
adamc@269
|
359 let
|
adamc@269
|
360 val (exps', sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
|
adamc@269
|
361 let
|
adamc@269
|
362 val (t, sm) = cifyTyp (t, sm)
|
adamc@269
|
363 in
|
adamc@269
|
364 ((x, t), sm)
|
adamc@269
|
365 end) sm exps
|
adamc@269
|
366 val (tables', sm) = ListUtil.foldlMap (fn ((x, xts), sm) =>
|
adamc@269
|
367 let
|
adamc@269
|
368 val (xts, sm) = ListUtil.foldlMap
|
adamc@269
|
369 (fn ((x, t), sm) =>
|
adamc@269
|
370 let
|
adamc@269
|
371 val (t, sm) = cifyTyp (t, sm)
|
adamc@269
|
372 in
|
adamc@269
|
373 ((x, t), sm)
|
adamc@269
|
374 end) sm xts
|
adamc@269
|
375 in
|
adamc@269
|
376 ((x, xts), sm)
|
adamc@269
|
377 end) sm tables
|
adamc@269
|
378
|
adamc@269
|
379 val row = exps @ map (fn (x, xts) => (x, (L.TRecord xts, loc))) tables
|
adamc@269
|
380 val row = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) row
|
adamc@269
|
381
|
adamc@269
|
382 val (tableRows, sm) = ListUtil.foldlMap (fn (((x, xts), (_, xts')), sm) =>
|
adamc@269
|
383 let
|
adamc@269
|
384 val (sm, rnum) = Sm.find (sm, xts, xts')
|
adamc@269
|
385 in
|
adamc@269
|
386 ((x, rnum), sm)
|
adamc@269
|
387 end)
|
adamc@269
|
388 sm (ListPair.zip (tables, tables'))
|
adamc@269
|
389 val row' = exps' @ map (fn (x, n) => (x, (L'.TRecord n, loc))) tableRows
|
adamc@269
|
390 val row' = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) row'
|
adamc@269
|
391
|
adamc@269
|
392 val (sm, rnum) = Sm.find (sm, row, row')
|
adamc@269
|
393
|
adamc@269
|
394 val (state, sm) = cifyTyp (state, sm)
|
adamc@269
|
395 val (query, sm) = cifyExp (query, sm)
|
adamc@269
|
396 val (body, sm) = cifyExp (body, sm)
|
adamc@269
|
397 val (initial, sm) = cifyExp (initial, sm)
|
adamc@269
|
398 in
|
adamc@269
|
399 ((L'.EQuery {exps = exps', tables = tables', rnum = rnum, state = state,
|
adamc@282
|
400 query = query, body = body, initial = initial, prepared = NONE}, loc), sm)
|
adamc@269
|
401 end
|
adamc@269
|
402
|
adamc@307
|
403 | L.EDml e =>
|
adamc@307
|
404 let
|
adamc@307
|
405 val (e, sm) = cifyExp (e, sm)
|
adamc@307
|
406 in
|
adamc@307
|
407 ((L'.EDml {dml = e, prepared = NONE}, loc), sm)
|
adamc@307
|
408 end
|
adamc@307
|
409
|
adamc@338
|
410 | L.ENextval e =>
|
adamc@338
|
411 let
|
adamc@338
|
412 val (e, sm) = cifyExp (e, sm)
|
adamc@338
|
413 in
|
adamc@338
|
414 ((L'.ENextval {seq = e, prepared = NONE}, loc), sm)
|
adamc@338
|
415 end
|
adamc@338
|
416
|
adamc@463
|
417 | L.EUnurlify (e, t) =>
|
adamc@463
|
418 let
|
adamc@463
|
419 val (e, sm) = cifyExp (e, sm)
|
adamc@463
|
420 val (t, sm) = cifyTyp (t, sm)
|
adamc@463
|
421 in
|
adamc@463
|
422 ((L'.EUnurlify (e, t), loc), sm)
|
adamc@463
|
423 end
|
adamc@252
|
424
|
adamc@578
|
425 | L.EJavaScript (_, _, SOME e) => cifyExp (e, sm)
|
adamc@568
|
426 | L.EJavaScript _ => raise Fail "Cjrize: EJavaScript remains"
|
adamc@578
|
427
|
adamc@568
|
428 | L.ESignalReturn _ => raise Fail "Cjrize: ESignalReturn remains"
|
adamc@572
|
429 | L.ESignalBind _ => raise Fail "Cjrize: ESignalBind remains"
|
adamc@574
|
430 | L.ESignalSource _ => raise Fail "Cjrize: ESignalSource remains"
|
adamc@566
|
431
|
adamc@608
|
432 | L.EServerCall _ => raise Fail "Cjrize EServerCall"
|
adamc@608
|
433
|
adamc@29
|
434 fun cifyDecl ((d, loc), sm) =
|
adamc@29
|
435 case d of
|
adamc@165
|
436 L.DDatatype (x, n, xncs) =>
|
adamc@165
|
437 let
|
adamc@198
|
438 val dk = ElabUtil.classifyDatatype xncs
|
adamc@165
|
439 val (xncs, sm) = ListUtil.foldlMap (fn ((x, n, to), sm) =>
|
adamc@165
|
440 case to of
|
adamc@165
|
441 NONE => ((x, n, NONE), sm)
|
adamc@165
|
442 | SOME t =>
|
adamc@165
|
443 let
|
adamc@165
|
444 val (t, sm) = cifyTyp (t, sm)
|
adamc@165
|
445 in
|
adamc@165
|
446 ((x, n, SOME t), sm)
|
adamc@165
|
447 end) sm xncs
|
adamc@165
|
448 in
|
adamc@188
|
449 (SOME (L'.DDatatype (dk, x, n, xncs), loc), NONE, sm)
|
adamc@165
|
450 end
|
adamc@164
|
451
|
adamc@164
|
452 | L.DVal (x, n, t, e, _) =>
|
adamc@29
|
453 let
|
adamc@29
|
454 val (t, sm) = cifyTyp (t, sm)
|
adamc@109
|
455
|
adamc@109
|
456 val (d, sm) = case #1 t of
|
adamc@121
|
457 L'.TFun _ =>
|
adamc@121
|
458 let
|
adamc@121
|
459 fun unravel (tAll as (t, _), eAll as (e, _)) =
|
adamc@121
|
460 case (t, e) of
|
adamc@121
|
461 (L'.TFun (dom, ran), L.EAbs (ax, _, _, e)) =>
|
adamc@121
|
462 let
|
adamc@121
|
463 val (args, t, e) = unravel (ran, e)
|
adamc@121
|
464 in
|
adamc@121
|
465 ((ax, dom) :: args, t, e)
|
adamc@121
|
466 end
|
adamc@121
|
467 | (L'.TFun _, _) =>
|
adamc@121
|
468 (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
|
adamc@121
|
469 ([], tAll, eAll))
|
adamc@121
|
470 | _ => ([], tAll, eAll)
|
adamc@121
|
471
|
adamc@121
|
472 val (args, ran, e) = unravel (t, e)
|
adamc@121
|
473 val (e, sm) = cifyExp (e, sm)
|
adamc@121
|
474 in
|
adamc@121
|
475 (L'.DFun (x, n, args, ran, e), sm)
|
adamc@121
|
476 end
|
adamc@121
|
477
|
adamc@109
|
478 | _ =>
|
adamc@109
|
479 let
|
adamc@109
|
480 val (e, sm) = cifyExp (e, sm)
|
adamc@109
|
481 in
|
adamc@109
|
482 (L'.DVal (x, n, t, e), sm)
|
adamc@109
|
483 end
|
adamc@29
|
484 in
|
adamc@109
|
485 (SOME (d, loc), NONE, sm)
|
adamc@29
|
486 end
|
adamc@129
|
487 | L.DValRec vis =>
|
adamc@129
|
488 let
|
adamc@129
|
489 val (vis, sm) = ListUtil.foldlMap
|
adamc@129
|
490 (fn ((x, n, t, e, _), sm) =>
|
adamc@129
|
491 let
|
adamc@129
|
492 val (t, sm) = cifyTyp (t, sm)
|
adamc@129
|
493
|
adamc@129
|
494 fun unravel (tAll as (t, _), eAll as (e, _)) =
|
adamc@129
|
495 case (t, e) of
|
adamc@129
|
496 (L'.TFun (dom, ran), L.EAbs (ax, _, _, e)) =>
|
adamc@129
|
497 let
|
adamc@129
|
498 val (args, t, e) = unravel (ran, e)
|
adamc@129
|
499 in
|
adamc@129
|
500 ((ax, dom) :: args, t, e)
|
adamc@129
|
501 end
|
adamc@129
|
502 | (L'.TFun _, _) =>
|
adamc@129
|
503 (ErrorMsg.errorAt loc "Function isn't explicit at code generation";
|
adamc@129
|
504 ([], tAll, eAll))
|
adamc@129
|
505 | _ => ([], tAll, eAll)
|
adamc@129
|
506
|
adamc@129
|
507 val (args, ran, e) = unravel (t, e)
|
adamc@129
|
508 val (e, sm) = cifyExp (e, sm)
|
adamc@129
|
509 in
|
adamc@129
|
510 ((x, n, args, ran, e), sm)
|
adamc@129
|
511 end)
|
adamc@129
|
512 sm vis
|
adamc@129
|
513 in
|
adamc@129
|
514 (SOME (L'.DFunRec vis, loc), NONE, sm)
|
adamc@129
|
515 end
|
adamc@129
|
516
|
adamc@609
|
517 | L.DExport (ek, s, n, ts, t) =>
|
adamc@120
|
518 let
|
adamc@120
|
519 val (ts, sm) = ListUtil.foldlMap cifyTyp sm ts
|
adamc@609
|
520 val (t, sm) = cifyTyp (t, sm)
|
adamc@120
|
521 in
|
adamc@643
|
522 (NONE, SOME (ek, "/" ^ s, n, ts, t, L'.ServerAndClient), sm)
|
adamc@120
|
523 end
|
adamc@29
|
524
|
adamc@273
|
525 | L.DTable (s, xts) =>
|
adamc@273
|
526 let
|
adamc@273
|
527 val (xts, sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
|
adamc@273
|
528 let
|
adamc@273
|
529 val (t, sm) = cifyTyp (t, sm)
|
adamc@273
|
530 in
|
adamc@273
|
531 ((x, t), sm)
|
adamc@273
|
532 end) sm xts
|
adamc@273
|
533 in
|
adamc@273
|
534 (SOME (L'.DTable (s, xts), loc), NONE, sm)
|
adamc@273
|
535 end
|
adamc@338
|
536 | L.DSequence s =>
|
adamc@338
|
537 (SOME (L'.DSequence s, loc), NONE, sm)
|
adamc@271
|
538 | L.DDatabase s => (SOME (L'.DDatabase s, loc), NONE, sm)
|
adamc@569
|
539 | L.DJavaScript s => (SOME (L'.DJavaScript s, loc), NONE, sm)
|
adamc@271
|
540
|
adamc@29
|
541 fun cjrize ds =
|
adamc@29
|
542 let
|
adamc@196
|
543 val (dsF, ds, ps, sm) = foldl (fn (d, (dsF, ds, ps, sm)) =>
|
adamc@196
|
544 let
|
adamc@196
|
545 val (dop, pop, sm) = cifyDecl (d, sm)
|
adamc@453
|
546
|
adamc@640
|
547 val dsF = case dop of
|
adamc@640
|
548 SOME (L'.DDatatype (dk, x, n, _), loc) =>
|
adamc@640
|
549 (L'.DDatatypeForward (dk, x, n), loc) :: dsF
|
adamc@640
|
550 | _ => dsF
|
adamc@640
|
551
|
adamc@640
|
552 val dsF = map (fn v => (L'.DStruct v, ErrorMsg.dummySpan)) (Sm.declares sm)
|
adamc@640
|
553 @ dsF
|
adamc@640
|
554
|
adamc@196
|
555 val (dsF, ds) = case dop of
|
adamc@196
|
556 NONE => (dsF, ds)
|
adamc@453
|
557 | SOME (d as (L'.DDatatype _, loc)) =>
|
adamc@453
|
558 (d :: dsF, ds)
|
adamc@196
|
559 | SOME d => (dsF, d :: ds)
|
adamc@453
|
560
|
adamc@196
|
561 val ps = case pop of
|
adamc@196
|
562 NONE => ps
|
adamc@196
|
563 | SOME p => p :: ps
|
adamc@196
|
564 in
|
adamc@453
|
565 (dsF, ds, ps, Sm.clearDeclares sm)
|
adamc@196
|
566 end)
|
adamc@196
|
567 ([], [], [], Sm.empty) ds
|
adamc@29
|
568 in
|
adamc@453
|
569 (List.revAppend (dsF, rev ds),
|
adamc@101
|
570 ps)
|
adamc@29
|
571 end
|
adamc@29
|
572
|
adamc@29
|
573 end
|