Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/pure-cfg/src/compiler/ast/ast-pp.sml
ViewVC logotype

Annotation of /branches/pure-cfg/src/compiler/ast/ast-pp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 477 - (view) (download)

1 : jhr 93 (* ast-pp.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 93 * 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 : jhr 381 fun ppIndex (ppStrm, NONE) = PP.string ppStrm ":"
50 :     | ppIndex (ppStrm, SOME e) = ppExp (ppStrm, e)
51 : jhr 94 fun pp e = (case e
52 : jhr 170 of AST.E_Var x => var x
53 : jhr 94 | AST.E_Lit lit => string (Literal.toString lit)
54 :     | AST.E_Tuple es => ppArgs (ppStrm, es)
55 :     | AST.E_Apply(f, [], args, _) => (var f; sp(); ppArgs (ppStrm, args))
56 :     | AST.E_Apply(f, mvs, args, _) => (
57 : jhr 96 var f; ppTyArgs (ppStrm, mvs); sp(); ppArgs (ppStrm, args))
58 : jhr 94 | AST.E_Cons es => (
59 :     ppList ppExp ("[", ",", "]") (ppStrm, es))
60 : jhr 399 | AST.E_Slice(e, indices, _) => (
61 : jhr 381 pp e;
62 :     ppList ppIndex ("[", ",", "]") (ppStrm, indices))
63 : jhr 416 | AST.E_Cond(e1, e2, e3, _) => (
64 :     pp e2; sp(); string "if"; sp(); pp e1; sp(); string "else"; sp(); pp e3)
65 : jhr 94 (* end case *))
66 :     in
67 :     pp e
68 :     end
69 :    
70 :     and ppArgs (ppStrm, args) = ppList ppExp ("(", ",", ")") (ppStrm, args)
71 :    
72 :     fun ppVarDecl ppStrm (AST.VD_Decl(x, e)) = let
73 :     fun sp () = PP.space ppStrm 1
74 :     val string = PP.string ppStrm
75 :     in
76 :     PP.openHBox ppStrm;
77 : jhr 173 case Var.kindOf x
78 :     of AST.InputVar => (string "input"; sp())
79 :     | AST.ActorOutputVar => (string "output"; sp())
80 :     | _ => ()
81 :     (* end case *);
82 :     string(TU.toString(#2(Var.typeOf x))); sp(); string(Var.nameOf x);
83 : jhr 94 sp(); string "="; sp(); ppExp(ppStrm, e); string ";";
84 :     PP.closeBox ppStrm
85 :     end
86 :    
87 :     fun ppBlock (ppStrm, stms) = let
88 :     fun sp () = PP.space ppStrm 1
89 :     fun nl () = PP.newline ppStrm
90 :     val string = PP.string ppStrm
91 :     fun var x = string(Var.nameOf x)
92 :     fun ppStmt stmt = (case stmt
93 :     of AST.S_Block stms => ppBlock (ppStrm, stms)
94 :     | AST.S_Decl vdcl => (ppVarDecl ppStrm vdcl; nl())
95 :     | AST.S_IfThenElse(e, AST.S_Block stms, AST.S_Block[]) => (
96 :     PP.openHBox ppStrm;
97 :     string "if"; sp(); ppExp(ppStrm, e);
98 :     sp(); ppBlock (ppStrm, stms);
99 :     PP.closeBox ppStrm)
100 :     | AST.S_IfThenElse(e, s1, AST.S_Block[]) => (
101 :     PP.openVBox ppStrm indent;
102 :     PP.openHBox ppStrm;
103 :     string "if"; sp(); ppExp(ppStrm, e);
104 :     PP.closeBox ppStrm;
105 :     nl();
106 :     ppStmt s1;
107 :     PP.closeBox ppStrm;
108 :     nl())
109 :     | AST.S_IfThenElse(e, AST.S_Block stms1, AST.S_Block stms2) => (
110 :     PP.openHBox ppStrm;
111 :     string "if"; sp(); ppExp(ppStrm, e);
112 : jhr 103 sp(); ppBlock (ppStrm, stms1);
113 : jhr 94 PP.closeBox ppStrm;
114 :     PP.openHBox ppStrm;
115 : jhr 103 string "else"; sp(); ppBlock (ppStrm, stms2);
116 : jhr 94 PP.closeBox ppStrm)
117 :     | AST.S_IfThenElse(e, AST.S_Block stms1, s2) => raise Fail "FIXME"
118 :     | AST.S_IfThenElse(e, s1, AST.S_Block stms2) => raise Fail "FIXME"
119 :     | AST.S_IfThenElse(e, s1, s2) => (
120 :     PP.openVBox ppStrm indent;
121 :     PP.openHBox ppStrm;
122 :     string "if"; sp(); ppExp(ppStrm, e);
123 :     PP.closeBox ppStrm;
124 :     nl();
125 :     ppStmt s1;
126 :     PP.closeBox ppStrm;
127 :     nl();
128 :     PP.openVBox ppStrm indent;
129 :     string "else"; nl();
130 :     ppStmt s2;
131 :     PP.closeBox ppStrm;
132 :     nl())
133 :     | AST.S_Assign(x, e) => (
134 :     PP.openHBox ppStrm;
135 :     var x; sp(); string "="; sp(); ppExp(ppStrm, e); string ";";
136 :     PP.closeBox ppStrm;
137 :     nl())
138 :     | AST.S_New(actor, args) => (
139 :     PP.openHBox ppStrm;
140 :     string "new"; sp(); string(Atom.toString actor); sp();
141 :     ppArgs (ppStrm, args); string ";";
142 :     PP.closeBox ppStrm;
143 :     nl())
144 :     | AST.S_Die => (string "die;"; nl())
145 :     | AST.S_Stabilize => (string "stabilize;"; nl())
146 :     (* end case *))
147 :     in
148 :     PP.openVBox ppStrm (PP.Abs 0);
149 :     string "{"; nl();
150 :     PP.openVBox ppStrm indent;
151 :     List.app ppStmt stms;
152 :     PP.closeBox ppStrm;
153 :     string "}"; nl();
154 :     PP.closeBox ppStrm
155 :     end
156 :    
157 :     fun ppActor (ppStrm, {name, params, state, methods}) = let
158 :     fun sp () = PP.space ppStrm 1
159 :     fun nl () = PP.newline ppStrm
160 :     val string = PP.string ppStrm
161 :     fun var x = string(Var.nameOf x)
162 :     fun ppMethod (AST.M_Method(name, AST.S_Block stms)) = (
163 :     nl(); string(Atom.toString name); nl(); ppBlock (ppStrm, stms))
164 :     | ppMethod (AST.M_Method(name, stm)) = (
165 :     nl(); string(Atom.toString name); nl(); ppBlock (ppStrm, [stm]))
166 :     in
167 :     PP.openHBox ppStrm;
168 :     string "actor"; sp(); string(Atom.toString name); sp();
169 : jhr 96 ppList (fn (_, x) => (string(TU.toString(#2(Var.typeOf x))); sp(); var x))
170 : jhr 94 ("(", ",", ")") (ppStrm, params);
171 :     PP.closeBox ppStrm;
172 :     nl();
173 :     PP.openVBox ppStrm indent;
174 :     string "{";
175 : jhr 173 List.app (fn vdcl => (nl(); ppVarDecl ppStrm vdcl)) state;
176 : jhr 94 List.app ppMethod methods;
177 :     PP.closeBox ppStrm;
178 :     nl();
179 :     string "}"; nl()
180 :     end
181 :    
182 :     fun ppDecl ppStrm = let
183 :     fun sp () = PP.space ppStrm 1
184 :     fun nl () = PP.newline ppStrm
185 :     val string = PP.string ppStrm
186 :     fun var x = string(Var.nameOf x)
187 :     in
188 :     fn AST.D_Input(x, NONE) => (
189 :     PP.openHBox ppStrm;
190 :     string "input"; sp();
191 : jhr 96 string(TU.toString(#2(Var.typeOf x))); sp(); var x; string ";";
192 : jhr 94 PP.closeBox ppStrm;
193 :     nl())
194 :     | AST.D_Input(x, SOME e) => (
195 :     PP.openHBox ppStrm;
196 :     string "input"; sp();
197 : jhr 96 string(TU.toString(#2(Var.typeOf x))); sp(); var x;
198 : jhr 94 sp(); string "="; sp(); ppExp(ppStrm, e); string ";";
199 :     PP.closeBox ppStrm;
200 :     nl())
201 :     | AST.D_Var vdcl => (ppVarDecl ppStrm vdcl; nl())
202 :     | AST.D_Actor def => ppActor (ppStrm, def)
203 :     | AST.D_InitialArray(create, iters) => (* FIXME *) ()
204 :     | AST.D_InitialCollection(create, iters) => (* FIXME *) ()
205 :     end
206 :    
207 :     fun output (outS, AST.Program decls) = let
208 : jhr 93 val ppStrm = PP.openOut {dst = outS, wid = 120}
209 :     in
210 : jhr 94 PP.openVBox ppStrm (PP.Abs 0);
211 :     PP.string ppStrm "/* Program start */"; PP.newline ppStrm;
212 :     List.app (ppDecl ppStrm) decls;
213 :     PP.string ppStrm "/* Program end */"; PP.newline ppStrm;
214 :     PP.closeBox ppStrm;
215 : jhr 93 PP.closeStream ppStrm
216 :     end
217 :    
218 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0