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 1151 - (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 :     *)
117 : jhr 1131 | IL.S_New(strand, args) =>
118 :     prln (out, [argsToString("new "^Atom.toString strand, args), ";\n"])
119 : jhr 1115 | IL.S_LoadImage(x, dim, exp) =>
120 :     prln (out, [
121 :     Var.name x, " = load<", Int.toString dim, "> (", expToString exp, ");\n"
122 :     ])
123 :     | IL.S_Input(x, name, NONE) =>
124 :     prln (out, [
125 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
126 :     String.toString name, "\");\n"
127 :     ])
128 :     | IL.S_Input(x, name, SOME dflt) =>
129 :     prln (out, [
130 :     Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"",
131 :     String.toString name, "\",", expToString dflt, ");\n"
132 :     ])
133 :     | IL.S_Exit es => prln (out, [argsToString("exit", es), ";\n"])
134 :     (* return functions for methods *)
135 :     | IL.S_Active es => prln (out, [argsToString("active", es), ";\n"])
136 :     | IL.S_Stabilize es => prln (out, [argsToString("stabilize", es), ";\n"])
137 :     | IL.S_Die => prln (out, ["die;\n"])
138 :     (* end case *))
139 :    
140 :     fun statement (outS, stm) = ppStm((outS, 0), stm)
141 :    
142 :     fun block (outS, blk) = (ppBlock ((outS, 0), blk); pr ((outS, 0), "\n"))
143 :    
144 : jhr 1151 fun ppInitially (out, {isArray, iterPrefix, iters, createPrefix, strand, args}) = let
145 :     fun ppBlock (out, IL.Block{locals, body}, inside) = let
146 :     val out' = incIndent out
147 :     in
148 :     pr (out, "{\n");
149 :     List.app (ppVarDecl out') locals;
150 :     List.app (fn stm => ppStm(out', stm)) body;
151 :     inside out';
152 :     indent out; pr (out, "}")
153 :     end
154 :     fun ppCreate out = let
155 :     fun ppNew out = prln(out, [argsToString("new "^Atom.toString strand, args), ";\n"])
156 :     in
157 :     indent out; ppBlock (out, createPrefix, ppNew); pr(out, "\n")
158 :     end
159 :     fun ppIters out = let
160 :     fun ppIter (out, []) = ppCreate out
161 :     | ppIter (out, (i, lo, hi)::iters) = (
162 :     prln(out, ["for ", Var.name i, " = ", expToString lo, " .. ", expToString hi, "\n"]);
163 :     ppIter (incIndent out, iters))
164 :     in
165 :     ppIter (out, iters)
166 :     end
167 :     in
168 :     indent out; pr(out, if isArray then "ARRAY " else "COLLECTION ");
169 :     ppBlock (out, iterPrefix, ppIters)
170 :     end
171 :    
172 : jhr 1131 fun program (outS, IL.Program{props, globals, globalInit, strands, initially}) = let
173 : jhr 1115 val out = (outS, 0)
174 :     val out' = incIndent out
175 :     in
176 : jhr 1131 pr(out, "//***** PROPERTIES *****\n");
177 :     case props
178 :     of [] => prln(out', ["none\n"])
179 :     | _ => prln(out', [String.concatWith " " (List.map IL.propToString props), "\n"])
180 :     (* end case *);
181 : jhr 1115 prln(out, ["//***** GLOBALS *****\n"]);
182 :     List.app (ppVarDecl out') globals;
183 :     prln(out, ["//***** GLOBAL INIT *****\n"]);
184 :     indent out'; ppBlock (out', globalInit); pr (out, "\n");
185 :     prln(out, ["//***** STRANDS *****\n"]);
186 :     List.app (ppStrand out) strands;
187 : jhr 1151 prln(out, ["//***** INITIALLY *****\n"]);
188 :     ppInitially (out', initially)
189 : jhr 1115 end
190 :    
191 :     end

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