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 172 - (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 :     fun ppVar ppStrm x = PP.string ppStrm (Var.uniqueNameOf x)
47 :    
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 :     fun ppBlock (ppStrm, stms) = let
84 :     fun sp () = PP.space ppStrm 1
85 :     fun nl () = PP.newline ppStrm
86 :     val string = PP.string ppStrm
87 :     fun var x = string(Var.nameOf x)
88 :     fun ppStmt stmt = (case stmt
89 :     of AST.S_Block stms => ppBlock (ppStrm, stms)
90 :     | AST.S_Decl vdcl => (ppVarDecl ppStrm vdcl; nl())
91 :     | AST.S_IfThenElse(e, AST.S_Block stms, AST.S_Block[]) => (
92 :     PP.openHBox ppStrm;
93 :     string "if"; sp(); ppExp(ppStrm, e);
94 :     sp(); ppBlock (ppStrm, stms);
95 :     PP.closeBox ppStrm)
96 :     | AST.S_IfThenElse(e, s1, AST.S_Block[]) => (
97 :     PP.openVBox ppStrm indent;
98 :     PP.openHBox ppStrm;
99 :     string "if"; sp(); ppExp(ppStrm, e);
100 :     PP.closeBox ppStrm;
101 :     nl();
102 :     ppStmt s1;
103 :     PP.closeBox ppStrm;
104 :     nl())
105 :     | AST.S_IfThenElse(e, AST.S_Block stms1, AST.S_Block stms2) => (
106 :     PP.openHBox ppStrm;
107 :     string "if"; sp(); ppExp(ppStrm, e);
108 :     sp(); ppBlock (ppStrm, stms1);
109 :     PP.closeBox ppStrm;
110 :     PP.openHBox ppStrm;
111 :     string "else"; sp(); ppBlock (ppStrm, stms2);
112 :     PP.closeBox ppStrm)
113 :     | AST.S_IfThenElse(e, AST.S_Block stms1, s2) => raise Fail "FIXME"
114 :     | AST.S_IfThenElse(e, s1, AST.S_Block stms2) => raise Fail "FIXME"
115 :     | AST.S_IfThenElse(e, s1, s2) => (
116 :     PP.openVBox ppStrm indent;
117 :     PP.openHBox ppStrm;
118 :     string "if"; sp(); ppExp(ppStrm, e);
119 :     PP.closeBox ppStrm;
120 :     nl();
121 :     ppStmt s1;
122 :     PP.closeBox ppStrm;
123 :     nl();
124 :     PP.openVBox ppStrm indent;
125 :     string "else"; nl();
126 :     ppStmt s2;
127 :     PP.closeBox ppStrm;
128 :     nl())
129 :     | AST.S_Assign(x, e) => (
130 :     PP.openHBox ppStrm;
131 :     var x; sp(); string "="; sp(); ppExp(ppStrm, e); string ";";
132 :     PP.closeBox ppStrm;
133 :     nl())
134 :     | AST.S_New(actor, args) => (
135 :     PP.openHBox ppStrm;
136 :     string "new"; sp(); string(Atom.toString actor); sp();
137 :     ppArgs (ppStrm, args); string ";";
138 :     PP.closeBox ppStrm;
139 :     nl())
140 :     | AST.S_Die => (string "die;"; nl())
141 :     | AST.S_Stabilize => (string "stabilize;"; nl())
142 :     (* end case *))
143 :     in
144 :     PP.openVBox ppStrm (PP.Abs 0);
145 :     string "{"; nl();
146 :     PP.openVBox ppStrm indent;
147 :     List.app ppStmt stms;
148 :     PP.closeBox ppStrm;
149 :     string "}"; nl();
150 :     PP.closeBox ppStrm
151 :     end
152 :    
153 :     fun ppActor (ppStrm, S.Actor{name, params, state, stateInit, methods}) = let
154 :     fun sp () = PP.space ppStrm 1
155 :     fun nl () = PP.newline ppStrm
156 :     val string = PP.string ppStrm
157 :     fun var x = string(Var.nameOf x)
158 :     fun ppMethod (AST.M_Method(name, AST.S_Block stms)) = (
159 :     nl(); string(Atom.toString name); nl(); ppBlock (ppStrm, stms))
160 :     | ppMethod (AST.M_Method(name, stm)) = (
161 :     nl(); string(Atom.toString name); nl(); ppBlock (ppStrm, [stm]))
162 :     in
163 :     PP.openHBox ppStrm;
164 :     string "actor"; sp(); string(Atom.toString name); sp();
165 :     ppList (fn (_, x) => (string(TU.toString(#2(Var.typeOf x))); sp(); var x))
166 :     ("(", ",", ")") (ppStrm, params);
167 :     PP.closeBox ppStrm;
168 :     nl();
169 :     PP.openVBox ppStrm indent;
170 :     string "{";
171 :     List.app (fn vdcl => (nl(); ppStateVarDecl ppStrm vdcl)) state;
172 :     List.app ppMethod methods;
173 :     PP.closeBox ppStrm;
174 :     nl();
175 :     string "}"; nl()
176 :     end
177 :    
178 :     fun output (outS, S.Program{globals, globalInit, actors}) = let
179 :     val ppStrm = PP.openOut {dst = outS, wid = 120}
180 :     in
181 :     PP.openVBox ppStrm (PP.Abs 0);
182 :     PP.string ppStrm "/* Program start */"; PP.newline ppStrm;
183 :     List.app (ppDecl ppStrm) decls;
184 :     PP.string ppStrm "/* Program end */"; PP.newline ppStrm;
185 :     PP.closeBox ppStrm;
186 :     PP.closeStream ppStrm
187 :     end
188 :    
189 :     end

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