SCM Repository
Annotation of /trunk/src/compiler/ast/ast-pp.sml
Parent Directory
|
Revision Log
Revision 96 -
(view)
(download)
Original Path: trunk/src/ast/ast-pp.sml
1 : | jhr | 93 | (* ast-pp.sml |
2 : | * | ||
3 : | * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu) | ||
4 : | * All rights reserved. | ||
5 : | jhr | 96 | * |
6 : | * Pretty printing for the AST representation. | ||
7 : | jhr | 93 | *) |
8 : | |||
9 : | structure ASTPP : sig | ||
10 : | |||
11 : | val output : TextIO.outstream * AST.program -> unit | ||
12 : | |||
13 : | end = struct | ||
14 : | |||
15 : | structure PP = TextIOPP | ||
16 : | jhr | 96 | structure TU = TypeUtil |
17 : | jhr | 93 | |
18 : | jhr | 94 | val indent = PP.Abs 2 |
19 : | |||
20 : | fun ppList ppFn (left, sep, right) (ppStrm, list) = let | ||
21 : | fun sp () = PP.space ppStrm 1 | ||
22 : | val string = PP.string ppStrm | ||
23 : | fun pp [] = string right | ||
24 : | | pp [x] = (ppFn(ppStrm, x); string right) | ||
25 : | | pp (x::xs) = (ppFn(ppStrm, x); string sep; sp(); pp xs) | ||
26 : | in | ||
27 : | string left; pp list | ||
28 : | end | ||
29 : | |||
30 : | jhr | 96 | (* print type arguments; we use "#" to denote differentiation arguments, "$" to denote |
31 : | * shape arguments, and "%" to denote dimension arguments. | ||
32 : | *) | ||
33 : | fun ppTyArgs (ppStrm, mvs) = let | ||
34 : | val string = PP.string ppStrm | ||
35 : | fun ppTyArg (_, mv) = (case mv | ||
36 : | of Types.TYPE tv => string(TU.toString(TU.resolve tv)) | ||
37 : | | Types.DIFF dv => string("#"^TU.diffToString(TU.resolveDiff dv)) | ||
38 : | | Types.SHAPE sv => string("$"^TU.shapeToString(TU.resolveShape sv)) | ||
39 : | | Types.DIM dv => string("%"^TU.dimToString(TU.resolveDim dv)) | ||
40 : | (* end case *)) | ||
41 : | in | ||
42 : | ppList ppTyArg ("<", ";", ">") (ppStrm, mvs) | ||
43 : | end | ||
44 : | |||
45 : | jhr | 94 | fun ppExp (ppStrm, e) = let |
46 : | fun sp () = PP.space ppStrm 1 | ||
47 : | val string = PP.string ppStrm | ||
48 : | fun var x = string(Var.nameOf x) | ||
49 : | fun pp e = (case e | ||
50 : | of AST.E_Var(x, [], _) => var x | ||
51 : | jhr | 96 | | AST.E_Var(x, mvs, ty) => (var x; ppTyArgs (ppStrm, mvs)) |
52 : | jhr | 94 | | AST.E_Lit lit => string (Literal.toString lit) |
53 : | | AST.E_Tuple es => ppArgs (ppStrm, es) | ||
54 : | | AST.E_Apply(f, [], args, _) => (var f; sp(); ppArgs (ppStrm, args)) | ||
55 : | | AST.E_Apply(f, mvs, args, _) => ( | ||
56 : | jhr | 96 | var f; ppTyArgs (ppStrm, mvs); sp(); ppArgs (ppStrm, args)) |
57 : | jhr | 94 | | AST.E_Cons es => ( |
58 : | ppList ppExp ("[", ",", "]") (ppStrm, es)) | ||
59 : | | AST.E_Cond(e1, e2, e3) => ( | ||
60 : | pp e1; sp(); string "?"; sp(); pp e2; sp(); string ":"; sp(); pp e3) | ||
61 : | (* end case *)) | ||
62 : | in | ||
63 : | pp e | ||
64 : | end | ||
65 : | |||
66 : | and ppArgs (ppStrm, args) = ppList ppExp ("(", ",", ")") (ppStrm, args) | ||
67 : | |||
68 : | fun ppVarDecl ppStrm (AST.VD_Decl(x, e)) = let | ||
69 : | fun sp () = PP.space ppStrm 1 | ||
70 : | val string = PP.string ppStrm | ||
71 : | fun var x = string(Var.nameOf x) | ||
72 : | in | ||
73 : | PP.openHBox ppStrm; | ||
74 : | jhr | 96 | string(TU.toString(#2(Var.typeOf x))); sp(); var x; |
75 : | jhr | 94 | sp(); string "="; sp(); ppExp(ppStrm, e); string ";"; |
76 : | PP.closeBox ppStrm | ||
77 : | end | ||
78 : | |||
79 : | fun ppBlock (ppStrm, stms) = let | ||
80 : | fun sp () = PP.space ppStrm 1 | ||
81 : | fun nl () = PP.newline ppStrm | ||
82 : | val string = PP.string ppStrm | ||
83 : | fun var x = string(Var.nameOf x) | ||
84 : | fun ppStmt stmt = (case stmt | ||
85 : | of AST.S_Block stms => ppBlock (ppStrm, stms) | ||
86 : | | AST.S_Decl vdcl => (ppVarDecl ppStrm vdcl; nl()) | ||
87 : | | AST.S_IfThenElse(e, AST.S_Block stms, AST.S_Block[]) => ( | ||
88 : | PP.openHBox ppStrm; | ||
89 : | string "if"; sp(); ppExp(ppStrm, e); | ||
90 : | sp(); ppBlock (ppStrm, stms); | ||
91 : | PP.closeBox ppStrm) | ||
92 : | | AST.S_IfThenElse(e, s1, AST.S_Block[]) => ( | ||
93 : | PP.openVBox ppStrm indent; | ||
94 : | PP.openHBox ppStrm; | ||
95 : | string "if"; sp(); ppExp(ppStrm, e); | ||
96 : | PP.closeBox ppStrm; | ||
97 : | nl(); | ||
98 : | ppStmt s1; | ||
99 : | PP.closeBox ppStrm; | ||
100 : | nl()) | ||
101 : | | AST.S_IfThenElse(e, AST.S_Block stms1, AST.S_Block stms2) => ( | ||
102 : | PP.openHBox ppStrm; | ||
103 : | string "if"; sp(); ppExp(ppStrm, e); | ||
104 : | sp(); ppBlock (ppStrm, stms); | ||
105 : | PP.closeBox ppStrm; | ||
106 : | PP.openHBox ppStrm; | ||
107 : | string "else"; sp(); ppBlock (ppStrm, stms); | ||
108 : | PP.closeBox ppStrm) | ||
109 : | | AST.S_IfThenElse(e, AST.S_Block stms1, s2) => raise Fail "FIXME" | ||
110 : | | AST.S_IfThenElse(e, s1, AST.S_Block stms2) => raise Fail "FIXME" | ||
111 : | | AST.S_IfThenElse(e, s1, s2) => ( | ||
112 : | PP.openVBox ppStrm indent; | ||
113 : | PP.openHBox ppStrm; | ||
114 : | string "if"; sp(); ppExp(ppStrm, e); | ||
115 : | PP.closeBox ppStrm; | ||
116 : | nl(); | ||
117 : | ppStmt s1; | ||
118 : | PP.closeBox ppStrm; | ||
119 : | nl(); | ||
120 : | PP.openVBox ppStrm indent; | ||
121 : | string "else"; nl(); | ||
122 : | ppStmt s2; | ||
123 : | PP.closeBox ppStrm; | ||
124 : | nl()) | ||
125 : | | AST.S_Assign(x, e) => ( | ||
126 : | PP.openHBox ppStrm; | ||
127 : | var x; sp(); string "="; sp(); ppExp(ppStrm, e); string ";"; | ||
128 : | PP.closeBox ppStrm; | ||
129 : | nl()) | ||
130 : | | AST.S_New(actor, args) => ( | ||
131 : | PP.openHBox ppStrm; | ||
132 : | string "new"; sp(); string(Atom.toString actor); sp(); | ||
133 : | ppArgs (ppStrm, args); string ";"; | ||
134 : | PP.closeBox ppStrm; | ||
135 : | nl()) | ||
136 : | | AST.S_Die => (string "die;"; nl()) | ||
137 : | | AST.S_Stabilize => (string "stabilize;"; nl()) | ||
138 : | (* end case *)) | ||
139 : | in | ||
140 : | PP.openVBox ppStrm (PP.Abs 0); | ||
141 : | string "{"; nl(); | ||
142 : | PP.openVBox ppStrm indent; | ||
143 : | List.app ppStmt stms; | ||
144 : | PP.closeBox ppStrm; | ||
145 : | string "}"; nl(); | ||
146 : | PP.closeBox ppStrm | ||
147 : | end | ||
148 : | |||
149 : | fun ppActor (ppStrm, {name, params, state, methods}) = let | ||
150 : | fun sp () = PP.space ppStrm 1 | ||
151 : | fun nl () = PP.newline ppStrm | ||
152 : | val string = PP.string ppStrm | ||
153 : | fun var x = string(Var.nameOf x) | ||
154 : | fun ppMethod (AST.M_Method(name, AST.S_Block stms)) = ( | ||
155 : | nl(); string(Atom.toString name); nl(); ppBlock (ppStrm, stms)) | ||
156 : | | ppMethod (AST.M_Method(name, stm)) = ( | ||
157 : | nl(); string(Atom.toString name); nl(); ppBlock (ppStrm, [stm])) | ||
158 : | in | ||
159 : | PP.openHBox ppStrm; | ||
160 : | string "actor"; sp(); string(Atom.toString name); sp(); | ||
161 : | jhr | 96 | ppList (fn (_, x) => (string(TU.toString(#2(Var.typeOf x))); sp(); var x)) |
162 : | jhr | 94 | ("(", ",", ")") (ppStrm, params); |
163 : | PP.closeBox ppStrm; | ||
164 : | nl(); | ||
165 : | PP.openVBox ppStrm indent; | ||
166 : | string "{"; | ||
167 : | List.app (fn vdcl => (nl(); ppVarDecl ppStrm vdcl)) state; | ||
168 : | List.app ppMethod methods; | ||
169 : | PP.closeBox ppStrm; | ||
170 : | nl(); | ||
171 : | string "}"; nl() | ||
172 : | end | ||
173 : | |||
174 : | fun ppDecl ppStrm = let | ||
175 : | fun sp () = PP.space ppStrm 1 | ||
176 : | fun nl () = PP.newline ppStrm | ||
177 : | val string = PP.string ppStrm | ||
178 : | fun var x = string(Var.nameOf x) | ||
179 : | in | ||
180 : | fn AST.D_Input(x, NONE) => ( | ||
181 : | PP.openHBox ppStrm; | ||
182 : | string "input"; sp(); | ||
183 : | jhr | 96 | string(TU.toString(#2(Var.typeOf x))); sp(); var x; string ";"; |
184 : | jhr | 94 | PP.closeBox ppStrm; |
185 : | nl()) | ||
186 : | | AST.D_Input(x, SOME e) => ( | ||
187 : | PP.openHBox ppStrm; | ||
188 : | string "input"; sp(); | ||
189 : | jhr | 96 | string(TU.toString(#2(Var.typeOf x))); sp(); var x; |
190 : | jhr | 94 | sp(); string "="; sp(); ppExp(ppStrm, e); string ";"; |
191 : | PP.closeBox ppStrm; | ||
192 : | nl()) | ||
193 : | | AST.D_Var vdcl => (ppVarDecl ppStrm vdcl; nl()) | ||
194 : | | AST.D_Actor def => ppActor (ppStrm, def) | ||
195 : | | AST.D_InitialArray(create, iters) => (* FIXME *) () | ||
196 : | | AST.D_InitialCollection(create, iters) => (* FIXME *) () | ||
197 : | end | ||
198 : | |||
199 : | fun output (outS, AST.Program decls) = let | ||
200 : | jhr | 93 | val ppStrm = PP.openOut {dst = outS, wid = 120} |
201 : | in | ||
202 : | jhr | 94 | PP.openVBox ppStrm (PP.Abs 0); |
203 : | PP.string ppStrm "/* Program start */"; PP.newline ppStrm; | ||
204 : | List.app (ppDecl ppStrm) decls; | ||
205 : | PP.string ppStrm "/* Program end */"; PP.newline ppStrm; | ||
206 : | PP.closeBox ppStrm; | ||
207 : | jhr | 93 | PP.closeStream ppStrm |
208 : | end | ||
209 : | |||
210 : | end |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |