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