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@38
|
86 if !debug then
|
adamc@38
|
87 string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n)
|
adamc@38
|
88 else
|
adamc@38
|
89 string (#1 (E.lookupCRel env n))
|
adamc@38
|
90 | CNamed n =>
|
adamc@38
|
91 ((if !debug then
|
adamc@38
|
92 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
|
adamc@38
|
93 else
|
adamc@38
|
94 string (#1 (E.lookupCNamed env n)))
|
adamc@38
|
95 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
|
adamc@38
|
96 | CModProj (m1, ms, x) =>
|
adamc@38
|
97 let
|
adamc@109
|
98 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@109
|
99 handle E.UnboundNamed _ => "UNBOUND"
|
adamc@38
|
100
|
adamc@38
|
101 val m1s = if !debug then
|
adamc@38
|
102 m1x ^ "__" ^ Int.toString m1
|
adamc@38
|
103 else
|
adamc@38
|
104 m1x
|
adamc@38
|
105 in
|
adamc@146
|
106 p_list_sep (string ".") string (m1s :: ms @ [x])
|
adamc@38
|
107 end
|
adamc@38
|
108
|
adamc@38
|
109 | CApp (c1, c2) => parenIf par (box [p_con env c1,
|
adamc@38
|
110 space,
|
adamc@38
|
111 p_con' true env c2])
|
adamc@38
|
112 | CAbs (x, k, c) => parenIf par (box [string "fn",
|
adamc@38
|
113 space,
|
adamc@38
|
114 string x,
|
adamc@38
|
115 space,
|
adamc@38
|
116 string "::",
|
adamc@38
|
117 space,
|
adamc@38
|
118 p_kind k,
|
adamc@38
|
119 space,
|
adamc@38
|
120 string "=>",
|
adamc@38
|
121 space,
|
adamc@38
|
122 p_con (E.pushCRel env x k) c])
|
adamc@38
|
123
|
adamc@38
|
124 | CName s => box [string "#", string s]
|
adamc@38
|
125
|
adamc@38
|
126 | CRecord (k, xcs) =>
|
adamc@38
|
127 if !debug then
|
adamc@38
|
128 parenIf par (box [string "[",
|
adamc@38
|
129 p_list (fn (x, c) =>
|
adamc@38
|
130 box [p_con env x,
|
adamc@38
|
131 space,
|
adamc@38
|
132 string "=",
|
adamc@38
|
133 space,
|
adamc@38
|
134 p_con env c]) xcs,
|
adamc@38
|
135 string "]::",
|
adamc@38
|
136 p_kind k])
|
adamc@38
|
137 else
|
adamc@38
|
138 parenIf par (box [string "[",
|
adamc@38
|
139 p_list (fn (x, c) =>
|
adamc@38
|
140 box [p_con env x,
|
adamc@38
|
141 space,
|
adamc@38
|
142 string "=",
|
adamc@38
|
143 space,
|
adamc@38
|
144 p_con env c]) xcs,
|
adamc@38
|
145 string "]"])
|
adamc@38
|
146 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
|
adamc@38
|
147 space,
|
adamc@38
|
148 string "++",
|
adamc@38
|
149 space,
|
adamc@38
|
150 p_con env c2])
|
adamc@68
|
151 | CFold _ => string "fold"
|
adamc@87
|
152 | CUnit => string "()"
|
adamc@213
|
153
|
adamc@213
|
154 | CTuple cs => box [string "(",
|
adamc@213
|
155 p_list (p_con env) cs,
|
adamc@213
|
156 string ")"]
|
adamc@213
|
157 | CProj (c, n) => box [p_con env c,
|
adamc@213
|
158 string ".",
|
adamc@213
|
159 string (Int.toString n)]
|
adamc@38
|
160
|
adamc@38
|
161 and p_con env = p_con' false env
|
adamc@38
|
162
|
adamc@38
|
163 and p_name env (all as (c, _)) =
|
adamc@38
|
164 case c of
|
adamc@38
|
165 CName s => string s
|
adamc@38
|
166 | _ => p_con env all
|
adamc@38
|
167
|
adamc@176
|
168 fun p_patCon env pc =
|
adamc@176
|
169 case pc of
|
adamc@176
|
170 PConVar n =>
|
adamc@176
|
171 ((if !debug then
|
adamc@176
|
172 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
|
adamc@176
|
173 else
|
adamc@176
|
174 string (#1 (E.lookupENamed env n)))
|
adamc@176
|
175 handle E.UnboundRel _ => string ("UNBOUND_NAMED" ^ Int.toString n))
|
adamc@176
|
176 | PConProj (m1, ms, x) =>
|
adamc@176
|
177 let
|
adamc@176
|
178 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@176
|
179 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
|
adamc@176
|
180
|
adamc@176
|
181 val m1s = if !debug then
|
adamc@176
|
182 m1x ^ "__" ^ Int.toString m1
|
adamc@176
|
183 else
|
adamc@176
|
184 m1x
|
adamc@176
|
185 in
|
adamc@176
|
186 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@176
|
187 end
|
adamc@176
|
188
|
adamc@176
|
189 fun p_pat' par env (p, _) =
|
adamc@176
|
190 case p of
|
adamc@176
|
191 PWild => string "_"
|
adamc@182
|
192 | PVar (s, _) => string s
|
adamc@176
|
193 | PPrim p => Prim.p_t p
|
adamc@191
|
194 | PCon (_, pc, _, NONE) => p_patCon env pc
|
adamc@191
|
195 | PCon (_, pc, _, SOME p) => parenIf par (box [p_patCon env pc,
|
adamc@191
|
196 space,
|
adamc@191
|
197 p_pat' true env p])
|
adamc@176
|
198 | PRecord xps =>
|
adamc@176
|
199 box [string "{",
|
adamc@182
|
200 p_list_sep (box [string ",", space]) (fn (x, p, _) =>
|
adamc@176
|
201 box [string x,
|
adamc@176
|
202 space,
|
adamc@176
|
203 string "=",
|
adamc@176
|
204 space,
|
adamc@176
|
205 p_pat env p]) xps,
|
adamc@176
|
206 string "}"]
|
adamc@176
|
207
|
adamc@176
|
208 and p_pat x = p_pat' false x
|
adamc@176
|
209
|
adamc@146
|
210 fun p_exp' par env (e, loc) =
|
adamc@38
|
211 case e of
|
adamc@38
|
212 EPrim p => Prim.p_t p
|
adamc@38
|
213 | ERel n =>
|
adamc@38
|
214 if !debug then
|
adamc@38
|
215 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
|
adamc@38
|
216 else
|
adamc@38
|
217 string (#1 (E.lookupERel env n))
|
adamc@38
|
218 | ENamed n =>
|
adamc@38
|
219 if !debug then
|
adamc@38
|
220 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
|
adamc@38
|
221 else
|
adamc@38
|
222 string (#1 (E.lookupENamed env n))
|
adamc@38
|
223 | EModProj (m1, ms, x) =>
|
adamc@38
|
224 let
|
adamc@38
|
225 val (m1x, sgn) = E.lookupStrNamed env m1
|
adamc@146
|
226 handle E.UnboundNamed _ => ("UNBOUND", (SgnConst [], loc))
|
adamc@38
|
227
|
adamc@38
|
228 val m1s = if !debug then
|
adamc@38
|
229 m1x ^ "__" ^ Int.toString m1
|
adamc@38
|
230 else
|
adamc@38
|
231 m1x
|
adamc@38
|
232 in
|
adamc@146
|
233 p_list_sep (string ".") string (m1s :: ms @ [x])
|
adamc@38
|
234 end
|
adamc@38
|
235
|
adamc@38
|
236 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
|
adamc@38
|
237 space,
|
adamc@38
|
238 p_exp' true env e2])
|
adamc@38
|
239 | EAbs (x, t, _, e) => parenIf par (box [string "fn",
|
adamc@38
|
240 space,
|
adamc@38
|
241 string x,
|
adamc@38
|
242 space,
|
adamc@38
|
243 string ":",
|
adamc@38
|
244 space,
|
adamc@38
|
245 p_con env t,
|
adamc@38
|
246 space,
|
adamc@38
|
247 string "=>",
|
adamc@38
|
248 space,
|
adamc@38
|
249 p_exp (E.pushERel env x t) e])
|
adamc@38
|
250 | ECApp (e, c) => parenIf par (box [p_exp env e,
|
adamc@38
|
251 space,
|
adamc@38
|
252 string "[",
|
adamc@38
|
253 p_con env c,
|
adamc@38
|
254 string "]"])
|
adamc@38
|
255 | ECAbs (x, k, e) => parenIf par (box [string "fn",
|
adamc@38
|
256 space,
|
adamc@38
|
257 string x,
|
adamc@38
|
258 space,
|
adamc@38
|
259 string "::",
|
adamc@38
|
260 space,
|
adamc@38
|
261 p_kind k,
|
adamc@38
|
262 space,
|
adamc@38
|
263 string "=>",
|
adamc@38
|
264 space,
|
adamc@38
|
265 p_exp (E.pushCRel env x k) e])
|
adamc@38
|
266
|
adamc@38
|
267 | ERecord xes => box [string "{",
|
adamc@38
|
268 p_list (fn (x, e, _) =>
|
adamc@38
|
269 box [p_name env x,
|
adamc@38
|
270 space,
|
adamc@38
|
271 string "=",
|
adamc@38
|
272 space,
|
adamc@38
|
273 p_exp env e]) xes,
|
adamc@38
|
274 string "}"]
|
adamc@38
|
275 | EField (e, c, {field, rest}) =>
|
adamc@38
|
276 if !debug then
|
adamc@38
|
277 box [p_exp' true env e,
|
adamc@38
|
278 string ".",
|
adamc@38
|
279 p_con' true env c,
|
adamc@38
|
280 space,
|
adamc@38
|
281 string "[",
|
adamc@38
|
282 p_con env field,
|
adamc@38
|
283 space,
|
adamc@38
|
284 string " in ",
|
adamc@38
|
285 space,
|
adamc@38
|
286 p_con env rest,
|
adamc@38
|
287 string "]"]
|
adamc@38
|
288 else
|
adamc@38
|
289 box [p_exp' true env e,
|
adamc@38
|
290 string ".",
|
adamc@38
|
291 p_con' true env c]
|
adamc@149
|
292 | ECut (e, c, {field, rest}) =>
|
adamc@149
|
293 parenIf par (if !debug then
|
adamc@149
|
294 box [p_exp' true env e,
|
adamc@149
|
295 space,
|
adamc@149
|
296 string "--",
|
adamc@149
|
297 space,
|
adamc@149
|
298 p_con' true env c,
|
adamc@149
|
299 space,
|
adamc@149
|
300 string "[",
|
adamc@149
|
301 p_con env field,
|
adamc@149
|
302 space,
|
adamc@149
|
303 string " in ",
|
adamc@149
|
304 space,
|
adamc@149
|
305 p_con env rest,
|
adamc@149
|
306 string "]"]
|
adamc@149
|
307 else
|
adamc@149
|
308 box [p_exp' true env e,
|
adamc@149
|
309 space,
|
adamc@149
|
310 string "--",
|
adamc@149
|
311 space,
|
adamc@149
|
312 p_con' true env c])
|
adamc@72
|
313 | EFold _ => string "fold"
|
adamc@38
|
314
|
adamc@109
|
315 | EWrite e => box [string "write(",
|
adamc@109
|
316 p_exp env e,
|
adamc@109
|
317 string ")"]
|
adamc@109
|
318
|
adamc@176
|
319 | ECase (e, pes, _) => parenIf par (box [string "case",
|
adamc@176
|
320 space,
|
adamc@176
|
321 p_exp env e,
|
adamc@176
|
322 space,
|
adamc@176
|
323 string "of",
|
adamc@176
|
324 space,
|
adamc@176
|
325 p_list_sep (box [space, string "|", space])
|
adamc@176
|
326 (fn (p, e) => box [p_pat env p,
|
adamc@176
|
327 space,
|
adamc@176
|
328 string "=>",
|
adamc@176
|
329 space,
|
adamc@176
|
330 p_exp env e]) pes])
|
adamc@176
|
331
|
adamc@38
|
332 and p_exp env = p_exp' false env
|
adamc@38
|
333
|
adamc@38
|
334 fun p_named x n =
|
adamc@38
|
335 if !debug then
|
adamc@38
|
336 box [string x,
|
adamc@38
|
337 string "__",
|
adamc@38
|
338 string (Int.toString n)]
|
adamc@38
|
339 else
|
adamc@38
|
340 string x
|
adamc@38
|
341
|
adamc@191
|
342 fun p_datatype env (x, n, xs, cons) =
|
adamc@162
|
343 let
|
adamc@191
|
344 val k = (KType, ErrorMsg.dummySpan)
|
adamc@191
|
345 val env = E.pushCNamed env x n k NONE
|
adamc@191
|
346 val env = foldl (fn (x, env) => E.pushCRel env x k) env xs
|
adamc@162
|
347 in
|
adamc@162
|
348 box [string "datatype",
|
adamc@162
|
349 space,
|
adamc@162
|
350 string x,
|
adamc@191
|
351 p_list_sep (box []) (fn x => box [space, string x]) xs,
|
adamc@162
|
352 space,
|
adamc@162
|
353 string "=",
|
adamc@162
|
354 space,
|
adamc@162
|
355 p_list_sep (box [space, string "|", space])
|
adamc@163
|
356 (fn (x, n, NONE) => if !debug then (string (x ^ "__" ^ Int.toString n))
|
adamc@163
|
357 else string x
|
adamc@193
|
358 | (x, n, SOME t) => box [if !debug then (string (x ^ "__" ^ Int.toString n))
|
adamc@163
|
359 else string x, space, string "of", space, p_con env t])
|
adamc@162
|
360 cons]
|
adamc@162
|
361 end
|
adamc@162
|
362
|
adamc@38
|
363 fun p_sgn_item env (sgi, _) =
|
adamc@38
|
364 case sgi of
|
adamc@38
|
365 SgiConAbs (x, n, k) => box [string "con",
|
adamc@38
|
366 space,
|
adamc@38
|
367 p_named x n,
|
adamc@38
|
368 space,
|
adamc@38
|
369 string "::",
|
adamc@38
|
370 space,
|
adamc@38
|
371 p_kind k]
|
adamc@38
|
372 | SgiCon (x, n, k, c) => box [string "con",
|
adamc@38
|
373 space,
|
adamc@38
|
374 p_named x n,
|
adamc@38
|
375 space,
|
adamc@38
|
376 string "::",
|
adamc@38
|
377 space,
|
adamc@38
|
378 p_kind k,
|
adamc@38
|
379 space,
|
adamc@38
|
380 string "=",
|
adamc@38
|
381 space,
|
adamc@38
|
382 p_con env c]
|
adamc@162
|
383 | SgiDatatype x => p_datatype env x
|
adamc@191
|
384 | SgiDatatypeImp (x, _, m1, ms, x', _, _) =>
|
adamc@162
|
385 let
|
adamc@162
|
386 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@162
|
387 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
|
adamc@162
|
388 in
|
adamc@162
|
389 box [string "datatype",
|
adamc@162
|
390 space,
|
adamc@162
|
391 string x,
|
adamc@162
|
392 space,
|
adamc@162
|
393 string "=",
|
adamc@162
|
394 space,
|
adamc@162
|
395 string "datatype",
|
adamc@162
|
396 space,
|
adamc@162
|
397 p_list_sep (string ".") string (m1x :: ms @ [x'])]
|
adamc@162
|
398 end
|
adamc@38
|
399 | SgiVal (x, n, c) => box [string "val",
|
adamc@38
|
400 space,
|
adamc@38
|
401 p_named x n,
|
adamc@38
|
402 space,
|
adamc@38
|
403 string ":",
|
adamc@38
|
404 space,
|
adamc@38
|
405 p_con env c]
|
adamc@38
|
406 | SgiStr (x, n, sgn) => box [string "structure",
|
adamc@38
|
407 space,
|
adamc@38
|
408 p_named x n,
|
adamc@38
|
409 space,
|
adamc@38
|
410 string ":",
|
adamc@38
|
411 space,
|
adamc@38
|
412 p_sgn env sgn]
|
adamc@64
|
413 | SgiSgn (x, n, sgn) => box [string "signature",
|
adamc@64
|
414 space,
|
adamc@64
|
415 p_named x n,
|
adamc@64
|
416 space,
|
adamc@64
|
417 string "=",
|
adamc@64
|
418 space,
|
adamc@64
|
419 p_sgn env sgn]
|
adamc@246
|
420 | SgiTable (_, x, n, c) => box [string "table",
|
adamc@246
|
421 space,
|
adamc@246
|
422 p_named x n,
|
adamc@246
|
423 space,
|
adamc@246
|
424 string ":",
|
adamc@246
|
425 space,
|
adamc@246
|
426 p_con env c]
|
adamc@38
|
427
|
adamc@146
|
428 and p_sgn env (sgn, loc) =
|
adamc@38
|
429 case sgn of
|
adamc@38
|
430 SgnConst sgis => box [string "sig",
|
adamc@38
|
431 newline,
|
adamc@38
|
432 let
|
adamc@38
|
433 val (psgis, _) = ListUtil.foldlMap (fn (sgi, env) =>
|
adamc@38
|
434 (p_sgn_item env sgi,
|
adamc@38
|
435 E.sgiBinds env sgi))
|
adamc@38
|
436 env sgis
|
adamc@38
|
437 in
|
adamc@38
|
438 p_list_sep newline (fn x => x) psgis
|
adamc@38
|
439 end,
|
adamc@38
|
440 newline,
|
adamc@38
|
441 string "end"]
|
adamc@146
|
442 | SgnVar n => string ((#1 (E.lookupSgnNamed env n))
|
adamc@146
|
443 handle E.UnboundNamed _ => "UNBOUND")
|
adamc@45
|
444 | SgnFun (x, n, sgn, sgn') => box [string "functor",
|
adamc@45
|
445 space,
|
adamc@45
|
446 string "(",
|
adamc@45
|
447 string x,
|
adamc@45
|
448 space,
|
adamc@45
|
449 string ":",
|
adamc@45
|
450 space,
|
adamc@45
|
451 p_sgn env sgn,
|
adamc@45
|
452 string ")",
|
adamc@45
|
453 space,
|
adamc@45
|
454 string ":",
|
adamc@45
|
455 space,
|
adamc@45
|
456 p_sgn (E.pushStrNamed env x n sgn) sgn']
|
adamc@45
|
457 | SgnWhere (sgn, x, c) => box [p_sgn env sgn,
|
adamc@45
|
458 space,
|
adamc@45
|
459 string "where",
|
adamc@45
|
460 space,
|
adamc@45
|
461 string "con",
|
adamc@45
|
462 space,
|
adamc@45
|
463 string x,
|
adamc@45
|
464 space,
|
adamc@45
|
465 string "=",
|
adamc@45
|
466 space,
|
adamc@45
|
467 p_con env c]
|
adamc@64
|
468 | SgnProj (m1, ms, x) =>
|
adamc@64
|
469 let
|
adamc@64
|
470 val (m1x, sgn) = E.lookupStrNamed env m1
|
adamc@146
|
471 handle E.UnboundNamed _ => ("UNBOUND", (SgnConst [], loc))
|
adamc@64
|
472
|
adamc@64
|
473 val m1s = if !debug then
|
adamc@64
|
474 m1x ^ "__" ^ Int.toString m1
|
adamc@64
|
475 else
|
adamc@64
|
476 m1x
|
adamc@64
|
477 in
|
adamc@64
|
478 p_list_sep (string ".") string (m1x :: ms @ [x])
|
adamc@64
|
479 end
|
adamc@38
|
480
|
adamc@124
|
481 fun p_vali env (x, n, t, e) = box [p_named x n,
|
adamc@124
|
482 space,
|
adamc@124
|
483 string ":",
|
adamc@124
|
484 space,
|
adamc@124
|
485 p_con env t,
|
adamc@124
|
486 space,
|
adamc@124
|
487 string "=",
|
adamc@124
|
488 space,
|
adamc@124
|
489 p_exp env e]
|
adamc@124
|
490
|
adamc@124
|
491 fun p_decl env (dAll as (d, _) : decl) =
|
adamc@38
|
492 case d of
|
adamc@38
|
493 DCon (x, n, k, c) => box [string "con",
|
adamc@38
|
494 space,
|
adamc@38
|
495 p_named x n,
|
adamc@38
|
496 space,
|
adamc@38
|
497 string "::",
|
adamc@38
|
498 space,
|
adamc@38
|
499 p_kind k,
|
adamc@38
|
500 space,
|
adamc@38
|
501 string "=",
|
adamc@38
|
502 space,
|
adamc@38
|
503 p_con env c]
|
adamc@162
|
504 | DDatatype x => p_datatype env x
|
adamc@191
|
505 | DDatatypeImp (x, _, m1, ms, x', _, _) =>
|
adamc@162
|
506 let
|
adamc@162
|
507 val m1x = #1 (E.lookupStrNamed env m1)
|
adamc@162
|
508 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
|
adamc@162
|
509 in
|
adamc@162
|
510 box [string "datatype",
|
adamc@162
|
511 space,
|
adamc@162
|
512 string x,
|
adamc@162
|
513 space,
|
adamc@162
|
514 string "=",
|
adamc@162
|
515 space,
|
adamc@162
|
516 string "datatype",
|
adamc@162
|
517 space,
|
adamc@162
|
518 p_list_sep (string ".") string (m1x :: ms @ [x'])]
|
adamc@162
|
519 end
|
adamc@124
|
520 | DVal vi => box [string "val",
|
adamc@124
|
521 space,
|
adamc@124
|
522 p_vali env vi]
|
adamc@124
|
523 | DValRec vis =>
|
adamc@124
|
524 let
|
adamc@124
|
525 val env = E.declBinds env dAll
|
adamc@124
|
526 in
|
adamc@124
|
527 box [string "val",
|
adamc@124
|
528 space,
|
adamc@124
|
529 string "rec",
|
adamc@124
|
530 space,
|
adamc@124
|
531 p_list_sep (box [newline, string "and", space]) (p_vali env) vis]
|
adamc@124
|
532 end
|
adamc@38
|
533
|
adamc@38
|
534 | DSgn (x, n, sgn) => box [string "signature",
|
adamc@38
|
535 space,
|
adamc@38
|
536 p_named x n,
|
adamc@38
|
537 space,
|
adamc@38
|
538 string "=",
|
adamc@38
|
539 space,
|
adamc@38
|
540 p_sgn env sgn]
|
adamc@38
|
541 | DStr (x, n, sgn, str) => box [string "structure",
|
adamc@38
|
542 space,
|
adamc@38
|
543 p_named x n,
|
adamc@38
|
544 space,
|
adamc@38
|
545 string ":",
|
adamc@38
|
546 space,
|
adamc@38
|
547 p_sgn env sgn,
|
adamc@38
|
548 space,
|
adamc@38
|
549 string "=",
|
adamc@38
|
550 space,
|
adamc@38
|
551 p_str env str]
|
adamc@48
|
552 | DFfiStr (x, n, sgn) => box [string "extern",
|
adamc@48
|
553 space,
|
adamc@48
|
554 string "structure",
|
adamc@48
|
555 space,
|
adamc@48
|
556 p_named x n,
|
adamc@48
|
557 space,
|
adamc@48
|
558 string ":",
|
adamc@48
|
559 space,
|
adamc@48
|
560 p_sgn env sgn]
|
adamc@109
|
561 | DExport (_, sgn, str) => box [string "export",
|
adamc@109
|
562 space,
|
adamc@109
|
563 p_str env str,
|
adamc@109
|
564 space,
|
adamc@109
|
565 string ":",
|
adamc@109
|
566 space,
|
adamc@109
|
567 p_sgn env sgn]
|
adamc@246
|
568 | DTable (_, x, n, c) => box [string "table",
|
adamc@246
|
569 space,
|
adamc@246
|
570 p_named x n,
|
adamc@246
|
571 space,
|
adamc@246
|
572 string ":",
|
adamc@246
|
573 space,
|
adamc@246
|
574 p_con env c]
|
adamc@38
|
575
|
adamc@38
|
576 and p_str env (str, _) =
|
adamc@38
|
577 case str of
|
adamc@38
|
578 StrConst ds => box [string "struct",
|
adamc@38
|
579 newline,
|
adamc@38
|
580 p_file env ds,
|
adamc@38
|
581 newline,
|
adamc@38
|
582 string "end"]
|
adamc@146
|
583 | StrVar n =>
|
adamc@146
|
584 let
|
adamc@146
|
585 val x = #1 (E.lookupStrNamed env n)
|
adamc@146
|
586 handle E.UnboundNamed _ => "UNBOUND"
|
adamc@146
|
587
|
adamc@146
|
588 val s = if !debug then
|
adamc@146
|
589 x ^ "__" ^ Int.toString n
|
adamc@146
|
590 else
|
adamc@146
|
591 x
|
adamc@146
|
592 in
|
adamc@146
|
593 string s
|
adamc@146
|
594 end
|
adamc@38
|
595 | StrProj (str, s) => box [p_str env str,
|
adamc@38
|
596 string ".",
|
adamc@38
|
597 string s]
|
adamc@45
|
598 | StrFun (x, n, sgn, sgn', str) =>
|
adamc@45
|
599 let
|
adamc@45
|
600 val env' = E.pushStrNamed env x n sgn
|
adamc@45
|
601 in
|
adamc@45
|
602 box [string "functor",
|
adamc@45
|
603 space,
|
adamc@45
|
604 string "(",
|
adamc@45
|
605 string x,
|
adamc@45
|
606 space,
|
adamc@45
|
607 string ":",
|
adamc@45
|
608 space,
|
adamc@45
|
609 p_sgn env sgn,
|
adamc@45
|
610 string ")",
|
adamc@45
|
611 space,
|
adamc@45
|
612 string ":",
|
adamc@45
|
613 space,
|
adamc@45
|
614 p_sgn env' sgn',
|
adamc@45
|
615 space,
|
adamc@45
|
616 string "=>",
|
adamc@45
|
617 space,
|
adamc@45
|
618 p_str env' str]
|
adamc@45
|
619 end
|
adamc@45
|
620 | StrApp (str1, str2) => box [p_str env str1,
|
adamc@45
|
621 string "(",
|
adamc@45
|
622 p_str env str2,
|
adamc@45
|
623 string ")"]
|
adamc@38
|
624
|
adamc@38
|
625 and p_file env file =
|
adamc@38
|
626 let
|
adamc@38
|
627 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
|
adamc@38
|
628 (p_decl env d,
|
adamc@38
|
629 E.declBinds env d))
|
adamc@38
|
630 env file
|
adamc@38
|
631 in
|
adamc@38
|
632 p_list_sep newline (fn x => x) pds
|
adamc@38
|
633 end
|
adamc@38
|
634
|
adamc@38
|
635 end
|