-
Notifications
You must be signed in to change notification settings - Fork 0
/
tigerpp.sml
executable file
·220 lines (217 loc) · 7.19 KB
/
tigerpp.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
(*
structure tigerpp :> tigerpp =
struct
*)
open tigerabs
open PP
fun ppexpr pps e0 =
let
fun ppf{name,escape,typ} =
(add_string pps ("{name="^name^",");
add_break pps (0, 1);
add_string pps "escape=";
add_string pps (Bool.toString(!escape));
add_string pps "typ=";
ppt typ;
add_string pps "}"; add_break pps (0, 1))
and ppd(FunctionDec flist) =
(begin_block pps INCONSISTENT 0;
add_string pps "FunctionDec([";
List.app
(fn({name,params,result,body}, _)=>
(add_string pps ("{name="^name^",");
add_break pps (0, 0);
add_string pps "params=[";
List.app ppf params;
add_string pps "],"; add_break pps (0, 1);
add_string pps ("result="^
(case result of SOME s => s | _ => "NONE"));
add_break pps (0, 0);
add_string pps "body="; ppe body;
add_string pps "}"; add_break pps (0, 0)))
flist;
add_string pps "])"; add_break pps (0, 0);
end_block pps)
| ppd(VarDec({name, escape, typ, init}, _)) =
(begin_block pps INCONSISTENT 0;
add_string pps ("VarDec{name="^name^",");
add_break pps (0, 1);
add_string pps "escape=";
add_string pps (Bool.toString(!escape));
add_string pps ","; add_break pps (0, 1);
add_string pps ("typ="^
(case typ of SOME s => s | _ => "NONE"));
add_string pps ","; add_break pps (0, 1);
add_string pps "init="; ppe init;
add_string pps "}"; add_break pps (0, 0);
end_block pps)
| ppd(TypeDec tlist) =
(begin_block pps INCONSISTENT 0;
add_string pps "TypeDec([";
List.app
(fn({name: symbol, ty: ty}, _)=>
(add_string pps ("{name="^name^","); add_break pps (0, 1);
add_string pps "ty=";
ppt ty; add_string pps "}";
add_break pps (0, 1)))
tlist;
add_string pps "])"; add_break pps (0, 0);
end_block pps)
and ppt(NameTy s) =
(begin_block pps INCONSISTENT 0;
add_string pps ("NameTy("^s^")"); add_break pps (0, 0);
end_block pps)
| ppt(RecordTy fieldlist) =
(begin_block pps INCONSISTENT 0;
add_string pps "RecordTy([";
List.app ppf fieldlist;
add_string pps "])";
end_block pps)
| ppt(ArrayTy s) =
(begin_block pps INCONSISTENT 0;
add_string pps ("ArrayTy("^s^")"); add_break pps (0, 1);
end_block pps)
and ppv(SimpleVar s) =
(begin_block pps INCONSISTENT 0;
add_string pps "SimpleVar("; add_break pps (0, 1);
add_string pps (s^")"); add_break pps (0, 0);
end_block pps)
| ppv(FieldVar(v, s)) =
(begin_block pps INCONSISTENT 0;
add_string pps "FieldVar(";
ppv v; add_break pps (0, 1);
add_string pps (","^s^")");
end_block pps)
| ppv(SubscriptVar(v, e)) =
(begin_block pps INCONSISTENT 0;
add_string pps "SubscriptVar("; add_break pps (0, 1);
ppv v; add_string pps ","; add_break pps (0, 1);
ppe e; add_string pps ")"; add_break pps (0, 0);
end_block pps)
and ppe(UnitExp _) = add_string pps "UnitExp"
| ppe(NilExp _) = add_string pps "NilExp"
| ppe(IntExp(n, _)) = add_string pps (Int.toString n)
| ppe(StringExp(s, _)) = add_string pps ("\""^s^"\"")
| ppe(BreakExp _) = add_string pps "BreakExp"
| ppe(VarExp(v, _)) =
(begin_block pps INCONSISTENT 0;
add_string pps "VarExp("; add_break pps (0, 1);
ppv v; add_break pps (0, 0);
add_string pps ")"; add_break pps (0, 0);
end_block pps)
| ppe(OpExp({left, oper, right}, _)) =
(begin_block pps INCONSISTENT 0;
add_string pps "OpExp{"; add_break pps (0, 0);
add_string pps "left=";
ppe left; add_string pps ","; add_break pps (0, 1);
add_string pps "oper=";
add_string pps
(case oper of
PlusOp => "PlusOp" | MinusOp => "MinusOp"
| TimesOp => "TimesOp" | DivideOp => "DivideOp"
| EqOp => "EqOp" | NeqOp => "NeqOp"
| LtOp => "LtOp" | LeOp => "LeOp"
| GtOp => "GtOp" | GeOp => "GeOp");
add_string pps ","; add_break pps (0, 1);
add_string pps "right="; ppe right; add_string pps "}";
end_block pps)
| ppe(WhileExp({test, body}, _)) =
(begin_block pps INCONSISTENT 0;
add_string pps "WhileExp{test=";
ppe test; add_string pps ","; add_break pps (0, 0);
add_string pps "body=";
add_string pps "body="; ppe body; add_string pps "}";
end_block pps)
| ppe(ForExp({var, escape, lo, hi, body}, _)) =
(begin_block pps INCONSISTENT 0;
add_string pps "ForExp{var=";
add_string pps var; add_string pps ","; add_break pps (0, 0);
add_string pps "escape=";
add_string pps (Bool.toString(!escape));
add_string pps ","; add_break pps (0, 0);
add_string pps "lo="; ppe lo; add_string pps ",";
add_break pps (0, 0);
add_string pps "hi="; ppe hi; add_string pps ",";
add_break pps (0, 0);
add_string pps "body="; ppe body; add_string pps "}";
add_break pps (0, 0);
end_block pps)
| ppe(AssignExp({var, exp}, _)) =
(begin_block pps INCONSISTENT 0;
add_string pps "AssignExp{"; add_break pps (0, 1);
add_string pps "var=";
ppv var; add_string pps ","; add_break pps (0, 1);
add_string pps "exp="; ppe exp; add_string pps "}";
end_block pps)
| ppe(IfExp({test, then', else'}, _)) =
(begin_block pps INCONSISTENT 0;
add_string pps "IfExp{"; add_break pps (0, 0);
add_string pps "test=";
ppe test; add_string pps ","; add_break pps (0, 0);
add_string pps "then'=";
ppe then'; add_string pps ","; add_break pps (0, 0);
add_string pps "else'=";
case else' of SOME e => ppe e | NONE => add_string pps "NONE";
add_break pps (0, 0);
add_string pps "}";
end_block pps)
| ppe(CallExp({func, args}, _)) =
(begin_block pps INCONSISTENT 0;
add_string pps "CallExp{"; add_break pps (0, 0);
add_string pps ("func="^func^","); add_break pps (0, 0);
add_string pps "args=[";
List.app (fn e => (ppe e; add_break pps (0, 0))) args;
add_string pps "]}";
add_break pps (0, 0);
end_block pps)
| ppe(SeqExp(explist, _)) =
(begin_block pps INCONSISTENT 0;
add_string pps "SeqExp([";
List.app (fn e => (ppe e; add_break pps (0, 0))) explist;
add_string pps "])"; add_break pps(0, 0);
end_block pps)
| ppe(RecordExp({fields, typ}, _)) =
(begin_block pps INCONSISTENT 0;
add_string pps "RecordExp{"; add_break pps (0, 1);
add_string pps "fields=["; add_break pps (0, 1);
List.app
(fn(s,e)=> (add_string pps ("("^s^",");
ppe e; add_string pps ")"; add_break pps (0, 0)))
fields;
add_string pps "],"; add_break pps (0, 1);
add_string pps "typ=";
add_string pps typ;
add_string pps "}";
end_block pps)
| ppe(ArrayExp({typ, size, init}, _)) =
(begin_block pps INCONSISTENT 0;
add_string pps "ArrayExp{test=";
add_string pps (typ^","); add_break pps (0, 1);
add_string pps "size=";
ppe size; add_string pps ","; add_break pps (0, 1);
add_string pps "init=";
ppe init; add_string pps "}";
end_block pps)
| ppe(LetExp({decs, body}, _)) =
(begin_block pps INCONSISTENT 0;
add_string pps "LetExp{decs=[";
List.app ppd decs;
add_string pps "],"; add_break pps (0, 1);
add_string pps "body=";
ppe body; add_string pps "}";
end_block pps)
in
begin_block pps INCONSISTENT 0;
ppe e0;
end_block pps
end
val ppstrm =
PP.mk_ppstream {
consumer=fn s=>TextIO.output(TextIO.stdOut, s),
linewidth = 79,
flush=fn()=>TextIO.flushOut TextIO.stdOut
}
fun exprAst e =
(ppexpr ppstrm e;
flush_ppstream ppstrm;
TextIO.output(TextIO.stdOut, "\n"))