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