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 2279 - (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 : jhr 2279 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 :     and toS (IL.E_State x, l) = IL.stateVarToString x :: l
43 : jhr 1640 | toS (IL.E_Var x, l) = (case (Var.kind x)
44 : jhr 2279 of IL.VK_Local => Var.name x :: l
45 :     | _ => "::" :: Var.name x :: l
46 :     (* end case *))
47 :     | 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 :     | toS (IL.E_Apply(f, args), l) = MathFuns.toString f :: argsToS ("(", args, ")", l)
50 :     | toS (IL.E_Cons(ty, args), l) =
51 :     "<" :: Ty.toString ty :: ">" :: argsToS ("{", args, "}", l)
52 :     in
53 :     String.concat (toS (e, []))
54 :     end
55 : jhr 1115
56 :     fun argsToString (prefix, es) = String.concat[
57 : jhr 2279 prefix, "(", String.concatWith "," (List.map expToString es), ")"
58 :     ]
59 : jhr 1115
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 : jhr 2279 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 :     fun ppSVarDecl (IL.SV{varying, output, name, ty, ...}) = let
70 :     val v = if varying then "varying " else ""
71 :     val out = if output then "output " else ""
72 :     in
73 :     prln (out', [v, out, Ty.toString ty, " ", name, ";\n"])
74 :     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 : jhr 1115
84 :     and ppMethod out (IL.Method{name, body}) = (
85 : jhr 2279 prln (out, [StrandUtil.nameToString name, " () "]);
86 :     ppBlock (out, body);
87 :     pr (out, "\n"))
88 : jhr 1115
89 :     and ppBlock (out, IL.Block{locals, body}) = let
90 : jhr 2279 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 : jhr 1115
98 :     and ppStm (out, stm) = (case stm
99 : jhr 2279 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 :     | 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 : jhr 1640 prln(out, ["(", Var.name x]);
108 :     List.app (fn x => prl(out, [",", Var.name x])) xs;
109 :     prl (out, [") = ", expToString e, ";\n"]))
110 : jhr 2279 | 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 : jhr 1115 (*
121 : jhr 2279 | 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 : jhr 1115 *)
129 : jhr 2279 | 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 : jhr 2279 | IL.S_Save(x::xs, e) => (
134 : jhr 1640 prln(out, ["(", IL.stateVarToString x]);
135 :     List.app (fn x => prl(out, [",", IL.stateVarToString x])) xs;
136 :     prl (out, [") = ", expToString e, ";\n"]))
137 : jhr 2279 | IL.S_LoadNrrd(x, ty, nrrd) =>
138 :     prln (out, [
139 :     Var.name x, " = load<", Ty.toString ty, "> (\"", String.toString nrrd, "\");\n"
140 :     ])
141 :     | IL.S_Input(x, name, desc, NONE) =>
142 :     prln (out, [
143 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
144 :     String.toString name, "\",\"", descToString desc, "\");\n"
145 :     ])
146 :     | IL.S_Input(x, name, desc, SOME dflt) =>
147 :     prln (out, [
148 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
149 :     String.toString name, "\",\"", descToString desc, "\",",
150 :     expToString dflt, ");\n"
151 :     ])
152 :     | IL.S_InputNrrd(x, name, desc, NONE) =>
153 :     prln (out, [
154 :     Var.name x, " = input-nrrd<", Ty.toString(Var.ty x), "> (\"",
155 :     String.toString name, "\",\"", descToString desc, "\");\n"
156 :     ])
157 :     | IL.S_InputNrrd(x, name, desc, SOME dflt) =>
158 :     prln (out, [
159 :     Var.name x, " = input-nrrd<", Ty.toString(Var.ty x), "> (\"",
160 :     String.toString name, "\",\"", descToString desc, "\",\"",
161 :     String.toString dflt, "\");\n"
162 :     ])
163 :     | IL.S_Exit es => prln (out, [argsToString("exit", es), ";\n"])
164 :     (* return functions for methods *)
165 :     | IL.S_Active => prln (out, ["active;\n"])
166 :     | IL.S_Stabilize => prln (out, ["stabilize;\n"])
167 :     | IL.S_Die => prln (out, ["die;\n"])
168 :     (* end case *))
169 : jhr 1115
170 :     fun statement (outS, stm) = ppStm((outS, 0), stm)
171 :    
172 :     fun block (outS, blk) = (ppBlock ((outS, 0), blk); pr ((outS, 0), "\n"))
173 :    
174 : jhr 1151 fun ppInitially (out, {isArray, iterPrefix, iters, createPrefix, strand, args}) = let
175 : jhr 2279 fun ppBlock (out, IL.Block{locals, body}, inside) = let
176 :     val out' = incIndent out
177 :     in
178 :     pr (out, "{\n");
179 :     List.app (ppVarDecl out') locals;
180 :     List.app (fn stm => ppStm(out', stm)) body;
181 :     inside out';
182 :     indent out; pr (out, "}")
183 :     end
184 :     fun ppCreate out = let
185 :     fun ppNew out = prln(out, [argsToString("new "^Atom.toString strand, args), ";\n"])
186 :     in
187 :     indent out; ppBlock (out, createPrefix, ppNew); pr(out, "\n")
188 :     end
189 :     fun ppIters out = let
190 :     fun ppIter (out, []) = ppCreate out
191 :     | ppIter (out, (i, lo, hi)::iters) = (
192 :     prln(out, ["for ", Var.name i, " = ", expToString lo, " .. ", expToString hi, "\n"]);
193 :     ppIter (incIndent out, iters))
194 :     in
195 :     ppIter (out, iters)
196 :     end
197 :     in
198 :     indent out; pr(out, if isArray then "ARRAY " else "COLLECTION ");
199 :     ppBlock (out, iterPrefix, ppIters);
200 :     pr (out, "\n")
201 :     end
202 : jhr 1151
203 : jhr 1301 fun program (outS, IL.Program{props, globals, inputInit, globalInit, strands, initially}) = let
204 : jhr 2279 val out = (outS, 0)
205 :     val out' = incIndent out
206 :     in
207 :     pr(out, "//***** PROPERTIES *****\n");
208 :     case props
209 :     of [] => prln(out', ["none\n"])
210 :     | _ => prln(out', [String.concatWith " " (List.map StrandUtil.propToString props), "\n"])
211 :     (* end case *);
212 :     prln(out, ["//***** GLOBALS *****\n"]);
213 :     List.app (ppVarDecl out') globals;
214 :     prln(out, ["//***** INPUT INIT *****\n"]);
215 :     indent out'; ppBlock (out', inputInit); pr (out, "\n");
216 :     prln(out, ["//***** GLOBAL INIT *****\n"]);
217 :     indent out'; ppBlock (out', globalInit); pr (out, "\n");
218 :     prln(out, ["//***** STRANDS *****\n"]);
219 :     List.app (ppStrand out) strands;
220 :     prln(out, ["//***** INITIALLY *****\n"]);
221 :     ppInitially (out', initially)
222 :     end
223 : jhr 1115
224 :     end

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