Mercurial > urweb
comparison src/core_print.sml @ 16:bc7b76ca57e0
Conversion to Core
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Sun, 08 Jun 2008 13:59:29 -0400 |
parents | |
children | 1ab48e37d0ef |
comparison
equal
deleted
inserted
replaced
15:1e645beb3f3b | 16:bc7b76ca57e0 |
---|---|
1 (* Copyright (c) 2008, Adam Chlipala | |
2 * All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are met: | |
6 * | |
7 * - Redistributions of source code must retain the above copyright notice, | |
8 * this list of conditions and the following disclaimer. | |
9 * - Redistributions in binary form must reproduce the above copyright notice, | |
10 * this list of conditions and the following disclaimer in the documentation | |
11 * and/or other materials provided with the distribution. | |
12 * - The names of contributors may not be used to endorse or promote products | |
13 * derived from this software without specific prior written permission. | |
14 * | |
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
25 * POSSIBILITY OF SUCH DAMAGE. | |
26 *) | |
27 | |
28 (* Pretty-printing core Laconic/Web *) | |
29 | |
30 structure CorePrint :> CORE_PRINT = struct | |
31 | |
32 open Print.PD | |
33 open Print | |
34 | |
35 open Core | |
36 | |
37 structure E = CoreEnv | |
38 | |
39 val debug = ref false | |
40 | |
41 fun p_kind' par (k, _) = | |
42 case k of | |
43 KType => string "Type" | |
44 | KArrow (k1, k2) => parenIf par (box [p_kind' true k1, | |
45 space, | |
46 string "->", | |
47 space, | |
48 p_kind k2]) | |
49 | KName => string "Name" | |
50 | KRecord k => box [string "{", p_kind k, string "}"] | |
51 | |
52 and p_kind k = p_kind' false k | |
53 | |
54 fun p_con' par env (c, _) = | |
55 case c of | |
56 TFun (t1, t2) => parenIf par (box [p_con' true env t1, | |
57 space, | |
58 string "->", | |
59 space, | |
60 p_con env t2]) | |
61 | TCFun (x, k, c) => parenIf par (box [string x, | |
62 space, | |
63 string "::", | |
64 space, | |
65 p_kind k, | |
66 space, | |
67 string "->", | |
68 space, | |
69 p_con (E.pushCRel env x k) c]) | |
70 | TRecord (CRecord (_, xcs), _) => box [string "{", | |
71 p_list (fn (x, c) => | |
72 box [p_con env x, | |
73 space, | |
74 string ":", | |
75 space, | |
76 p_con env c]) xcs, | |
77 string "}"] | |
78 | TRecord c => box [string "$", | |
79 p_con' true env c] | |
80 | |
81 | CRel n => | |
82 if !debug then | |
83 string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n) | |
84 else | |
85 string (#1 (E.lookupCRel env n)) | |
86 | CNamed n => | |
87 if !debug then | |
88 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n) | |
89 else | |
90 string (#1 (E.lookupCNamed env n)) | |
91 | |
92 | CApp (c1, c2) => parenIf par (box [p_con env c1, | |
93 space, | |
94 p_con' true env c2]) | |
95 | CAbs (x, k, c) => parenIf par (box [string "fn", | |
96 space, | |
97 string x, | |
98 space, | |
99 string "::", | |
100 space, | |
101 p_kind k, | |
102 space, | |
103 string "=>", | |
104 space, | |
105 p_con (E.pushCRel env x k) c]) | |
106 | |
107 | CName s => box [string "#", string s] | |
108 | |
109 | CRecord (k, xcs) => | |
110 if !debug then | |
111 parenIf par (box [string "[", | |
112 p_list (fn (x, c) => | |
113 box [p_con env x, | |
114 space, | |
115 string "=", | |
116 space, | |
117 p_con env c]) xcs, | |
118 string "]::", | |
119 p_kind k]) | |
120 else | |
121 parenIf par (box [string "[", | |
122 p_list (fn (x, c) => | |
123 box [p_con env x, | |
124 space, | |
125 string "=", | |
126 space, | |
127 p_con env c]) xcs, | |
128 string "]"]) | |
129 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1, | |
130 space, | |
131 string "++", | |
132 space, | |
133 p_con env c2]) | |
134 | |
135 and p_con env = p_con' false env | |
136 | |
137 fun p_exp' par env (e, _) = | |
138 case e of | |
139 EPrim p => Prim.p_t p | |
140 | ERel n => | |
141 if !debug then | |
142 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n) | |
143 else | |
144 string (#1 (E.lookupERel env n)) | |
145 | ENamed n => | |
146 if !debug then | |
147 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n) | |
148 else | |
149 string (#1 (E.lookupENamed env n)) | |
150 | EApp (e1, e2) => parenIf par (box [p_exp env e1, | |
151 space, | |
152 p_exp' true env e2]) | |
153 | EAbs (x, t, e) => parenIf par (box [string "fn", | |
154 space, | |
155 string x, | |
156 space, | |
157 string ":", | |
158 space, | |
159 p_con env t, | |
160 space, | |
161 string "=>", | |
162 space, | |
163 p_exp (E.pushERel env x t) e]) | |
164 | ECApp (e, c) => parenIf par (box [p_exp env e, | |
165 space, | |
166 string "[", | |
167 p_con env c, | |
168 string "]"]) | |
169 | ECAbs (x, k, e) => parenIf par (box [string "fn", | |
170 space, | |
171 string x, | |
172 space, | |
173 string "::", | |
174 space, | |
175 p_kind k, | |
176 space, | |
177 string "=>", | |
178 space, | |
179 p_exp (E.pushCRel env x k) e]) | |
180 | |
181 | ERecord xes => box [string "{", | |
182 p_list (fn (x, e) => | |
183 box [p_con env x, | |
184 space, | |
185 string "=", | |
186 space, | |
187 p_exp env e]) xes, | |
188 string "}"] | |
189 | EField (e, c, {field, rest}) => | |
190 if !debug then | |
191 box [p_exp' true env e, | |
192 string ".", | |
193 p_con' true env c, | |
194 space, | |
195 string "[", | |
196 p_con env field, | |
197 space, | |
198 string " in ", | |
199 space, | |
200 p_con env rest, | |
201 string "]"] | |
202 else | |
203 box [p_exp' true env e, | |
204 string ".", | |
205 p_con' true env c] | |
206 | |
207 and p_exp env = p_exp' false env | |
208 | |
209 fun p_decl env ((d, _) : decl) = | |
210 case d of | |
211 DCon (x, n, k, c) => | |
212 let | |
213 val xp = if !debug then | |
214 box [string x, | |
215 string "__", | |
216 string (Int.toString n)] | |
217 else | |
218 string x | |
219 in | |
220 box [string "con", | |
221 space, | |
222 xp, | |
223 space, | |
224 string "::", | |
225 space, | |
226 p_kind k, | |
227 space, | |
228 string "=", | |
229 space, | |
230 p_con env c] | |
231 end | |
232 | DVal (x, n, t, e) => | |
233 let | |
234 val xp = if !debug then | |
235 box [string x, | |
236 string "__", | |
237 string (Int.toString n)] | |
238 else | |
239 string x | |
240 in | |
241 box [string "val", | |
242 space, | |
243 xp, | |
244 space, | |
245 string ":", | |
246 space, | |
247 p_con env t, | |
248 space, | |
249 string "=", | |
250 space, | |
251 p_exp env e] | |
252 end | |
253 | |
254 fun p_file env file = | |
255 let | |
256 val (_, pds) = ListUtil.mapfoldl (fn (d, env) => | |
257 (E.declBinds env d, | |
258 p_decl env d)) | |
259 env file | |
260 in | |
261 p_list_sep newline (fn x => x) pds | |
262 end | |
263 | |
264 end |