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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1115 - (view) (download)
Original Path: trunk/src/compiler/tree-il/tree-il-pp.sml

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 :     fun expToString e = let
32 :     fun argsToS (lp, args, rp, l) = let
33 :     fun argToS ([], l) = l
34 :     | argToS ([e], l) = toS (e, l)
35 :     | argToS (e::es, l) = toS(e, "," :: argToS(es, l))
36 :     in
37 :     lp :: argToS(args, rp :: l)
38 :     end
39 :     and toS (IL.E_Var x, l) = Var.name x :: l
40 :     | toS (IL.E_Lit lit, l) = Literal.toString lit :: l
41 :     | toS (IL.E_Op(rator, args), l) = Op.toString rator :: argsToS ("(", args, ")", l)
42 :     | toS (IL.E_Apply(f, args), l) = ILBasis.toString f :: argsToS ("(", args, ")", l)
43 :     | toS (IL.E_Cons(ty, args), l) =
44 :     "<" :: Ty.toString ty :: ">" :: argsToS ("{", args, "}", l)
45 :     in
46 :     String.concat (toS (e, []))
47 :     end
48 :    
49 :     fun argsToString (prefix, es) = String.concat[
50 :     prefix, "(", String.concatWith "," (List.map expToString es), ")"
51 :     ]
52 :    
53 :     fun ppVarDecl out x = prln (out, [Ty.toString(Var.ty x), " ", Var.name x, ";\n"])
54 :    
55 :     fun ppStrand out (IL.Strand{name, params, state, stateInit, methods}) = let
56 :     val out' = incIndent out
57 :     fun ppParams [] = ()
58 :     | ppParams [x] = prl(out, [Ty.toString (Var.ty x), " ", Var.name x])
59 :     | ppParams (x::r) = (
60 :     prl(out, [Ty.toString (Var.ty x), " ", Var.name x, ","]);
61 :     ppParams r)
62 :     fun ppSVarDecl (IL.SV{varying, output, var}) = let
63 :     val v = if varying then "varying " else ""
64 :     val out = if output then "output " else ""
65 :     in
66 :     prln (out', [v, out, Ty.toString(Var.ty var), " ", Var.name var, ";\n"])
67 :     end
68 :     in
69 :     prln (out, ["strand ", Atom.toString name, " ("]);
70 :     ppParams params; pr(out, ") {\n");
71 :     List.app ppSVarDecl state;
72 :     prln (out', ["init () "]); ppBlock(out', stateInit); pr (out', "\n");
73 :     List.app (ppMethod out') methods;
74 :     prln (out, ["}\n"])
75 :     end
76 :    
77 :     and ppMethod out (IL.Method{name, body}) = (
78 :     prln (out, [Atom.toString name, " () "]);
79 :     ppBlock (out, body);
80 :     pr (out, "\n"))
81 :    
82 :     and ppBlock (out, IL.Block{locals, body}) = let
83 :     val out' = incIndent out
84 :     in
85 :     pr (out, "{\n");
86 :     List.app (ppVarDecl out') locals;
87 :     List.app (fn stm => ppStm(out', stm)) body;
88 :     indent out; pr (out, "}")
89 :     end
90 :    
91 :     and ppStm (out, stm) = (case stm
92 :     of IL.S_Comment text => let
93 :     val out = decIndent out
94 :     in
95 :     List.app (fn s => prln(out, ["// ", s, "\n"])) text
96 :     end
97 :     | IL.S_Assign(x, e) => prln(out, [Var.name x, " = ", expToString e, ";\n"])
98 :     | IL.S_IfThen(cond, blk) => (
99 :     prln (out, ["if (", expToString cond, ") "]);
100 :     ppBlock (out, blk);
101 :     pr (out, "\n"))
102 :     | IL.S_IfThenElse(cond, blk1, blk2) => (
103 :     prln (out, ["if (", expToString cond, ") "]);
104 :     ppBlock (out, blk1);
105 :     pr (out, " else ");
106 :     ppBlock (out, blk2);
107 :     pr (out, "\n"))
108 :     (*
109 :     | IL.S_For(x, e1, e2, blk) => (
110 :     prln (out, [
111 :     "for (", Ty.toString(Var.ty x), " ", Var.name x, " = ",
112 :     expToString e1, " ..", expToString e2, ") "
113 :     ]);
114 :     ppBlock (out, blk);
115 :     pr (out, "\n"))
116 :     | IL.S_Cons(x, args) => let
117 :     fun mkStrings [] = []
118 :     | mkStrings [e] = [expToString e]
119 :     | mkStrings (e::es) = (expToString e ^ ",") :: mkStrings es
120 :     val args = mkStrings args
121 :     in
122 :     if (List.foldl (fn (s, n) => n+size s) 0 args > 60)
123 :     then let
124 :     val out' = incIndent out
125 :     val out'' = incIndent out'
126 :     fun prArgs (_, []) = (
127 :     pr (out', "\n");
128 :     prln (out', ["};\n"]))
129 :     | prArgs (n, arg::args) =
130 :     if (n = 0)
131 :     then (
132 :     prln (out'', [arg]);
133 :     prArgs (n + size arg, args))
134 :     else if (n + size arg > 60)
135 :     then (
136 :     pr(out'', "\n");
137 :     prArgs (0, arg::args))
138 :     else (
139 :     pr(out'', " ");
140 :     pr(out'', arg);
141 :     prArgs (n + size arg + 1, args))
142 :     in
143 :     prln (out, [Var.name x, " = {\n"]);
144 :     prArgs (0, args)
145 :     end
146 :     else (
147 :     prln (out, Var.name x :: " = {" :: args);
148 :     pr (out, "};\n"))
149 :     end
150 :     | IL.S_LoadVoxels(x, dim, exp) =>
151 :     prln (out, [
152 :     Var.name x, " = loadVoxels<",
153 :     Int.toString dim, "> (", expToString exp, ");\n"
154 :     ])
155 :     *)
156 :     | IL.S_LoadImage(x, dim, exp) =>
157 :     prln (out, [
158 :     Var.name x, " = load<", Int.toString dim, "> (", expToString exp, ");\n"
159 :     ])
160 :     | IL.S_Input(x, name, NONE) =>
161 :     prln (out, [
162 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
163 :     String.toString name, "\");\n"
164 :     ])
165 :     | IL.S_Input(x, name, SOME dflt) =>
166 :     prln (out, [
167 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
168 :     String.toString name, "\",", expToString dflt, ");\n"
169 :     ])
170 :     | IL.S_Exit es => prln (out, [argsToString("exit", es), ";\n"])
171 :     (* return functions for methods *)
172 :     | IL.S_Active es => prln (out, [argsToString("active", es), ";\n"])
173 :     | IL.S_Stabilize es => prln (out, [argsToString("stabilize", es), ";\n"])
174 :     | IL.S_Die => prln (out, ["die;\n"])
175 :     (* end case *))
176 :    
177 :     fun statement (outS, stm) = ppStm((outS, 0), stm)
178 :    
179 :     fun block (outS, blk) = (ppBlock ((outS, 0), blk); pr ((outS, 0), "\n"))
180 :    
181 :     fun program (outS, IL.Program{globals, globalInit, strands, initially}) = let
182 :     val out = (outS, 0)
183 :     val out' = incIndent out
184 :     in
185 :     prln(out, ["//***** GLOBALS *****\n"]);
186 :     List.app (ppVarDecl out') globals;
187 :     prln(out, ["//***** GLOBAL INIT *****\n"]);
188 :     indent out'; ppBlock (out', globalInit); pr (out, "\n");
189 :     prln(out, ["//***** STRANDS *****\n"]);
190 :     List.app (ppStrand out) strands;
191 :     prln(out, ["//***** INITIALLY *****\n"])
192 :     (* FIXME: print initially stuff *)
193 :     end
194 :    
195 :     end

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