-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty.ml
257 lines (231 loc) · 7.46 KB
/
pretty.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
(******************************************************************************)
(* The Frenetic Project *)
(* [email protected] *)
(******************************************************************************)
(* Licensed to the Frenetic Project by one or more contributors. See the *)
(* NOTICE file distributed with this work for additional information *)
(* regarding copyright and ownership. The Frenetic Project licenses this *)
(* file to you under the following license. *)
(* *)
(* Redistribution and use in source and binary forms, with or without *)
(* modification, are permitted provided the following conditions are met: *)
(* - Redistributions of source code must retain the above copyright *)
(* notice, this list of conditions and the following disclaimer. *)
(* - Redistributions in binary form must reproduce the above copyright *)
(* notice, this list of conditions and the following disclaimer in *)
(* the documentation or other materials provided with the distribution. *)
(* - The names of the copyright holds and contributors may not be used to *)
(* endorse or promote products derived from this work without specific *)
(* prior written permission. *)
(* *)
(* Unless required by applicable law or agreed to in writing, software *)
(* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT *)
(* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the *)
(* LICENSE file distributed with this work for specific language governing *)
(* permissions and limitations under the License. *)
(******************************************************************************)
(* /src/compiler/pretty.ml *)
(* Pretty printer *)
(* $Id$ *)
(******************************************************************************)
(* ----- imports and abbreviations ----- *)
open Syntax
let msg = Util.format
(* ----- formatters for abstract syntax trees ----- *)
let rec format_type = function
| TUnit -> msg "@[unit@]"
| TBool -> msg "@[bool@]"
| TInteger -> msg "@[int@]"
| TChar -> msg "@[char@]"
| TString -> msg "@[string@]"
| TProduct(t1,t2) ->
msg "@[<2>(";
format_type t1;
msg "@ *@ ";
format_type t2;
msg ")@]"
| TData([],x) ->
msg "@[%s@]" (Id.string_of_t x)
| TData([t],x) ->
msg "@[";
format_type t;
msg "@ ";
msg "%s@]" (Id.string_of_t x)
| TData(ts,x) ->
msg "@[<2>(@[<2>";
Util.format_list ",@ " format_type ts;
msg "@])@ %s@]" (Id.string_of_t x)
| TFunction(t1,t2) ->
msg "@[(";
format_type t1;
msg "@ ->@ ";
format_type t2;
msg ")@]"
| TVar(x) ->
msg "@['%s@]" (Id.string_of_t x)
let format_scheme = function
| Scheme(_,t) ->
msg "@[<2>(";
format_type t;
msg ")@]"
let rec format_pattern = function
| PWild _ -> msg "_"
| PUnit _ -> msg "()"
| PInteger(_,n) -> msg "%d" n
| PBool(_,b) -> msg "%b" b
| PString(_,s) -> msg "%s" s
| PVar(_,x,_) -> msg "%s" (Id.string_of_t x)
| PPair(_,p1,p2) ->
msg "@[<2>(";
format_pattern p1;
msg ",@,";
format_pattern p2;
msg ")@]";
| PData(_,x,None) -> msg "%s" (Id.string_of_t x)
| PData(_,x,Some p1) ->
msg "@[<2>(%s@ " (Id.string_of_t x);
format_pattern p1;
msg ")@]"
and format_param p0 = match p0 with
| Param(_,p,None) ->
msg "@[";
format_pattern p;
msg "@]";
| Param(_,p,Some t) ->
msg "@[(";
format_pattern p;
msg ":";
format_type t;
msg ")@]"
and format_bind b0 = match b0 with
| Bind(_,p,tyo,e) ->
msg "@[";
format_pattern p;
(match tyo with
| None -> ()
| Some t -> msg "@ :@ "; format_type t);
msg "@ =@ ";
format_exp e;
msg "@]"
and format_exp e0 = match e0 with
| EApp(_,e1,e2) ->
msg "@[<2>(";
format_exp e1;
msg "@ ";
format_exp e2;
msg ")@]"
| EVar(_,x) ->
msg "@[%s@]" (Id.string_of_t x)
| EFun(_,p,e) ->
msg "@[<2>(fun@ ";
format_param p;
msg "@ ->@ ";
format_exp e;
msg ")@]";
| ECond(_,e1,e2,e3) ->
msg "@[<2>if@ ";
format_exp e1;
msg "@ then@ ";
format_exp e2;
msg "@ else@ ";
format_exp e3;
msg "@]";
| ELet(_,b,e) ->
msg "@[<2>let ";
format_bind b;
msg "@ in@ ";
format_exp e;
msg "@]";
| EAsc(_,e,t) ->
msg "@[<2>(";
format_exp e;
msg "@ :@ ";
format_type t;
msg ")@]";
| EOver(_,o,[e1;e2]) ->
msg "@[<2>(";
format_exp e1;
msg "@ ";
format_op o;
msg "@ ";
format_exp e2;
msg ")@]"
| EOver _ ->
Error.simple_error "Cannot pretty print ill-formed EOver expression"
| EPair(_,e1,e2) ->
msg "@[<2>(@[";
format_exp e1;
msg ",@,";
format_exp e2;
msg "@])@]"
| ECase(_,e1,bs) ->
msg "@[<2>(match@ ";
format_exp e1;
msg "@ with@ ";
Util.format_list "@ | "
(fun (p,e) ->
msg "@[<2>";
format_pattern p;
msg "@ ->@ ";
format_exp e;
msg "@]")
bs;
msg ")@]"
| EUnit _ ->
msg "()"
| EInteger (_,i) ->
msg "@[%d@]" i
| EChar(_,c) ->
msg "'%s'" (Char.escaped c)
| EString (_,s) ->
msg "@[\"%s\"@]" (String.escaped s)
| EBool (_,b) ->
msg "@[%b@]" b
and format_op = function
| OSemi -> msg ";"
| OMinus -> msg "-"
| OEqual -> msg "="
| OLt -> msg "<"
| OLeq -> msg "<="
| OGt -> msg ">"
| OGeq -> msg ">="
and format_decl = function
DLet (_,b) ->
msg "@[<2>let ";
format_bind b;
msg "@]"
| DType(_,vs,x,cs) ->
msg "@[<2>type@ ";
(match vs with
| [] -> ()
| [x] -> msg "%s" (Id.string_of_t x)
| _ ->
msg "(";
Util.format_list ",@ " (fun xi -> msg "%s" (Id.string_of_t xi)) vs;
msg ")");
msg "@ %s@ =@ " (Id.string_of_t x);
Util.format_list " | "
(fun (l,tyo) -> match tyo with
| None ->
msg "%s" (Id.string_of_t l)
| Some t ->
msg "(%s@ " (Id.string_of_t l);
format_type t;
msg ")")
cs;
msg "@]"
and format_modl = function
| Modl (_,m,ds) ->
msg "@[module %s =@\n @[" (Id.string_of_t m);
Util.format_list "@\n" format_decl ds;
msg "@\n@]@\n@]"
(* ----- conversions to string ----- *)
let to_string = Util.format_to_string
let string_of_exp e = to_string (fun () -> format_exp e)
let string_of_bind b = to_string (fun () -> format_bind b)
let string_of_decl d = to_string (fun () -> format_decl d)
let string_of_op o = to_string (fun () -> format_op o)
let string_of_modl m = to_string (fun () -> format_modl m)
let string_of_type s = to_string (fun () -> format_type s)
let string_of_param p = to_string (fun () -> format_param p)
let string_of_pattern p = to_string (fun () -> format_pattern p)