SCM Repository
Annotation of /branches/charisee/src/compiler/simplify/simple-pp.sml
Parent Directory
|
Revision Log
Revision 2604 - (view) (download)
1 : | jhr | 172 | (* simple-pp.sml |
2 : | * | ||
3 : | jhr | 435 | * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu) |
4 : | jhr | 172 | * All rights reserved. |
5 : | * | ||
6 : | * Pretty printing for the Simple-AST representation. | ||
7 : | *) | ||
8 : | |||
9 : | structure SimplePP : sig | ||
10 : | |||
11 : | jhr | 2356 | val output : TextIO.outstream * string * Simple.program -> unit |
12 : | jhr | 172 | |
13 : | jhr | 2604 | val outputFunc : TextIO.outstream * string * Simple.func -> unit |
14 : | |||
15 : | jhr | 172 | end = struct |
16 : | |||
17 : | structure PP = TextIOPP | ||
18 : | jhr | 2490 | structure Ty = SimpleTypes |
19 : | jhr | 172 | structure S = Simple |
20 : | |||
21 : | val indent = PP.Abs 2 | ||
22 : | |||
23 : | fun ppList ppFn (left, sep, right) (ppStrm, list) = let | ||
24 : | jhr | 2356 | fun sp () = PP.space ppStrm 1 |
25 : | val string = PP.string ppStrm | ||
26 : | fun pp [] = string right | ||
27 : | | pp [x] = (ppFn(ppStrm, x); string right) | ||
28 : | | pp (x::xs) = (ppFn(ppStrm, x); string sep; sp(); pp xs) | ||
29 : | in | ||
30 : | string left; pp list | ||
31 : | end | ||
32 : | jhr | 172 | |
33 : | (* print type arguments; we use "#" to denote differentiation arguments, "$" to denote | ||
34 : | * shape arguments, and "%" to denote dimension arguments. | ||
35 : | *) | ||
36 : | fun ppTyArgs (ppStrm, mvs) = let | ||
37 : | jhr | 2356 | val string = PP.string ppStrm |
38 : | fun ppTyArg (_, mv) = (case mv | ||
39 : | jhr | 2490 | of Ty.TY ty => string(Ty.toString ty) |
40 : | | Ty.DIFF k => string("#"^Int.toString k) | ||
41 : | | Ty.SHAPE shp => string(concat[ | ||
42 : | jhr | 2492 | "$[", String.concatWith "," (List.map Int.toString shp), "]" |
43 : | ]) | ||
44 : | jhr | 2490 | | Ty.DIM d => string("%"^Int.toString d) |
45 : | jhr | 2356 | (* end case *)) |
46 : | in | ||
47 : | ppList ppTyArg ("<", ";", ">") (ppStrm, mvs) | ||
48 : | end | ||
49 : | jhr | 172 | |
50 : | jhr | 2490 | fun ppVar (ppStrm, x) = PP.string ppStrm (SimpleVar.uniqueNameOf x) |
51 : | jhr | 172 | |
52 : | fun ppVarDecl ppStrm = let | ||
53 : | jhr | 2356 | fun sp () = PP.space ppStrm 1 |
54 : | val string = PP.string ppStrm | ||
55 : | in | ||
56 : | fn x => ( | ||
57 : | PP.openHBox ppStrm; | ||
58 : | jhr | 2490 | case SimpleVar.kindOf x |
59 : | jhr | 2356 | of S.InputVar => (string "input"; sp()) |
60 : | | S.StrandOutputVar => (string "output"; sp()) | ||
61 : | | _ => () | ||
62 : | (* end case *); | ||
63 : | jhr | 2490 | string(Ty.toString(SimpleVar.typeOf x)); sp(); string(SimpleVar.uniqueNameOf x); string ";"; |
64 : | jhr | 2356 | PP.closeBox ppStrm) |
65 : | end | ||
66 : | jhr | 172 | |
67 : | fun ppExp (ppStrm, e) = let | ||
68 : | jhr | 2356 | fun sp () = PP.space ppStrm 1 |
69 : | val string = PP.string ppStrm | ||
70 : | fun var x = ppVar (ppStrm, x) | ||
71 : | fun ppIndex (ppStrm, NONE) = PP.string ppStrm ":" | ||
72 : | | ppIndex (ppStrm, SOME i) = var i | ||
73 : | fun pp e = (case e | ||
74 : | of S.E_Var x => var x | ||
75 : | | S.E_Lit lit => string (Literal.toString lit) | ||
76 : | | S.E_Tuple es => ppArgs (ppStrm, es) | ||
77 : | jhr | 2490 | | S.E_Apply(f, args, _) => (var f; sp(); ppArgs (ppStrm, args)) |
78 : | | S.E_Prim(f, [], args, _) => (string(Var.nameOf f); sp(); ppArgs (ppStrm, args)) | ||
79 : | | S.E_Prim(f, mvs, args, _) => ( | ||
80 : | string(Var.nameOf f); ppTyArgs (ppStrm, mvs); sp(); ppArgs (ppStrm, args)) | ||
81 : | jhr | 2356 | | S.E_Cons es => ( |
82 : | ppList ppVar ("[", ",", "]") (ppStrm, es)) | ||
83 : | | S.E_Slice(x, indices, _) => ( | ||
84 : | var x; | ||
85 : | ppList ppIndex ("[", ",", "]") (ppStrm, indices)) | ||
86 : | | S.E_Coerce{srcTy, dstTy, x} => ( | ||
87 : | jhr | 2490 | string "("; string(Ty.toString dstTy); string ")"; var x) |
88 : | jhr | 2356 | | S.E_Input(ty, argName, desc, NONE) => ( |
89 : | string(concat["input(\"", argName, "\","]); sp(); | ||
90 : | jhr | 2492 | case desc |
91 : | of SOME desc => ( | ||
92 : | string (concat["\"", String.toString desc, "\")"]); sp()) | ||
93 : | | NONE => string ")" | ||
94 : | (* end case *)) | ||
95 : | jhr | 2356 | | S.E_Input(ty, argName, desc, SOME default) => ( |
96 : | string "inputWithDefault"; string "("; | ||
97 : | string (concat["\"", argName, "\","]); sp(); | ||
98 : | jhr | 2492 | case desc |
99 : | of SOME desc => ( | ||
100 : | string (concat["\"", String.toString desc, "\","]); sp()) | ||
101 : | | NONE => () | ||
102 : | (* end case *); | ||
103 : | jhr | 2356 | var default; string ")") |
104 : | | S.E_LoadImage(info, x) => ( | ||
105 : | string "load"; sp(); string "("; | ||
106 : | string(ImageInfo.toString info); sp(); string ","; | ||
107 : | var x; string ")") | ||
108 : | (* end case *)) | ||
109 : | in | ||
110 : | pp e | ||
111 : | end | ||
112 : | jhr | 172 | |
113 : | and ppArgs (ppStrm, args) = ppList ppVar ("(", ",", ")") (ppStrm, args) | ||
114 : | |||
115 : | jhr | 1116 | fun ppBlock (ppStrm, [], S.Block[]) = PP.string ppStrm "{ }" |
116 : | | ppBlock (ppStrm, vars, S.Block stms) = let | ||
117 : | jhr | 2356 | fun sp () = PP.space ppStrm 1 |
118 : | fun nl () = PP.newline ppStrm | ||
119 : | val string = PP.string ppStrm | ||
120 : | fun var x = ppVar (ppStrm, x) | ||
121 : | fun ppStmt stmt = ( | ||
122 : | nl(); | ||
123 : | case stmt | ||
124 : | of S.S_Var x => ( | ||
125 : | PP.openHBox ppStrm; | ||
126 : | jhr | 2490 | string(Ty.toString(SimpleVar.typeOf x)); sp(); var x; string ";"; |
127 : | jhr | 2356 | PP.closeBox ppStrm) |
128 : | | S.S_Assign(x, e) => ( | ||
129 : | PP.openHBox ppStrm; | ||
130 : | var x; sp(); string "="; sp(); ppExp(ppStrm, e); string ";"; | ||
131 : | PP.closeBox ppStrm) | ||
132 : | | S.S_IfThenElse(x, S.Block[s1], S.Block[]) => ( | ||
133 : | PP.openVBox ppStrm indent; | ||
134 : | PP.openHBox ppStrm; | ||
135 : | string "if"; sp(); ppVar(ppStrm, x); | ||
136 : | PP.closeBox ppStrm; | ||
137 : | ppStmt s1; | ||
138 : | PP.closeBox ppStrm) | ||
139 : | | S.S_IfThenElse(x, blk, S.Block[]) => ( | ||
140 : | PP.openHBox ppStrm; | ||
141 : | string "if"; sp(); ppVar(ppStrm, x); | ||
142 : | sp(); ppBlock (ppStrm, [], blk); | ||
143 : | PP.closeBox ppStrm) | ||
144 : | | S.S_IfThenElse(x, S.Block[s1], S.Block[s2]) => ( | ||
145 : | PP.openVBox ppStrm indent; | ||
146 : | PP.openHBox ppStrm; | ||
147 : | string "if"; sp(); ppVar(ppStrm, x); | ||
148 : | PP.closeBox ppStrm; | ||
149 : | ppStmt s1; | ||
150 : | PP.closeBox ppStrm; | ||
151 : | nl(); | ||
152 : | PP.openVBox ppStrm indent; | ||
153 : | string "else"; | ||
154 : | ppStmt s2; | ||
155 : | PP.closeBox ppStrm) | ||
156 : | | S.S_IfThenElse(x, blk1, blk2) => ( | ||
157 : | PP.openHBox ppStrm; | ||
158 : | string "if"; sp(); ppVar(ppStrm, x); | ||
159 : | sp(); ppBlock (ppStrm, [], blk1); | ||
160 : | PP.closeBox ppStrm; | ||
161 : | PP.openHBox ppStrm; | ||
162 : | sp(); string "else"; sp(); ppBlock (ppStrm, [], blk2); | ||
163 : | PP.closeBox ppStrm) | ||
164 : | | S.S_New(strand, args) => ( | ||
165 : | PP.openHBox ppStrm; | ||
166 : | string "new"; sp(); string(Atom.toString strand); sp(); | ||
167 : | ppArgs (ppStrm, args); string ";"; | ||
168 : | PP.closeBox ppStrm) | ||
169 : | | S.S_Die => string "die;" | ||
170 : | | S.S_Stabilize => string "stabilize;" | ||
171 : | | S.S_Return x => ( | ||
172 : | PP.openHBox ppStrm; | ||
173 : | string "return"; sp(); ppVar(ppStrm, x); string ";"; | ||
174 : | PP.closeBox ppStrm) | ||
175 : | jhr | 1640 | | S.S_Print args => ( |
176 : | PP.openHBox ppStrm; | ||
177 : | string "print"; sp(); ppArgs (ppStrm, args); string ";"; | ||
178 : | jhr | 2356 | PP.closeBox ppStrm) |
179 : | (* end case *)) | ||
180 : | in | ||
181 : | PP.openVBox ppStrm (PP.Abs 0); | ||
182 : | string "{"; | ||
183 : | PP.openVBox ppStrm indent; | ||
184 : | List.app (fn vdcl => (nl(); ppVarDecl ppStrm vdcl)) vars; | ||
185 : | List.app ppStmt stms; | ||
186 : | PP.closeBox ppStrm; | ||
187 : | nl(); string "}"; | ||
188 : | PP.closeBox ppStrm | ||
189 : | end | ||
190 : | jhr | 172 | |
191 : | jhr | 2356 | fun ppParams (ppStrm, params) = let |
192 : | fun sp () = PP.space ppStrm 1 | ||
193 : | val string = PP.string ppStrm | ||
194 : | in | ||
195 : | ppList | ||
196 : | jhr | 2490 | (fn (_, x) => (string(Ty.toString(SimpleVar.typeOf x)); sp(); ppVar (ppStrm, x))) |
197 : | jhr | 2356 | ("(", ",", ")") |
198 : | (ppStrm, params) | ||
199 : | end | ||
200 : | |||
201 : | fun ppFunc ppStrm (S.Func{f, params, body}) = let | ||
202 : | fun sp () = PP.space ppStrm 1 | ||
203 : | fun nl () = PP.newline ppStrm | ||
204 : | val string = PP.string ppStrm | ||
205 : | fun var x = ppVar (ppStrm, x) | ||
206 : | in | ||
207 : | PP.openHBox ppStrm; | ||
208 : | string "function"; sp(); | ||
209 : | jhr | 2490 | string(Ty.toString(Ty.rngOf(SimpleVar.typeOf f))); |
210 : | jhr | 2356 | sp(); var f; sp(); ppParams (ppStrm, params); |
211 : | PP.closeBox ppStrm; | ||
212 : | nl(); | ||
213 : | ppBlock (ppStrm, [], body); | ||
214 : | nl() | ||
215 : | end | ||
216 : | |||
217 : | jhr | 1116 | fun ppInit (ppStrm, S.Initially{isArray, rangeInit, iters, create}) = let |
218 : | jhr | 2356 | fun sp () = PP.space ppStrm 1 |
219 : | fun nl () = PP.newline ppStrm | ||
220 : | val string = PP.string ppStrm | ||
221 : | fun var x = ppVar (ppStrm, x) | ||
222 : | val label = if isArray then "Array" else "Collection" | ||
223 : | fun ppIters [] = let | ||
224 : | val S.C_Create{argInit, name, args} = create | ||
225 : | in | ||
226 : | ppBlock (ppStrm, [], argInit); nl(); | ||
227 : | PP.openHBox ppStrm; | ||
228 : | string "new"; sp(); string(Atom.toString name); | ||
229 : | ppArgs (ppStrm, args); string ";"; | ||
230 : | PP.closeBox ppStrm | ||
231 : | end | ||
232 : | | ppIters ({param, lo, hi} :: iters) = ( | ||
233 : | PP.openVBox ppStrm indent; | ||
234 : | PP.openHBox ppStrm; | ||
235 : | string "for"; sp(); | ||
236 : | jhr | 2490 | string(Ty.toString(SimpleVar.typeOf param)); sp(); var param; |
237 : | jhr | 2356 | sp(); string "="; sp(); var lo; sp(); string ".."; sp(); var hi; |
238 : | PP.closeBox ppStrm; | ||
239 : | nl(); | ||
240 : | ppIters iters; | ||
241 : | PP.closeBox ppStrm) | ||
242 : | in | ||
243 : | PP.openVBox ppStrm indent; | ||
244 : | string label; nl(); | ||
245 : | ppBlock (ppStrm, [], rangeInit); nl(); | ||
246 : | ppIters iters; | ||
247 : | PP.closeBox ppStrm; | ||
248 : | nl() | ||
249 : | end | ||
250 : | jhr | 1116 | |
251 : | jhr | 511 | fun ppStrand ppStrm (S.Strand{name, params, state, stateInit, methods}) = let |
252 : | jhr | 2356 | fun sp () = PP.space ppStrm 1 |
253 : | fun nl () = PP.newline ppStrm | ||
254 : | val string = PP.string ppStrm | ||
255 : | fun var x = ppVar (ppStrm, x) | ||
256 : | fun ppMethod (S.Method(name, body)) = ( | ||
257 : | nl(); string(StrandUtil.nameToString name); nl(); ppBlock (ppStrm, [], body)) | ||
258 : | in | ||
259 : | PP.openHBox ppStrm; | ||
260 : | string "strand"; sp(); string(Atom.toString name); sp(); | ||
261 : | ppParams (ppStrm, params); | ||
262 : | PP.closeBox ppStrm; | ||
263 : | nl(); | ||
264 : | PP.openVBox ppStrm indent; | ||
265 : | string "{"; | ||
266 : | ppBlock (ppStrm, state, stateInit); | ||
267 : | List.app ppMethod methods; | ||
268 : | PP.closeBox ppStrm; | ||
269 : | nl(); | ||
270 : | string "}"; nl() | ||
271 : | end | ||
272 : | jhr | 172 | |
273 : | jhr | 2356 | fun output (outS, msg, S.Program{globals, globalInit, funcs, strands, init}) = let |
274 : | val ppStrm = PP.openOut {dst = outS, wid = 120} | ||
275 : | fun nl () = PP.newline ppStrm | ||
276 : | in | ||
277 : | PP.openVBox ppStrm (PP.Abs 0); | ||
278 : | PP.string ppStrm (concat[ | ||
279 : | "/* Simplified Program (after ", msg, ") start */" | ||
280 : | ]); nl(); | ||
281 : | List.app (ppFunc ppStrm) funcs; | ||
282 : | ppBlock (ppStrm, globals, globalInit); | ||
283 : | nl(); | ||
284 : | ppInit (ppStrm, init); | ||
285 : | List.app (ppStrand ppStrm) strands; | ||
286 : | PP.string ppStrm "/* Program end */"; PP.newline ppStrm; | ||
287 : | PP.closeBox ppStrm; | ||
288 : | PP.closeStream ppStrm | ||
289 : | end | ||
290 : | jhr | 172 | |
291 : | jhr | 2604 | fun outputFunc (outS, msg, func) = let |
292 : | val ppStrm = PP.openOut {dst = outS, wid = 120} | ||
293 : | in | ||
294 : | PP.openVBox ppStrm (PP.Abs 0); | ||
295 : | PP.string ppStrm (concat[ | ||
296 : | "/* ", msg, " */" | ||
297 : | ]); PP.newline ppStrm; | ||
298 : | ppFunc ppStrm func; | ||
299 : | PP.closeBox ppStrm; | ||
300 : | PP.closeStream ppStrm | ||
301 : | end | ||
302 : | |||
303 : | jhr | 172 | end |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |