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

SCM Repository

[diderot] Annotation of /trunk/src/compiler/simplify/simple-pp.sml
ViewVC logotype

Annotation of /trunk/src/compiler/simplify/simple-pp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 175 - (view) (download)

1 : jhr 172 (* simple-pp.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * Pretty printing for the Simple-AST representation.
7 :     *)
8 :    
9 :     structure SimplePP : sig
10 :    
11 :     val output : TextIO.outstream * Simple.program -> unit
12 :    
13 :     end = struct
14 :    
15 :     structure PP = TextIOPP
16 :     structure TU = TypeUtil
17 :     structure S = Simple
18 :    
19 :     val indent = PP.Abs 2
20 :    
21 :     fun ppList ppFn (left, sep, right) (ppStrm, list) = let
22 :     fun sp () = PP.space ppStrm 1
23 :     val string = PP.string ppStrm
24 :     fun pp [] = string right
25 :     | pp [x] = (ppFn(ppStrm, x); string right)
26 :     | pp (x::xs) = (ppFn(ppStrm, x); string sep; sp(); pp xs)
27 :     in
28 :     string left; pp list
29 :     end
30 :    
31 :     (* print type arguments; we use "#" to denote differentiation arguments, "$" to denote
32 :     * shape arguments, and "%" to denote dimension arguments.
33 :     *)
34 :     fun ppTyArgs (ppStrm, mvs) = let
35 :     val string = PP.string ppStrm
36 :     fun ppTyArg (_, mv) = (case mv
37 :     of Types.TYPE tv => string(TU.toString(TU.resolve tv))
38 :     | Types.DIFF dv => string("#"^TU.diffToString(TU.resolveDiff dv))
39 :     | Types.SHAPE sv => string("$"^TU.shapeToString(TU.resolveShape sv))
40 :     | Types.DIM dv => string("%"^TU.dimToString(TU.resolveDim dv))
41 :     (* end case *))
42 :     in
43 :     ppList ppTyArg ("<", ";", ">") (ppStrm, mvs)
44 :     end
45 :    
46 : jhr 175 fun ppVar (ppStrm, x) = PP.string ppStrm (Var.uniqueNameOf x)
47 : jhr 172
48 :     fun ppVarDecl ppStrm = let
49 :     fun sp () = PP.space ppStrm 1
50 :     val string = PP.string ppStrm
51 :     in
52 :     fn x => (
53 :     PP.openHBox ppStrm;
54 :     case Var.kindOf x
55 :     of S.InputVar => (string "input"; sp())
56 :     | S.ActorOutputVar => (string "output"; sp())
57 :     | _ => ()
58 :     (* end case *);
59 :     string(TU.toString(#2(Var.typeOf x))); sp(); string(Var.uniqueNameOf x); string ";";
60 :     PP.closeBox ppStrm)
61 :     end
62 :    
63 :     fun ppExp (ppStrm, e) = let
64 :     fun sp () = PP.space ppStrm 1
65 :     val string = PP.string ppStrm
66 :     fun var x = string(Var.nameOf x)
67 :     fun pp e = (case e
68 :     of S.E_Var x => string(Var.uniqueNameOf x)
69 :     | S.E_Lit lit => string (Literal.toString lit)
70 :     | S.E_Tuple es => ppArgs (ppStrm, es)
71 :     | S.E_Apply(f, [], args, _) => (var f; sp(); ppArgs (ppStrm, args))
72 :     | S.E_Apply(f, mvs, args, _) => (
73 :     var f; ppTyArgs (ppStrm, mvs); sp(); ppArgs (ppStrm, args))
74 :     | S.E_Cons es => (
75 :     ppList ppVar ("[", ",", "]") (ppStrm, es))
76 :     (* end case *))
77 :     in
78 :     pp e
79 :     end
80 :    
81 :     and ppArgs (ppStrm, args) = ppList ppVar ("(", ",", ")") (ppStrm, args)
82 :    
83 : jhr 175 fun ppBlock (ppStrm, vars, stms) = let
84 : jhr 172 fun sp () = PP.space ppStrm 1
85 :     fun nl () = PP.newline ppStrm
86 :     val string = PP.string ppStrm
87 : jhr 175 fun var x = string(Var.uniqueNameOf x)
88 : jhr 172 fun ppStmt stmt = (case stmt
89 : jhr 175 of S.S_Block stms => ppBlock (ppStrm, [], stms)
90 :     | S.S_Assign(x, e) => (
91 : jhr 172 PP.openHBox ppStrm;
92 : jhr 175 var x; sp(); string "="; sp(); ppExp(ppStrm, e); string ";";
93 :     PP.closeBox ppStrm;
94 :     nl())
95 :     | S.S_IfThenElse(x, S.S_Block stms, S.S_Block[]) => (
96 :     PP.openHBox ppStrm;
97 :     string "if"; sp(); ppVar(ppStrm, x);
98 :     sp(); ppBlock (ppStrm, [], stms);
99 : jhr 172 PP.closeBox ppStrm)
100 : jhr 175 | S.S_IfThenElse(x, s1, S.S_Block[]) => (
101 : jhr 172 PP.openVBox ppStrm indent;
102 :     PP.openHBox ppStrm;
103 : jhr 175 string "if"; sp(); ppVar(ppStrm, x);
104 : jhr 172 PP.closeBox ppStrm;
105 :     nl();
106 :     ppStmt s1;
107 :     PP.closeBox ppStrm;
108 :     nl())
109 : jhr 175 | S.S_IfThenElse(x, S.S_Block stms1, S.S_Block stms2) => (
110 : jhr 172 PP.openHBox ppStrm;
111 : jhr 175 string "if"; sp(); ppVar(ppStrm, x);
112 :     sp(); ppBlock (ppStrm, [], stms1);
113 : jhr 172 PP.closeBox ppStrm;
114 :     PP.openHBox ppStrm;
115 : jhr 175 string "else"; sp(); ppBlock (ppStrm, [], stms2);
116 : jhr 172 PP.closeBox ppStrm)
117 : jhr 175 | S.S_IfThenElse(x, S.S_Block stms1, s2) => raise Fail "FIXME"
118 :     | S.S_IfThenElse(x, s1, S.S_Block stms2) => raise Fail "FIXME"
119 :     | S.S_IfThenElse(x, s1, s2) => (
120 : jhr 172 PP.openVBox ppStrm indent;
121 :     PP.openHBox ppStrm;
122 : jhr 175 string "if"; sp(); ppVar(ppStrm, x);
123 : jhr 172 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 : jhr 175 | S.S_New(actor, args) => (
134 : jhr 172 PP.openHBox ppStrm;
135 :     string "new"; sp(); string(Atom.toString actor); sp();
136 :     ppArgs (ppStrm, args); string ";";
137 :     PP.closeBox ppStrm;
138 :     nl())
139 : jhr 175 | S.S_Die => (string "die;"; nl())
140 :     | S.S_Stabilize => (string "stabilize;"; nl())
141 : jhr 172 (* end case *))
142 :     in
143 :     PP.openVBox ppStrm (PP.Abs 0);
144 :     string "{"; nl();
145 :     PP.openVBox ppStrm indent;
146 : jhr 175 List.app (fn vdcl => (ppVarDecl ppStrm vdcl; nl())) vars;
147 : jhr 172 List.app ppStmt stms;
148 :     PP.closeBox ppStrm;
149 :     string "}"; nl();
150 :     PP.closeBox ppStrm
151 :     end
152 :    
153 : jhr 175 fun ppVarsAndStmt (ppStrm, vars, S.S_Block stms) = ppBlock(ppStrm, vars, stms)
154 :     | ppVarsAndStmt (ppStrm, vars, stm) = ppBlock(ppStrm, vars, [stm])
155 :    
156 :     fun ppActor ppStrm (S.Actor{name, params, state, stateInit, methods}) = let
157 : jhr 172 fun sp () = PP.space ppStrm 1
158 :     fun nl () = PP.newline ppStrm
159 :     val string = PP.string ppStrm
160 :     fun var x = string(Var.nameOf x)
161 : jhr 175 fun ppMethod (S.M_Method(name, body)) = (
162 :     nl(); string(Atom.toString name); nl(); ppVarsAndStmt (ppStrm, [], body))
163 : jhr 172 in
164 :     PP.openHBox ppStrm;
165 :     string "actor"; sp(); string(Atom.toString name); sp();
166 :     ppList (fn (_, x) => (string(TU.toString(#2(Var.typeOf x))); sp(); var x))
167 :     ("(", ",", ")") (ppStrm, params);
168 :     PP.closeBox ppStrm;
169 :     nl();
170 :     PP.openVBox ppStrm indent;
171 :     string "{";
172 : jhr 175 ppVarsAndStmt (ppStrm, state, stateInit);
173 : jhr 172 List.app ppMethod methods;
174 :     PP.closeBox ppStrm;
175 :     nl();
176 :     string "}"; nl()
177 :     end
178 :    
179 :     fun output (outS, S.Program{globals, globalInit, actors}) = let
180 :     val ppStrm = PP.openOut {dst = outS, wid = 120}
181 :     in
182 :     PP.openVBox ppStrm (PP.Abs 0);
183 : jhr 175 PP.string ppStrm "/* Simplified Program start */"; PP.newline ppStrm;
184 :     ppVarsAndStmt (ppStrm, globals, globalInit);
185 :     List.app (ppActor ppStrm) actors;
186 : jhr 172 PP.string ppStrm "/* Program end */"; PP.newline ppStrm;
187 :     PP.closeBox ppStrm;
188 :     PP.closeStream ppStrm
189 :     end
190 :    
191 :     end

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