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

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/tree-il/tree-il-pp.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/tree-il/tree-il-pp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2023 - (view) (download)

1 : jhr 1115 (* tree-il-pp.sml
2 :     *
3 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * Printing for the TreeIL
7 :     *)
8 :    
9 :     structure TreeILPP : sig
10 :    
11 :     val statement : TextIO.outstream * TreeIL.stm -> unit
12 :    
13 :     val block : TextIO.outstream * TreeIL.block -> unit
14 :    
15 :     val program : TextIO.outstream * TreeIL.program -> unit
16 :    
17 :     end = struct
18 :    
19 :     structure IL = TreeIL
20 :     structure Op = IL.Op
21 :     structure Var = IL.Var
22 :     structure Ty = IL.Ty
23 :    
24 :     fun indent (outS, i) = TextIO.output(outS, StringCvt.padLeft #" " i "")
25 :     fun incIndent (outS, i) = (outS, i+2)
26 :     fun decIndent (outS, i) = (outS, Int.max(0, i-2))
27 :     fun pr ((outS, _), s) = TextIO.output(outS, s)
28 :     fun prl (out, l) = pr(out, concat l)
29 :     fun prln (out, l) = (indent out; prl(out, l))
30 :    
31 : jhr 2012 fun descToString NONE = ""
32 :     | descToString (SOME desc) = String.toString desc
33 :    
34 : jhr 1115 fun expToString e = let
35 :     fun argsToS (lp, args, rp, l) = let
36 :     fun argToS ([], l) = l
37 :     | argToS ([e], l) = toS (e, l)
38 :     | argToS (e::es, l) = toS(e, "," :: argToS(es, l))
39 :     in
40 :     lp :: argToS(args, rp :: l)
41 :     end
42 : jhr 1640 and toS (IL.E_State x, l) = IL.stateVarToString x :: l
43 :     | toS (IL.E_Var x, l) = (case (Var.kind x)
44 : jhr 1706 of IL.VK_Local => Var.name x :: l
45 :     | _ => "::" :: Var.name x :: l
46 : jhr 1380 (* end case *))
47 : jhr 1115 | toS (IL.E_Lit lit, l) = Literal.toString lit :: l
48 :     | toS (IL.E_Op(rator, args), l) = Op.toString rator :: argsToS ("(", args, ")", l)
49 : jhr 1922 | toS (IL.E_Apply(f, args), l) = MathFuns.toString f :: argsToS ("(", args, ")", l)
50 : jhr 1115 | toS (IL.E_Cons(ty, args), l) =
51 :     "<" :: Ty.toString ty :: ">" :: argsToS ("{", args, "}", l)
52 :     in
53 :     String.concat (toS (e, []))
54 :     end
55 :    
56 :     fun argsToString (prefix, es) = String.concat[
57 :     prefix, "(", String.concatWith "," (List.map expToString es), ")"
58 :     ]
59 :    
60 :     fun ppVarDecl out x = prln (out, [Ty.toString(Var.ty x), " ", Var.name x, ";\n"])
61 :    
62 :     fun ppStrand out (IL.Strand{name, params, state, stateInit, methods}) = let
63 :     val out' = incIndent out
64 :     fun ppParams [] = ()
65 :     | ppParams [x] = prl(out, [Ty.toString (Var.ty x), " ", Var.name x])
66 :     | ppParams (x::r) = (
67 :     prl(out, [Ty.toString (Var.ty x), " ", Var.name x, ","]);
68 :     ppParams r)
69 : jhr 1640 fun ppSVarDecl (IL.SV{varying, output, name, ty, ...}) = let
70 : jhr 1115 val v = if varying then "varying " else ""
71 :     val out = if output then "output " else ""
72 :     in
73 : jhr 1640 prln (out', [v, out, Ty.toString ty, " ", name, ";\n"])
74 : jhr 1115 end
75 :     in
76 :     prln (out, ["strand ", Atom.toString name, " ("]);
77 :     ppParams params; pr(out, ") {\n");
78 :     List.app ppSVarDecl state;
79 :     prln (out', ["init () "]); ppBlock(out', stateInit); pr (out', "\n");
80 :     List.app (ppMethod out') methods;
81 :     prln (out, ["}\n"])
82 :     end
83 :    
84 :     and ppMethod out (IL.Method{name, body}) = (
85 : jhr 1640 prln (out, [StrandUtil.nameToString name, " () "]);
86 : jhr 1115 ppBlock (out, body);
87 :     pr (out, "\n"))
88 :    
89 :     and ppBlock (out, IL.Block{locals, body}) = let
90 :     val out' = incIndent out
91 :     in
92 :     pr (out, "{\n");
93 :     List.app (ppVarDecl out') locals;
94 :     List.app (fn stm => ppStm(out', stm)) body;
95 :     indent out; pr (out, "}")
96 :     end
97 :    
98 :     and ppStm (out, stm) = (case stm
99 :     of IL.S_Comment text => let
100 :     val out = decIndent out
101 :     in
102 :     List.app (fn s => prln(out, ["// ", s, "\n"])) text
103 :     end
104 : jhr 1640 | IL.S_Assign([], e) => prln(out, [expToString e, ";\n"])
105 :     | IL.S_Assign([x], e) => prln(out, [Var.name x, " = ", expToString e, ";\n"])
106 :     | IL.S_Assign(x::xs, e) => (
107 :     prln(out, ["(", Var.name x]);
108 :     List.app (fn x => prl(out, [",", Var.name x])) xs;
109 :     prl (out, [") = ", expToString e, ";\n"]))
110 : jhr 1115 | IL.S_IfThen(cond, blk) => (
111 :     prln (out, ["if (", expToString cond, ") "]);
112 :     ppBlock (out, blk);
113 :     pr (out, "\n"))
114 :     | IL.S_IfThenElse(cond, blk1, blk2) => (
115 :     prln (out, ["if (", expToString cond, ") "]);
116 :     ppBlock (out, blk1);
117 :     pr (out, " else ");
118 :     ppBlock (out, blk2);
119 :     pr (out, "\n"))
120 :     (*
121 :     | IL.S_For(x, e1, e2, blk) => (
122 :     prln (out, [
123 :     "for (", Ty.toString(Var.ty x), " ", Var.name x, " = ",
124 :     expToString e1, " ..", expToString e2, ") "
125 :     ]);
126 :     ppBlock (out, blk);
127 :     pr (out, "\n"))
128 :     *)
129 : jhr 1131 | IL.S_New(strand, args) =>
130 :     prln (out, [argsToString("new "^Atom.toString strand, args), ";\n"])
131 : jhr 1640 | IL.S_Save([x], rhs) =>
132 :     prln (out, [IL.stateVarToString x, " = ", expToString rhs, ";\n"])
133 :     | IL.S_Save(x::xs, e) => (
134 :     prln(out, ["(", IL.stateVarToString x]);
135 :     List.app (fn x => prl(out, [",", IL.stateVarToString x])) xs;
136 :     prl (out, [") = ", expToString e, ";\n"]))
137 : jhr 2004 (* DEPRECATED
138 : jhr 1115 | IL.S_LoadImage(x, dim, exp) =>
139 :     prln (out, [
140 :     Var.name x, " = load<", Int.toString dim, "> (", expToString exp, ");\n"
141 :     ])
142 : jhr 2004 *)
143 : jhr 1301 | IL.S_Input(x, name, desc, NONE) =>
144 : jhr 1115 prln (out, [
145 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
146 : jhr 2012 String.toString name, "\",\"", descToString desc, "\");\n"
147 : jhr 1115 ])
148 : jhr 1301 | IL.S_Input(x, name, desc, SOME dflt) =>
149 : jhr 1115 prln (out, [
150 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
151 : jhr 2012 String.toString name, "\",\"", descToString desc, "\",",
152 : jhr 1301 expToString dflt, ");\n"
153 : jhr 1115 ])
154 : jhr 2023 | IL.S_InputNrrd(x, name, desc, NONE) =>
155 : jhr 2004 prln (out, [
156 : jhr 2023 Var.name x, " = input-nrrd<", Ty.toString(Var.ty x), "> (\"",
157 : jhr 2012 String.toString name, "\",\"", descToString desc, "\");\n"
158 : jhr 2004 ])
159 : jhr 2023 | IL.S_InputNrrd(x, name, desc, SOME dflt) =>
160 : jhr 2004 prln (out, [
161 : jhr 2023 Var.name x, " = input-nrrd<", Ty.toString(Var.ty x), "> (\"",
162 : jhr 2012 String.toString name, "\",\"", descToString desc, "\",\"",
163 : jhr 2004 String.toString dflt, "\");\n"
164 :     ])
165 : jhr 1115 | IL.S_Exit es => prln (out, [argsToString("exit", es), ";\n"])
166 :     (* return functions for methods *)
167 : jhr 1640 | IL.S_Active => prln (out, ["active;\n"])
168 :     | IL.S_Stabilize => prln (out, ["stabilize;\n"])
169 : jhr 1115 | IL.S_Die => prln (out, ["die;\n"])
170 :     (* end case *))
171 :    
172 :     fun statement (outS, stm) = ppStm((outS, 0), stm)
173 :    
174 :     fun block (outS, blk) = (ppBlock ((outS, 0), blk); pr ((outS, 0), "\n"))
175 :    
176 : jhr 1151 fun ppInitially (out, {isArray, iterPrefix, iters, createPrefix, strand, args}) = let
177 :     fun ppBlock (out, IL.Block{locals, body}, inside) = let
178 :     val out' = incIndent out
179 :     in
180 :     pr (out, "{\n");
181 :     List.app (ppVarDecl out') locals;
182 :     List.app (fn stm => ppStm(out', stm)) body;
183 :     inside out';
184 :     indent out; pr (out, "}")
185 :     end
186 :     fun ppCreate out = let
187 :     fun ppNew out = prln(out, [argsToString("new "^Atom.toString strand, args), ";\n"])
188 :     in
189 :     indent out; ppBlock (out, createPrefix, ppNew); pr(out, "\n")
190 :     end
191 :     fun ppIters out = let
192 :     fun ppIter (out, []) = ppCreate out
193 :     | ppIter (out, (i, lo, hi)::iters) = (
194 :     prln(out, ["for ", Var.name i, " = ", expToString lo, " .. ", expToString hi, "\n"]);
195 :     ppIter (incIndent out, iters))
196 :     in
197 :     ppIter (out, iters)
198 :     end
199 :     in
200 :     indent out; pr(out, if isArray then "ARRAY " else "COLLECTION ");
201 : jhr 1232 ppBlock (out, iterPrefix, ppIters);
202 :     pr (out, "\n")
203 : jhr 1151 end
204 :    
205 : jhr 1301 fun program (outS, IL.Program{props, globals, inputInit, globalInit, strands, initially}) = let
206 : jhr 1115 val out = (outS, 0)
207 :     val out' = incIndent out
208 :     in
209 : jhr 1131 pr(out, "//***** PROPERTIES *****\n");
210 :     case props
211 :     of [] => prln(out', ["none\n"])
212 : jhr 1640 | _ => prln(out', [String.concatWith " " (List.map StrandUtil.propToString props), "\n"])
213 : jhr 1131 (* end case *);
214 : jhr 1115 prln(out, ["//***** GLOBALS *****\n"]);
215 :     List.app (ppVarDecl out') globals;
216 : jhr 1301 prln(out, ["//***** INPUT INIT *****\n"]);
217 :     indent out'; ppBlock (out', inputInit); pr (out, "\n");
218 : jhr 1115 prln(out, ["//***** GLOBAL INIT *****\n"]);
219 :     indent out'; ppBlock (out', globalInit); pr (out, "\n");
220 :     prln(out, ["//***** STRANDS *****\n"]);
221 :     List.app (ppStrand out) strands;
222 : jhr 1151 prln(out, ["//***** INITIALLY *****\n"]);
223 :     ppInitially (out', initially)
224 : jhr 1115 end
225 :    
226 :     end

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