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