12 |
|
|
13 |
structure PP = TextIOPP |
structure PP = TextIOPP |
14 |
|
|
15 |
fun output (outS, prog) = let |
val indent = PP.Abs 2 |
16 |
|
|
17 |
|
fun ppList ppFn (left, sep, right) (ppStrm, list) = let |
18 |
|
fun sp () = PP.space ppStrm 1 |
19 |
|
val string = PP.string ppStrm |
20 |
|
fun pp [] = string right |
21 |
|
| pp [x] = (ppFn(ppStrm, x); string right) |
22 |
|
| pp (x::xs) = (ppFn(ppStrm, x); string sep; sp(); pp xs) |
23 |
|
in |
24 |
|
string left; pp list |
25 |
|
end |
26 |
|
|
27 |
|
fun ppExp (ppStrm, e) = let |
28 |
|
fun sp () = PP.space ppStrm 1 |
29 |
|
val string = PP.string ppStrm |
30 |
|
fun var x = string(Var.nameOf x) |
31 |
|
fun pp e = (case e |
32 |
|
of AST.E_Var(x, [], _) => var x |
33 |
|
(* FIXME: print type args *) |
34 |
|
| AST.E_Var(x, mvs, ty) => var x |
35 |
|
| AST.E_Lit lit => string (Literal.toString lit) |
36 |
|
| AST.E_Tuple es => ppArgs (ppStrm, es) |
37 |
|
| AST.E_Apply(f, [], args, _) => (var f; sp(); ppArgs (ppStrm, args)) |
38 |
|
| AST.E_Apply(f, mvs, args, _) => ( |
39 |
|
(* FIXME: print type args *) |
40 |
|
var f; sp(); ppArgs (ppStrm, args)) |
41 |
|
| AST.E_Cons es => ( |
42 |
|
ppList ppExp ("[", ",", "]") (ppStrm, es)) |
43 |
|
| AST.E_Cond(e1, e2, e3) => ( |
44 |
|
pp e1; sp(); string "?"; sp(); pp e2; sp(); string ":"; sp(); pp e3) |
45 |
|
(* end case *)) |
46 |
|
in |
47 |
|
pp e |
48 |
|
end |
49 |
|
|
50 |
|
and ppArgs (ppStrm, args) = ppList ppExp ("(", ",", ")") (ppStrm, args) |
51 |
|
|
52 |
|
fun ppVarDecl ppStrm (AST.VD_Decl(x, e)) = let |
53 |
|
fun sp () = PP.space ppStrm 1 |
54 |
|
val string = PP.string ppStrm |
55 |
|
fun var x = string(Var.nameOf x) |
56 |
|
in |
57 |
|
PP.openHBox ppStrm; |
58 |
|
string(TypeUtil.toString(#2(Var.typeOf x))); sp(); var x; |
59 |
|
sp(); string "="; sp(); ppExp(ppStrm, e); string ";"; |
60 |
|
PP.closeBox ppStrm |
61 |
|
end |
62 |
|
|
63 |
|
fun ppBlock (ppStrm, stms) = let |
64 |
|
fun sp () = PP.space ppStrm 1 |
65 |
|
fun nl () = PP.newline ppStrm |
66 |
|
val string = PP.string ppStrm |
67 |
|
fun var x = string(Var.nameOf x) |
68 |
|
fun ppStmt stmt = (case stmt |
69 |
|
of AST.S_Block stms => ppBlock (ppStrm, stms) |
70 |
|
| AST.S_Decl vdcl => (ppVarDecl ppStrm vdcl; nl()) |
71 |
|
| AST.S_IfThenElse(e, AST.S_Block stms, AST.S_Block[]) => ( |
72 |
|
PP.openHBox ppStrm; |
73 |
|
string "if"; sp(); ppExp(ppStrm, e); |
74 |
|
sp(); ppBlock (ppStrm, stms); |
75 |
|
PP.closeBox ppStrm) |
76 |
|
| AST.S_IfThenElse(e, s1, AST.S_Block[]) => ( |
77 |
|
PP.openVBox ppStrm indent; |
78 |
|
PP.openHBox ppStrm; |
79 |
|
string "if"; sp(); ppExp(ppStrm, e); |
80 |
|
PP.closeBox ppStrm; |
81 |
|
nl(); |
82 |
|
ppStmt s1; |
83 |
|
PP.closeBox ppStrm; |
84 |
|
nl()) |
85 |
|
| AST.S_IfThenElse(e, AST.S_Block stms1, AST.S_Block stms2) => ( |
86 |
|
PP.openHBox ppStrm; |
87 |
|
string "if"; sp(); ppExp(ppStrm, e); |
88 |
|
sp(); ppBlock (ppStrm, stms); |
89 |
|
PP.closeBox ppStrm; |
90 |
|
PP.openHBox ppStrm; |
91 |
|
string "else"; sp(); ppBlock (ppStrm, stms); |
92 |
|
PP.closeBox ppStrm) |
93 |
|
| AST.S_IfThenElse(e, AST.S_Block stms1, s2) => raise Fail "FIXME" |
94 |
|
| AST.S_IfThenElse(e, s1, AST.S_Block stms2) => raise Fail "FIXME" |
95 |
|
| AST.S_IfThenElse(e, s1, s2) => ( |
96 |
|
PP.openVBox ppStrm indent; |
97 |
|
PP.openHBox ppStrm; |
98 |
|
string "if"; sp(); ppExp(ppStrm, e); |
99 |
|
PP.closeBox ppStrm; |
100 |
|
nl(); |
101 |
|
ppStmt s1; |
102 |
|
PP.closeBox ppStrm; |
103 |
|
nl(); |
104 |
|
PP.openVBox ppStrm indent; |
105 |
|
string "else"; nl(); |
106 |
|
ppStmt s2; |
107 |
|
PP.closeBox ppStrm; |
108 |
|
nl()) |
109 |
|
| AST.S_Assign(x, e) => ( |
110 |
|
PP.openHBox ppStrm; |
111 |
|
var x; sp(); string "="; sp(); ppExp(ppStrm, e); string ";"; |
112 |
|
PP.closeBox ppStrm; |
113 |
|
nl()) |
114 |
|
| AST.S_New(actor, args) => ( |
115 |
|
PP.openHBox ppStrm; |
116 |
|
string "new"; sp(); string(Atom.toString actor); sp(); |
117 |
|
ppArgs (ppStrm, args); string ";"; |
118 |
|
PP.closeBox ppStrm; |
119 |
|
nl()) |
120 |
|
| AST.S_Die => (string "die;"; nl()) |
121 |
|
| AST.S_Stabilize => (string "stabilize;"; nl()) |
122 |
|
(* end case *)) |
123 |
|
in |
124 |
|
PP.openVBox ppStrm (PP.Abs 0); |
125 |
|
string "{"; nl(); |
126 |
|
PP.openVBox ppStrm indent; |
127 |
|
List.app ppStmt stms; |
128 |
|
PP.closeBox ppStrm; |
129 |
|
string "}"; nl(); |
130 |
|
PP.closeBox ppStrm |
131 |
|
end |
132 |
|
|
133 |
|
fun ppActor (ppStrm, {name, params, state, methods}) = let |
134 |
|
fun sp () = PP.space ppStrm 1 |
135 |
|
fun nl () = PP.newline ppStrm |
136 |
|
val string = PP.string ppStrm |
137 |
|
fun var x = string(Var.nameOf x) |
138 |
|
fun ppMethod (AST.M_Method(name, AST.S_Block stms)) = ( |
139 |
|
nl(); string(Atom.toString name); nl(); ppBlock (ppStrm, stms)) |
140 |
|
| ppMethod (AST.M_Method(name, stm)) = ( |
141 |
|
nl(); string(Atom.toString name); nl(); ppBlock (ppStrm, [stm])) |
142 |
|
in |
143 |
|
PP.openHBox ppStrm; |
144 |
|
string "actor"; sp(); string(Atom.toString name); sp(); |
145 |
|
ppList (fn (_, x) => (string(TypeUtil.toString(#2(Var.typeOf x))); sp(); var x)) |
146 |
|
("(", ",", ")") (ppStrm, params); |
147 |
|
PP.closeBox ppStrm; |
148 |
|
nl(); |
149 |
|
PP.openVBox ppStrm indent; |
150 |
|
string "{"; |
151 |
|
List.app (fn vdcl => (nl(); ppVarDecl ppStrm vdcl)) state; |
152 |
|
List.app ppMethod methods; |
153 |
|
PP.closeBox ppStrm; |
154 |
|
nl(); |
155 |
|
string "}"; nl() |
156 |
|
end |
157 |
|
|
158 |
|
fun ppDecl ppStrm = let |
159 |
|
fun sp () = PP.space ppStrm 1 |
160 |
|
fun nl () = PP.newline ppStrm |
161 |
|
val string = PP.string ppStrm |
162 |
|
fun var x = string(Var.nameOf x) |
163 |
|
in |
164 |
|
fn AST.D_Input(x, NONE) => ( |
165 |
|
PP.openHBox ppStrm; |
166 |
|
string "input"; sp(); |
167 |
|
string(TypeUtil.toString(#2(Var.typeOf x))); sp(); var x; string ";"; |
168 |
|
PP.closeBox ppStrm; |
169 |
|
nl()) |
170 |
|
| AST.D_Input(x, SOME e) => ( |
171 |
|
PP.openHBox ppStrm; |
172 |
|
string "input"; sp(); |
173 |
|
string(TypeUtil.toString(#2(Var.typeOf x))); sp(); var x; |
174 |
|
sp(); string "="; sp(); ppExp(ppStrm, e); string ";"; |
175 |
|
PP.closeBox ppStrm; |
176 |
|
nl()) |
177 |
|
| AST.D_Var vdcl => (ppVarDecl ppStrm vdcl; nl()) |
178 |
|
| AST.D_Actor def => ppActor (ppStrm, def) |
179 |
|
| AST.D_InitialArray(create, iters) => (* FIXME *) () |
180 |
|
| AST.D_InitialCollection(create, iters) => (* FIXME *) () |
181 |
|
end |
182 |
|
|
183 |
|
fun output (outS, AST.Program decls) = let |
184 |
val ppStrm = PP.openOut {dst = outS, wid = 120} |
val ppStrm = PP.openOut {dst = outS, wid = 120} |
185 |
in |
in |
186 |
|
PP.openVBox ppStrm (PP.Abs 0); |
187 |
|
PP.string ppStrm "/* Program start */"; PP.newline ppStrm; |
188 |
|
List.app (ppDecl ppStrm) decls; |
189 |
|
PP.string ppStrm "/* Program end */"; PP.newline ppStrm; |
190 |
|
PP.closeBox ppStrm; |
191 |
PP.closeStream ppStrm |
PP.closeStream ppStrm |
192 |
end |
end |
193 |
|
|