-
Notifications
You must be signed in to change notification settings - Fork 0
/
snick_pprint.ml
345 lines (317 loc) · 13.7 KB
/
snick_pprint.ml
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
(*
** File: snick_pprint.ml
** Description: Pretty-printer converts from snick source
** code to well-formed style.
** Last Modified: Sun. 9th April 2017
**
** Group name: Mainframe
**
** Member names | usernames
** Xianzhuo REN | xianzhuor
** Haoyu LIN | haoyul3
** Zequn MA | zequnm
*)
open Snick_ast
open Format
(* Print program as list of procedures. *)
let rec print_program fmtr prog = print_procs fmtr prog
(* Print list of procedures. *)
and print_procs fmtr = function
| [] -> ()
| x::[] -> fprintf fmtr "%a@," print_proc x
| x::xs -> fprintf fmtr "%a@,%a" print_proc x print_procs xs
(* Print a single procedure. *)
and print_proc fmtr (header, body) =
fprintf fmtr "@[<v>proc %a@;<0 4>@[<v>%a@]@,end@]@."
print_proc_header header print_proc_body body
(* Print procedure header. *)
and print_proc_header fmtr (ident, params) =
fprintf fmtr "%s (%a)" ident print_params params
(* Print the list of parameters in header. *)
and print_params fmtr = function
| [] -> ()
| x :: [] -> fprintf fmtr "%a" print_param x
| x :: xs -> fprintf fmtr "%a, %a" print_param x print_params xs
(* Print a single procedure parameter. *)
and print_param fmtr (indicator, param_type, ident) =
fprintf fmtr "%a %a %s"
print_param_indc indicator print_type param_type ident
(* Print the indicator of a procedure parameter. *)
and print_param_indc fmtr = function
| Val -> fprintf fmtr "%s" "val"
| Ref -> fprintf fmtr "%s" "ref"
(* Print the type of a procedure parameter. *)
and print_type fmtr = function
| Bool -> fprintf fmtr "%s" "bool"
| Int -> fprintf fmtr "%s" "int"
| Float -> fprintf fmtr "%s" "float"
(* Print procedure body as a list of declarations
** followed by a list of statements. *)
and print_proc_body fmtr prog_body =
fprintf fmtr "%a@,%a" print_decls prog_body.decls
print_stmts prog_body.stmts
(* Print the list of declarations. *)
and print_decls fmtr = function
| [] -> ()
| x :: [] -> fprintf fmtr "%a@," print_decl x
| x :: xs -> fprintf fmtr "%a@,%a" print_decl x print_decls xs
(* Print the list of statements. *)
and print_stmts fmtr = function
| [] -> ()
| x :: [] -> fprintf fmtr "%a" print_stmt x
| x :: xs -> fprintf fmtr "%a@,%a" print_stmt x print_stmts xs
(* Print a single declaration. *)
and print_decl fmtr (var_type, variable) =
fprintf fmtr "%a %a;" print_type var_type print_var variable
(* Print a variable. *)
and print_var fmtr = function
| Variable (ident, None) -> fprintf fmtr "%s" ident
| Variable (ident, Some itvls) -> fprintf fmtr "%s[%a]"
ident print_itvls itvls
(* Print list of intervals. *)
and print_itvls fmtr = function
| [] -> ()
| x::[] -> fprintf fmtr "%a" print_itvl x
| x::xs -> fprintf fmtr "%a,%a" print_itvl x print_itvls xs
(* Print a single interval *)
and print_itvl fmtr (st_pnt, end_pnt) =
fprintf fmtr "%d..%d" st_pnt end_pnt
(* Print statement. *)
and print_stmt fmtr = function
| Assign (elem, expr) -> fprintf fmtr "%a := %a;" print_elem elem
print_expr expr
| Read elem -> fprintf fmtr "read %a;" print_elem elem
| Write expr ->
begin
match expr with
| Expr wexpr -> fprintf fmtr "write %a;" print_expr wexpr
| String str -> fprintf fmtr "write %s;" str
end
| Call (ident, exprs) -> fprintf fmtr "%s(%a);" ident print_exprs exprs
| If_then (expr, stmts) -> fprintf fmtr "if %a then@;<0 4>@[<v>%a@]@,fi"
print_expr expr print_stmts stmts
| If_then_else (expr, then_stmts, else_stmts) ->
fprintf fmtr "if %a then@;<0 4>@[<v>%a@]@,else@;<0 4>@[<v>%a@]@,fi"
print_expr expr print_stmts then_stmts print_stmts else_stmts
| While (expr, stmts) -> fprintf fmtr "while %a do@;<0 4>@[<v>%a@]@,od"
print_expr expr print_stmts stmts
(* Print element to be assigned to or be read / written. *)
and print_elem fmtr = function
| Elem (ident, None) -> fprintf fmtr "%s" ident
| Elem (ident, Some idxs) -> fprintf fmtr "%s[%a]" ident print_exprs idxs
(* Print an expression. *)
and print_expr fmtr = function
| Eelem elem -> fprintf fmtr "%a" print_elem elem
| Ebool bool_const -> fprintf fmtr "%B" bool_const
| Eint int_const -> fprintf fmtr "%d" int_const
| Efloat float_const -> fprintf fmtr "%f" float_const
(* Parentheses to be printed (or removed) in other functions,
so only print the expression within the parenthesis.
*)
| Eparen expr -> fprintf fmtr "%a" print_expr (strip_paren expr)
| Ebinop bin_expr -> fprintf fmtr "%a" print_binop bin_expr
| Eunop un_expr -> fprintf fmtr "%a" print_unop un_expr
(* Print expressions. *)
and print_exprs fmtr = function
| [] -> ()
| x::[] -> fprintf fmtr "%a" print_expr x
| x::xs -> fprintf fmtr "%a, %a" print_expr x print_exprs xs
(* Print binary operations.
** Parenthese around an operation expression on LHS with higher
** or equal precedence will be removed.
** Parenthese around an operation expression on RHS with higher
** precedence will be removed.
*)
and print_binop fmtr = function
| (Eparen lexpr_inside, optr, Eparen rexpr_inside) ->
begin
let
lexpr_inside_strip = strip_paren lexpr_inside
and
rexpr_inside_strip = strip_paren rexpr_inside
in
match lexpr_inside_strip with
| Ebinop _ | Eunop _ ->
begin
match rexpr_inside_strip with
| Ebinop _ | Eunop _ ->
let
lcmpr_result = cmpr_prec lexpr_inside_strip optr
and
rcmpr_result = cmpr_prec rexpr_inside_strip optr
in
if lcmpr_result>=0 && rcmpr_result>0 then
fprintf fmtr "%a %a %a"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
else if lcmpr_result>=0 && rcmpr_result<=0 then
fprintf fmtr "%a %a (%a)"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
else if lcmpr_result<0 && rcmpr_result>0 then
fprintf fmtr "(%a) %a %a"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
else
fprintf fmtr "(%a) %a (%a)"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
| _ ->
let
lcmpr_result = cmpr_prec lexpr_inside_strip optr
in
if lcmpr_result>=0 then
fprintf fmtr "%a %a %a"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
else
fprintf fmtr "(%a) %a %a"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
end
| _ ->
begin
match rexpr_inside_strip with
| Ebinop _ | Eunop _ ->
let
rcmpr_result = cmpr_prec rexpr_inside_strip optr
in
if rcmpr_result>0 then
fprintf fmtr "%a %a %a"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
else
fprintf fmtr "%a %a (%a)"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
| _ ->
fprintf fmtr "%a %a %a"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
end
end
| (Eparen lexpr_inside, optr, rexpr) ->
begin
let
lexpr_inside_strip = strip_paren lexpr_inside
in
match lexpr_inside_strip with
| Ebinop _ | Eunop _ ->
let
lcmpr_result = cmpr_prec lexpr_inside_strip optr
in
if lcmpr_result>=0 then
fprintf fmtr "%a %a %a"
print_expr lexpr_inside_strip
print_optr optr print_expr rexpr
else
fprintf fmtr "(%a) %a %a"
print_expr lexpr_inside_strip
print_optr optr print_expr rexpr
| _ ->
fprintf fmtr "%a %a %a"
print_expr lexpr_inside_strip
print_optr optr print_expr rexpr
end
| (lexpr, optr, Eparen rexpr_inside) ->
begin
let
rexpr_inside_strip = strip_paren rexpr_inside
in
match rexpr_inside_strip with
| Ebinop _ | Eunop _ ->
let
rcmpr_result = cmpr_prec rexpr_inside_strip optr
in
if rcmpr_result>0 then
fprintf fmtr "%a %a %a"
print_expr lexpr print_optr optr
print_expr rexpr_inside_strip
else
fprintf fmtr "%a %a (%a)"
print_expr lexpr print_optr optr
print_expr rexpr_inside_strip
| _ ->
fprintf fmtr "%a %a %a"
print_expr lexpr print_optr optr
print_expr rexpr_inside_strip
end
| (lexpr, optr, rexpr) ->
begin
fprintf fmtr "%a %a %a"
print_expr lexpr print_optr optr print_expr rexpr
end
(* Print unary operations.
** Parenthese around an operation expression with higher precedence
** will be removed. *)
and print_unop fmtr = function
| (optr, Eparen expr_inside) ->
begin
let
expr_inside_strip = strip_paren expr_inside
in
match expr_inside_strip with
| Ebinop _ | Eunop _ ->
let
cmpr_result = cmpr_prec expr_inside_strip optr
in
if cmpr_result<0 then
fprintf fmtr "%a (%a)"
print_optr optr print_expr expr_inside_strip
else
fprintf fmtr "%a %a"
print_optr optr print_expr expr_inside_strip
| _ ->
fprintf fmtr "%a %a"
print_optr optr print_expr expr_inside_strip
end
| (optr, expr) ->
begin
fprintf fmtr "%a %a"
print_optr optr print_expr expr
end
(* compare operator precedence *)
and cmpr_prec exp optr = match exp with
| Ebinop (_, exp_binoptr, _) -> (get_prec exp_binoptr) - (get_prec optr)
| Eunop (exp_unoptr, _) -> (get_prec exp_unoptr) - (get_prec optr)
| _ -> raise (Failure "Impossible!")
(* get the precedence of an operator *)
and get_prec optr = match optr with
| Op_minus -> 7
| Op_mul | Op_div -> 6
| Op_add | Op_sub -> 5
| Op_eq | Op_ne | Op_lt | Op_gt | Op_le | Op_ge -> 4
| Op_not -> 3
| Op_and -> 2
| Op_or -> 1
(* print operator symbol *)
and print_optr fmtr optr = match optr with
| Op_add -> fprintf fmtr "%s" "+"
| Op_sub -> fprintf fmtr "%s" "-"
| Op_mul -> fprintf fmtr "%s" "*"
| Op_div -> fprintf fmtr "%s" "/"
| Op_eq -> fprintf fmtr "%s" "="
| Op_ne -> fprintf fmtr "%s" "!="
| Op_lt -> fprintf fmtr "%s" "<"
| Op_gt -> fprintf fmtr "%s" ">"
| Op_le -> fprintf fmtr "%s" "<="
| Op_ge -> fprintf fmtr "%s" ">="
| Op_and -> fprintf fmtr "%s" "and"
| Op_or -> fprintf fmtr "%s" "or"
| Op_not -> fprintf fmtr "%s" "not"
| Op_minus -> fprintf fmtr "%s" "-"
(* Strips parentheses in case there are multiple pairs of parentheses
** around an expression.
*)
and strip_paren expr = match expr with
| Eparen paren_expr -> strip_paren paren_expr
| _ -> expr