adamc@16
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@16
|
2 * All rights reserved.
|
adamc@16
|
3 *
|
adamc@16
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@16
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@16
|
6 *
|
adamc@16
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@16
|
8 * this list of conditions and the following disclaimer.
|
adamc@16
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@16
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@16
|
11 * and/or other materials provided with the distribution.
|
adamc@16
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@16
|
13 * derived from this software without specific prior written permission.
|
adamc@16
|
14 *
|
adamc@16
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@16
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@16
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@16
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@16
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@16
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@16
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@16
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@16
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@16
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@16
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@16
|
26 *)
|
adamc@16
|
27
|
adamc@244
|
28 (* Pretty-printing core Ur/Web *)
|
adamc@16
|
29
|
adamc@16
|
30 structure CorePrint :> CORE_PRINT = struct
|
adamc@16
|
31
|
adamc@16
|
32 open Print.PD
|
adamc@16
|
33 open Print
|
adamc@16
|
34
|
adamc@16
|
35 open Core
|
adamc@16
|
36
|
adamc@16
|
37 structure E = CoreEnv
|
adamc@16
|
38
|
adamc@16
|
39 val debug = ref false
|
adamc@16
|
40
|
adamc@626
|
41 fun p_kind' par env (k, _) =
|
adamc@16
|
42 case k of
|
adamc@16
|
43 KType => string "Type"
|
adamc@626
|
44 | KArrow (k1, k2) => parenIf par (box [p_kind' true env k1,
|
adamc@16
|
45 space,
|
adamc@16
|
46 string "->",
|
adamc@16
|
47 space,
|
adamc@626
|
48 p_kind env k2])
|
adamc@16
|
49 | KName => string "Name"
|
adamc@626
|
50 | KRecord k => box [string "{", p_kind env k, string "}"]
|
adamc@87
|
51 | KUnit => string "Unit"
|
adamc@214
|
52 | KTuple ks => box [string "(",
|
adamc@626
|
53 p_list_sep (box [space, string "*", space]) (p_kind env) ks,
|
adamc@214
|
54 string ")"]
|
adamc@16
|
55
|
adamc@626
|
56 | KRel n => ((if !debug then
|
adamc@626
|
57 string (E.lookupKRel env n ^ "_" ^ Int.toString n)
|
adamc@626
|
58 else
|
adamc@626
|
59 string (E.lookupKRel env n))
|
adamc@626
|
60 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
|
adamc@626
|
61 | KFun (x, k) => box [string x,
|
adamc@626
|
62 space,
|
adamc@626
|
63 string "-->",
|
adamc@626
|
64 space,
|
adamc@626
|
65 p_kind (E.pushKRel env x) k]
|
adamc@626
|
66
|
adamc@626
|
67 and p_kind env = p_kind' false env
|
adamc@16
|
68
|
adamc@16
|
69 fun p_con' par env (c, _) =
|
adamc@16
|
70 case c of
|
adamc@16
|
71 TFun (t1, t2) => parenIf par (box [p_con' true env t1,
|
adamc@16
|
72 space,
|
adamc@16
|
73 string "->",
|
adamc@16
|
74 space,
|
adamc@16
|
75 p_con env t2])
|
adamc@16
|
76 | TCFun (x, k, c) => parenIf par (box [string x,
|
adamc@16
|
77 space,
|
adamc@16
|
78 string "::",
|
adamc@16
|
79 space,
|
adamc@626
|
80 p_kind env k,
|
adamc@16
|
81 space,
|
adamc@16
|
82 string "->",
|
adamc@16
|
83 space,
|
adamc@16
|
84 p_con (E.pushCRel env x k) c])
|
adamc@16
|
85 | TRecord (CRecord (_, xcs), _) => box [string "{",
|
adamc@16
|
86 p_list (fn (x, c) =>
|
adamc@20
|
87 box [p_name env x,
|
adamc@16
|
88 space,
|
adamc@16
|
89 string ":",
|
adamc@16
|
90 space,
|
adamc@16
|
91 p_con env c]) xcs,
|
adamc@16
|
92 string "}"]
|
adamc@16
|
93 | TRecord c => box [string "$",
|
adamc@16
|
94 p_con' true env c]
|
adamc@16
|
95
|
adamc@16
|
96 | CRel n =>
|
adamc@39
|
97 ((if !debug then
|
adamc@39
|
98 string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n)
|
adamc@39
|
99 else
|
adamc@39
|
100 string (#1 (E.lookupCRel env n)))
|
adamc@39
|
101 handle E.UnboundRel _ => string ("UNBOUND_" ^ Int.toString n))
|
adamc@16
|
102 | CNamed n =>
|
adamc@39
|
103 ((if !debug then
|
adamc@39
|
104 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
|
adamc@39
|
105 else
|
adamc@39
|
106 string (#1 (E.lookupCNamed env n)))
|
adamc@480
|
107 handle E.UnboundNamed _ => string ("UNBOUNDN_" ^ Int.toString n))
|
adamc@48
|
108 | CFfi (m, x) => box [string "FFI(", string m, string ".", string x, string ")"]
|
adamc@16
|
109
|
adamc@16
|
110 | CApp (c1, c2) => parenIf par (box [p_con env c1,
|
adamc@16
|
111 space,
|
adamc@16
|
112 p_con' true env c2])
|
adamc@16
|
113 | CAbs (x, k, c) => parenIf par (box [string "fn",
|
adamc@16
|
114 space,
|
adamc@16
|
115 string x,
|
adamc@16
|
116 space,
|
adamc@16
|
117 string "::",
|
adamc@16
|
118 space,
|
adamc@626
|
119 p_kind env k,
|
adamc@16
|
120 space,
|
adamc@16
|
121 string "=>",
|
adamc@16
|
122 space,
|
adamc@16
|
123 p_con (E.pushCRel env x k) c])
|
adamc@16
|
124
|
adamc@16
|
125 | CName s => box [string "#", string s]
|
adamc@16
|
126
|
adamc@16
|
127 | CRecord (k, xcs) =>
|
adamc@16
|
128 if !debug then
|
adamc@16
|
129 parenIf par (box [string "[",
|
adamc@16
|
130 p_list (fn (x, c) =>
|
adamc@16
|
131 box [p_con env x,
|
adamc@16
|
132 space,
|
adamc@16
|
133 string "=",
|
adamc@16
|
134 space,
|
adamc@16
|
135 p_con env c]) xcs,
|
adamc@16
|
136 string "]::",
|
adamc@626
|
137 p_kind env k])
|
adamc@16
|
138 else
|
adamc@16
|
139 parenIf par (box [string "[",
|
adamc@16
|
140 p_list (fn (x, c) =>
|
adamc@16
|
141 box [p_con env x,
|
adamc@16
|
142 space,
|
adamc@16
|
143 string "=",
|
adamc@16
|
144 space,
|
adamc@16
|
145 p_con env c]) xcs,
|
adamc@16
|
146 string "]"])
|
adamc@16
|
147 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
|
adamc@16
|
148 space,
|
adamc@16
|
149 string "++",
|
adamc@16
|
150 space,
|
adamc@16
|
151 p_con env c2])
|
adamc@621
|
152 | CMap _ => string "map"
|
adamc@87
|
153 | CUnit => string "()"
|
adamc@214
|
154
|
adamc@214
|
155 | CTuple cs => box [string "(",
|
adamc@214
|
156 p_list (p_con env) cs,
|
adamc@214
|
157 string ")"]
|
adamc@214
|
158 | CProj (c, n) => box [p_con env c,
|
adamc@214
|
159 string ".",
|
adamc@214
|
160 string (Int.toString n)]
|
adamc@626
|
161
|
adamc@626
|
162 | CKAbs (x, c) => box [string x,
|
adamc@626
|
163 space,
|
adamc@626
|
164 string "==>",
|
adamc@626
|
165 space,
|
adamc@626
|
166 p_con (E.pushKRel env x) c]
|
adamc@626
|
167 | CKApp (c, k) => box [p_con env c,
|
adamc@626
|
168 string "[[",
|
adamc@626
|
169 p_kind env k,
|
adamc@626
|
170 string "]]"]
|
adamc@626
|
171 | TKFun (x, c) => box [string x,
|
adamc@626
|
172 space,
|
adamc@626
|
173 string "-->",
|
adamc@626
|
174 space,
|
adamc@626
|
175 p_con (E.pushKRel env x) c]
|
adamc@16
|
176
|
adamc@16
|
177 and p_con env = p_con' false env
|
adamc@16
|
178
|
adamc@20
|
179 and p_name env (all as (c, _)) =
|
adamc@20
|
180 case c of
|
adamc@20
|
181 CName s => string s
|
adamc@20
|
182 | _ => p_con env all
|
adamc@20
|
183
|
adamc@109
|
184 fun p_enamed env n =
|
adamc@109
|
185 (if !debug then
|
adamc@109
|
186 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
|
adamc@109
|
187 else
|
adamc@109
|
188 string (#1 (E.lookupENamed env n)))
|
adamc@109
|
189 handle E.UnboundNamed _ => string ("UNBOUNDN_" ^ Int.toString n)
|
adamc@109
|
190
|
adamc@177
|
191 fun p_con_named env n =
|
adamc@177
|
192 (if !debug then
|
adamc@177
|
193 string (#1 (E.lookupConstructor env n) ^ "__" ^ Int.toString n)
|
adamc@177
|
194 else
|
adamc@177
|
195 string (#1 (E.lookupConstructor env n)))
|
adamc@177
|
196 handle E.UnboundNamed _ => string ("CONSTRUCTOR_" ^ Int.toString n)
|
adamc@177
|
197
|
adamc@177
|
198 fun p_patCon env pc =
|
adamc@177
|
199 case pc of
|
adamc@177
|
200 PConVar n => p_con_named env n
|
adamc@757
|
201 | PConFfi {mod = m, con, arg, ...} =>
|
adamc@757
|
202 if !debug then
|
adamc@757
|
203 box [string "FFIC[",
|
adamc@757
|
204 case arg of
|
adamc@757
|
205 NONE => box []
|
adamc@757
|
206 | SOME t => p_con env t,
|
adamc@757
|
207 string "](",
|
adamc@757
|
208 string m,
|
adamc@757
|
209 string ".",
|
adamc@757
|
210 string con,
|
adamc@757
|
211 string ")"]
|
adamc@757
|
212 else
|
adamc@757
|
213 box [string "FFIC(",
|
adamc@757
|
214 string m,
|
adamc@757
|
215 string ".",
|
adamc@757
|
216 string con,
|
adamc@757
|
217 string ")"]
|
adamc@177
|
218
|
adamc@177
|
219 fun p_pat' par env (p, _) =
|
adamc@177
|
220 case p of
|
adamc@177
|
221 PWild => string "_"
|
adamc@182
|
222 | PVar (s, _) => string s
|
adamc@177
|
223 | PPrim p => Prim.p_t p
|
adamc@192
|
224 | PCon (_, n, _, NONE) => p_patCon env n
|
adamc@192
|
225 | PCon (_, n, _, SOME p) => parenIf par (box [p_patCon env n,
|
adamc@192
|
226 space,
|
adamc@192
|
227 p_pat' true env p])
|
adamc@177
|
228 | PRecord xps =>
|
adamc@177
|
229 box [string "{",
|
adamc@182
|
230 p_list_sep (box [string ",", space]) (fn (x, p, _) =>
|
adamc@177
|
231 box [string x,
|
adamc@177
|
232 space,
|
adamc@177
|
233 string "=",
|
adamc@177
|
234 space,
|
adamc@177
|
235 p_pat env p]) xps,
|
adamc@177
|
236 string "}"]
|
adamc@177
|
237
|
adamc@177
|
238 and p_pat x = p_pat' false x
|
adamc@177
|
239
|
adamc@16
|
240 fun p_exp' par env (e, _) =
|
adamc@16
|
241 case e of
|
adamc@16
|
242 EPrim p => Prim.p_t p
|
adamc@16
|
243 | ERel n =>
|
adamc@39
|
244 ((if !debug then
|
adamc@39
|
245 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
|
adamc@39
|
246 else
|
adamc@39
|
247 string (#1 (E.lookupERel env n)))
|
adamc@39
|
248 handle E.UnboundRel _ => string ("UNBOUND_" ^ Int.toString n))
|
adamc@109
|
249 | ENamed n => p_enamed env n
|
adamc@194
|
250 | ECon (_, pc, ts, NONE) => box [string "[",
|
adamc@194
|
251 p_patCon env pc,
|
adamc@194
|
252 p_list_sep (box []) (fn t => box [space, string "[", p_con env t, string "]"]) ts,
|
adamc@194
|
253 string "]"]
|
adamc@194
|
254 | ECon (_, pc, ts, SOME e) => box [string "[",
|
adamc@193
|
255 p_patCon env pc,
|
adamc@193
|
256 space,
|
adamc@193
|
257 p_exp' true env e,
|
adamc@194
|
258 p_list_sep (box []) (fn t => box [space, string "[", p_con env t, string "]"]) ts,
|
adamc@193
|
259 string "]"]
|
adamc@48
|
260 | EFfi (m, x) => box [string "FFI(", string m, string ".", string x, string ")"]
|
adamc@48
|
261 | EFfiApp (m, x, es) => box [string "FFI(",
|
adamc@48
|
262 string m,
|
adamc@48
|
263 string ".",
|
adamc@48
|
264 string x,
|
adamc@48
|
265 string "(",
|
adamc@48
|
266 p_list (p_exp env) es,
|
adamc@48
|
267 string "))"]
|
adamc@487
|
268 | EApp (e1, e2) => parenIf par (box [p_exp' true env e1,
|
adamc@16
|
269 space,
|
adamc@16
|
270 p_exp' true env e2])
|
adamc@26
|
271 | EAbs (x, t, _, e) => parenIf par (box [string "fn",
|
adamc@26
|
272 space,
|
adamc@26
|
273 string x,
|
adamc@26
|
274 space,
|
adamc@26
|
275 string ":",
|
adamc@26
|
276 space,
|
adamc@26
|
277 p_con env t,
|
adamc@26
|
278 space,
|
adamc@26
|
279 string "=>",
|
adamc@26
|
280 space,
|
adamc@26
|
281 p_exp (E.pushERel env x t) e])
|
adamc@16
|
282 | ECApp (e, c) => parenIf par (box [p_exp env e,
|
adamc@16
|
283 space,
|
adamc@16
|
284 string "[",
|
adamc@16
|
285 p_con env c,
|
adamc@16
|
286 string "]"])
|
adamc@16
|
287 | ECAbs (x, k, e) => parenIf par (box [string "fn",
|
adamc@16
|
288 space,
|
adamc@16
|
289 string x,
|
adamc@16
|
290 space,
|
adamc@16
|
291 string "::",
|
adamc@16
|
292 space,
|
adamc@626
|
293 p_kind env k,
|
adamc@16
|
294 space,
|
adamc@16
|
295 string "=>",
|
adamc@16
|
296 space,
|
adamc@16
|
297 p_exp (E.pushCRel env x k) e])
|
adamc@16
|
298
|
adamc@16
|
299 | ERecord xes => box [string "{",
|
adamc@29
|
300 p_list (fn (x, e, _) =>
|
adamc@21
|
301 box [p_name env x,
|
adamc@16
|
302 space,
|
adamc@16
|
303 string "=",
|
adamc@16
|
304 space,
|
adamc@16
|
305 p_exp env e]) xes,
|
adamc@16
|
306 string "}"]
|
adamc@16
|
307 | EField (e, c, {field, rest}) =>
|
adamc@16
|
308 if !debug then
|
adamc@16
|
309 box [p_exp' true env e,
|
adamc@16
|
310 string ".",
|
adamc@16
|
311 p_con' true env c,
|
adamc@16
|
312 space,
|
adamc@16
|
313 string "[",
|
adamc@16
|
314 p_con env field,
|
adamc@16
|
315 space,
|
adamc@16
|
316 string " in ",
|
adamc@16
|
317 space,
|
adamc@16
|
318 p_con env rest,
|
adamc@16
|
319 string "]"]
|
adamc@16
|
320 else
|
adamc@16
|
321 box [p_exp' true env e,
|
adamc@16
|
322 string ".",
|
adamc@16
|
323 p_con' true env c]
|
adamc@445
|
324 | EConcat (e1, c1, e2, c2) =>
|
adamc@339
|
325 parenIf par (if !debug then
|
adamc@445
|
326 box [p_exp' true env e1,
|
adamc@445
|
327 space,
|
adamc@445
|
328 string ":",
|
adamc@445
|
329 space,
|
adamc@445
|
330 p_con env c1,
|
adamc@445
|
331 space,
|
adamc@445
|
332 string "++",
|
adamc@445
|
333 space,
|
adamc@445
|
334 p_exp' true env e2,
|
adamc@445
|
335 space,
|
adamc@445
|
336 string ":",
|
adamc@445
|
337 space,
|
adamc@445
|
338 p_con env c2]
|
adamc@445
|
339 else
|
adamc@445
|
340 box [p_exp' true env e1,
|
adamc@339
|
341 space,
|
adamc@339
|
342 string "with",
|
adamc@339
|
343 space,
|
adamc@339
|
344 p_exp' true env e2])
|
adamc@149
|
345 | ECut (e, c, {field, rest}) =>
|
adamc@149
|
346 parenIf par (if !debug then
|
adamc@149
|
347 box [p_exp' true env e,
|
adamc@149
|
348 space,
|
adamc@149
|
349 string "--",
|
adamc@149
|
350 space,
|
adamc@149
|
351 p_con' true env c,
|
adamc@149
|
352 space,
|
adamc@149
|
353 string "[",
|
adamc@149
|
354 p_con env field,
|
adamc@149
|
355 space,
|
adamc@149
|
356 string " in ",
|
adamc@149
|
357 space,
|
adamc@149
|
358 p_con env rest,
|
adamc@149
|
359 string "]"]
|
adamc@149
|
360 else
|
adamc@149
|
361 box [p_exp' true env e,
|
adamc@149
|
362 space,
|
adamc@149
|
363 string "--",
|
adamc@149
|
364 space,
|
adamc@149
|
365 p_con' true env c])
|
adamc@493
|
366 | ECutMulti (e, c, {rest}) =>
|
adamc@493
|
367 parenIf par (if !debug then
|
adamc@493
|
368 box [p_exp' true env e,
|
adamc@493
|
369 space,
|
adamc@493
|
370 string "---",
|
adamc@493
|
371 space,
|
adamc@493
|
372 p_con' true env c,
|
adamc@493
|
373 space,
|
adamc@493
|
374 string "[",
|
adamc@493
|
375 p_con env rest,
|
adamc@493
|
376 string "]"]
|
adamc@493
|
377 else
|
adamc@493
|
378 box [p_exp' true env e,
|
adamc@493
|
379 space,
|
adamc@493
|
380 string "---",
|
adamc@493
|
381 space,
|
adamc@493
|
382 p_con' true env c])
|
adamc@16
|
383
|
adamc@288
|
384 | ECase (e, pes, {disc, result}) =>
|
adamc@288
|
385 parenIf par (box [string "case",
|
adamc@288
|
386 space,
|
adamc@288
|
387 p_exp env e,
|
adamc@288
|
388 space,
|
adamc@288
|
389 if !debug then
|
adamc@288
|
390 box [string "in",
|
adamc@288
|
391 space,
|
adamc@288
|
392 p_con env disc,
|
adamc@288
|
393 space,
|
adamc@288
|
394 string "return",
|
adamc@288
|
395 space,
|
adamc@288
|
396 p_con env result,
|
adamc@288
|
397 space]
|
adamc@288
|
398 else
|
adamc@288
|
399 box [],
|
adamc@288
|
400 string "of",
|
adamc@288
|
401 space,
|
adamc@288
|
402 p_list_sep (box [space, string "|", space])
|
adamc@288
|
403 (fn (p, e) => box [p_pat env p,
|
adamc@288
|
404 space,
|
adamc@288
|
405 string "=>",
|
adamc@288
|
406 space,
|
adamc@288
|
407 p_exp (E.patBinds env p) e]) pes])
|
adamc@177
|
408
|
adamc@102
|
409 | EWrite e => box [string "write(",
|
adamc@102
|
410 p_exp env e,
|
adamc@102
|
411 string ")"]
|
adamc@102
|
412
|
adamc@110
|
413 | EClosure (n, es) => box [string "CLOSURE(",
|
adamc@110
|
414 p_enamed env n,
|
adamc@110
|
415 p_list_sep (string "") (fn e => box [string ", ",
|
adamc@110
|
416 p_exp env e]) es,
|
adamc@110
|
417 string ")"]
|
adamc@110
|
418
|
adamc@450
|
419 | ELet (x, t, e1, e2) => box [string "let",
|
adamc@450
|
420 space,
|
adamc@450
|
421 string x,
|
adamc@450
|
422 space,
|
adamc@450
|
423 string ":",
|
adamc@450
|
424 p_con env t,
|
adamc@450
|
425 space,
|
adamc@450
|
426 string "=",
|
adamc@450
|
427 space,
|
adamc@450
|
428 p_exp env e1,
|
adamc@450
|
429 space,
|
adamc@450
|
430 string "in",
|
adamc@450
|
431 newline,
|
adamc@450
|
432 p_exp (E.pushERel env x t) e2]
|
adamc@450
|
433
|
adamc@609
|
434 | EServerCall (n, es, e, _) => box [string "Server(",
|
adamc@609
|
435 p_enamed env n,
|
adamc@609
|
436 string ",",
|
adamc@609
|
437 space,
|
adamc@609
|
438 p_list (p_exp env) es,
|
adamc@609
|
439 string ")[",
|
adamc@609
|
440 p_exp env e,
|
adamc@609
|
441 string "]"]
|
adamc@607
|
442
|
adamc@626
|
443 | EKAbs (x, e) => box [string x,
|
adamc@626
|
444 space,
|
adamc@626
|
445 string "==>",
|
adamc@626
|
446 space,
|
adamc@626
|
447 p_exp (E.pushKRel env x) e]
|
adamc@626
|
448 | EKApp (e, k) => box [p_exp env e,
|
adamc@626
|
449 string "[[",
|
adamc@626
|
450 p_kind env k,
|
adamc@626
|
451 string "]]"]
|
adamc@626
|
452
|
adamc@16
|
453 and p_exp env = p_exp' false env
|
adamc@16
|
454
|
adamc@247
|
455 fun p_named x n =
|
adamc@247
|
456 if !debug then
|
adamc@247
|
457 box [string x,
|
adamc@247
|
458 string "__",
|
adamc@247
|
459 string (Int.toString n)]
|
adamc@247
|
460 else
|
adamc@247
|
461 string x
|
adamc@247
|
462
|
adamc@125
|
463 fun p_vali env (x, n, t, e, s) =
|
adamc@125
|
464 let
|
adamc@247
|
465 val xp = p_named x n
|
adamc@125
|
466 in
|
adamc@125
|
467 box [xp,
|
adamc@125
|
468 space,
|
adamc@125
|
469 string "as",
|
adamc@125
|
470 space,
|
adamc@125
|
471 string s,
|
adamc@125
|
472 space,
|
adamc@125
|
473 string ":",
|
adamc@125
|
474 space,
|
adamc@125
|
475 p_con env t,
|
adamc@125
|
476 space,
|
adamc@125
|
477 string "=",
|
adamc@125
|
478 space,
|
adamc@125
|
479 p_exp env e]
|
adamc@125
|
480 end
|
adamc@125
|
481
|
adamc@192
|
482 fun p_datatype env (x, n, xs, cons) =
|
adamc@163
|
483 let
|
adamc@192
|
484 val k = (KType, ErrorMsg.dummySpan)
|
adamc@163
|
485 val env = E.pushCNamed env x n (KType, ErrorMsg.dummySpan) NONE
|
adamc@192
|
486 val env = foldl (fn (x, env) => E.pushCRel env x k) env xs
|
adamc@193
|
487
|
adamc@193
|
488 val xp = if !debug then
|
adamc@193
|
489 string (x ^ "__" ^ Int.toString n)
|
adamc@193
|
490 else
|
adamc@193
|
491 string x
|
adamc@163
|
492 in
|
adamc@163
|
493 box [string "datatype",
|
adamc@163
|
494 space,
|
adamc@193
|
495 xp,
|
adamc@192
|
496 p_list_sep (box []) (fn x => box [space, string x]) xs,
|
adamc@163
|
497 space,
|
adamc@163
|
498 string "=",
|
adamc@163
|
499 space,
|
adamc@163
|
500 p_list_sep (box [space, string "|", space])
|
adamc@163
|
501 (fn (x, n, NONE) => if !debug then (string (x ^ "__" ^ Int.toString n))
|
adamc@163
|
502 else string x
|
adamc@193
|
503 | (x, n, SOME t) => box [if !debug then (string (x ^ "__" ^ Int.toString n))
|
adamc@163
|
504 else string x, space, string "of", space, p_con env t])
|
adamc@163
|
505 cons]
|
adamc@163
|
506 end
|
adamc@163
|
507
|
adamc@125
|
508 fun p_decl env (dAll as (d, _) : decl) =
|
adamc@16
|
509 case d of
|
adamc@16
|
510 DCon (x, n, k, c) =>
|
adamc@16
|
511 let
|
adamc@16
|
512 val xp = if !debug then
|
adamc@16
|
513 box [string x,
|
adamc@16
|
514 string "__",
|
adamc@16
|
515 string (Int.toString n)]
|
adamc@16
|
516 else
|
adamc@16
|
517 string x
|
adamc@16
|
518 in
|
adamc@16
|
519 box [string "con",
|
adamc@16
|
520 space,
|
adamc@16
|
521 xp,
|
adamc@16
|
522 space,
|
adamc@16
|
523 string "::",
|
adamc@16
|
524 space,
|
adamc@626
|
525 p_kind env k,
|
adamc@16
|
526 space,
|
adamc@16
|
527 string "=",
|
adamc@16
|
528 space,
|
adamc@16
|
529 p_con env c]
|
adamc@16
|
530 end
|
adamc@163
|
531 | DDatatype x => p_datatype env x
|
adamc@125
|
532 | DVal vi => box [string "val",
|
adamc@125
|
533 space,
|
adamc@125
|
534 p_vali env vi]
|
adamc@125
|
535 | DValRec vis =>
|
adamc@16
|
536 let
|
adamc@125
|
537 val env = E.declBinds env dAll
|
adamc@16
|
538 in
|
adamc@16
|
539 box [string "val",
|
adamc@16
|
540 space,
|
adamc@125
|
541 string "rec",
|
adamc@16
|
542 space,
|
adamc@125
|
543 p_list_sep (box [newline, string "and", space]) (p_vali env) vis]
|
adamc@16
|
544 end
|
adamc@144
|
545 | DExport (ek, n) => box [string "export",
|
adamc@144
|
546 space,
|
adamc@732
|
547 Export.p_export_kind ek,
|
adamc@144
|
548 space,
|
adamc@144
|
549 p_enamed env n,
|
adamc@144
|
550 space,
|
adamc@144
|
551 string "as",
|
adamc@144
|
552 space,
|
adamc@482
|
553 (p_con env (#2 (E.lookupENamed env n))
|
adamc@482
|
554 handle E.UnboundNamed _ => string "UNBOUND")]
|
adamc@707
|
555 | DTable (x, n, c, s, pe, _, ce, _) => box [string "table",
|
adamc@707
|
556 space,
|
adamc@707
|
557 p_named x n,
|
adamc@707
|
558 space,
|
adamc@707
|
559 string "as",
|
adamc@707
|
560 space,
|
adamc@707
|
561 string s,
|
adamc@707
|
562 space,
|
adamc@707
|
563 string ":",
|
adamc@707
|
564 space,
|
adamc@707
|
565 p_con env c,
|
adamc@707
|
566 space,
|
adamc@707
|
567 string "keys",
|
adamc@707
|
568 space,
|
adamc@707
|
569 p_exp env pe,
|
adamc@707
|
570 space,
|
adamc@707
|
571 string "constraints",
|
adamc@707
|
572 space,
|
adamc@707
|
573 p_exp env ce]
|
adamc@338
|
574 | DSequence (x, n, s) => box [string "sequence",
|
adamc@338
|
575 space,
|
adamc@338
|
576 p_named x n,
|
adamc@338
|
577 space,
|
adamc@338
|
578 string "as",
|
adamc@338
|
579 space,
|
adamc@338
|
580 string s]
|
adamc@754
|
581 | DView (x, n, s, e, _) => box [string "view",
|
adamc@754
|
582 space,
|
adamc@754
|
583 p_named x n,
|
adamc@754
|
584 space,
|
adamc@754
|
585 string "as",
|
adamc@754
|
586 space,
|
adamc@754
|
587 p_exp env e]
|
adamc@271
|
588 | DDatabase s => box [string "database",
|
adamc@271
|
589 space,
|
adamc@271
|
590 string s]
|
adamc@461
|
591 | DCookie (x, n, c, s) => box [string "cookie",
|
adamc@461
|
592 space,
|
adamc@461
|
593 p_named x n,
|
adamc@461
|
594 space,
|
adamc@461
|
595 string "as",
|
adamc@461
|
596 space,
|
adamc@461
|
597 string s,
|
adamc@461
|
598 space,
|
adamc@461
|
599 string ":",
|
adamc@461
|
600 space,
|
adamc@461
|
601 p_con env c]
|
adamc@720
|
602 | DStyle (x, n, s) => box [string "style",
|
adamc@720
|
603 space,
|
adamc@720
|
604 p_named x n,
|
adamc@720
|
605 space,
|
adamc@720
|
606 string "as",
|
adamc@720
|
607 space,
|
adamc@720
|
608 string s]
|
adamc@16
|
609
|
adamc@16
|
610 fun p_file env file =
|
adamc@16
|
611 let
|
adamc@31
|
612 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
|
adamc@31
|
613 (p_decl env d,
|
adamc@31
|
614 E.declBinds env d))
|
adamc@16
|
615 env file
|
adamc@16
|
616 in
|
adamc@16
|
617 p_list_sep newline (fn x => x) pds
|
adamc@16
|
618 end
|
adamc@16
|
619
|
adamc@16
|
620 end
|