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

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