@@ -50,12 +50,65 @@ module Oak = struct
50
50
| Tobject _ -> Ident " type_desc.Tobject"
51
51
| Tfield _ -> Ident " type_desc.Tfield"
52
52
| Tnil -> Ident " type_desc.Tnil"
53
- | Tlink {desc} -> Ident " type_desc.Tlink"
53
+ | Tlink {desc} ->
54
+ Application {name = " type_desc.Tlink" ; argument = mk_type_desc desc}
54
55
| Tsubst _ -> Ident " type_desc.Tsubst"
55
- | Tvariant row_descr -> Ident " type_desc.Tvariant"
56
+ | Tvariant row_descr ->
57
+ Application
58
+ {name = " type_desc.Tvariant" ; argument = mk_row_desc row_descr}
56
59
| Tunivar _ -> Ident " type_desc.Tunivar"
57
60
| Tpoly _ -> Ident " type_desc.Tpoly"
58
61
| Tpackage _ -> Ident " type_desc.Tpackage"
62
+
63
+ and mk_row_desc (row_desc : Types.row_desc ) : oak =
64
+ let fields =
65
+ [
66
+ {
67
+ name = " row_fields" ;
68
+ value =
69
+ ( row_desc.row_fields
70
+ |> List. map (fun (label , row_field ) ->
71
+ Tuple
72
+ [
73
+ {name = " label" ; value = Ident label};
74
+ {name = " row_field" ; value = mk_row_field row_field};
75
+ ])
76
+ |> fun ts -> List ts );
77
+ };
78
+ {name = " row_more" ; value = mk_type_desc row_desc.row_more.desc};
79
+ {name = " row_closed" ; value = mk_bool row_desc.row_closed};
80
+ {name = " row_fixed" ; value = mk_bool row_desc.row_fixed};
81
+ ]
82
+ in
83
+ match row_desc.row_name with
84
+ | None -> Record fields
85
+ | Some (path , ts ) ->
86
+ Record
87
+ ({
88
+ name = " row_name" ;
89
+ value =
90
+ Tuple
91
+ [
92
+ {name = " Path.t" ; value = Ident (path_to_string path)};
93
+ {
94
+ name = " fields" ;
95
+ value =
96
+ List
97
+ (ts
98
+ |> List. map (fun (t : Types.type_expr ) ->
99
+ mk_type_desc t.desc));
100
+ };
101
+ ];
102
+ }
103
+ :: fields)
104
+
105
+ and mk_row_field (row_field : Types.row_field ) : oak =
106
+ match row_field with
107
+ | Rpresent _ -> Ident " row_field.Rpresent"
108
+ | Reither _ -> Ident " row_field.Reither"
109
+ | Rabsent -> Ident " row_field.Rabsent"
110
+
111
+ and mk_bool (b : bool ) : oak = if b then Ident " true" else Ident " false"
59
112
end
60
113
61
114
(* * Transform the Oak types to string *)
@@ -116,7 +169,7 @@ module CodePrinter = struct
116
169
117
170
let sepNln ctx =
118
171
{ctx with events = WriteLine :: ctx.events; current_line_column = 0 }
119
-
172
+ let sepSpace ctx = ! - " " ctx
120
173
let sepComma ctx = ! - " , " ctx
121
174
let sepSemi ctx = ! - " ; " ctx
122
175
let sepOpenT ctx = ! - " (" ctx
@@ -126,6 +179,7 @@ module CodePrinter = struct
126
179
let sepOpenL ctx = ! - " [" ctx
127
180
let sepCloseL ctx = ! - " ]" ctx
128
181
let sepEq ctx = ! - " = " ctx
182
+ let wrapInParentheses f = sepOpenT +> f +> sepCloseT
129
183
let indent ctx =
130
184
let nextIdent = ctx.current_indent + ctx.indent_size in
131
185
{
@@ -197,14 +251,18 @@ module CodePrinter = struct
197
251
in
198
252
let long =
199
253
! - (application.name) +> sepOpenT
200
- +> indentAndNln (genOak application.argument)
201
- +> sepNln +> sepCloseT
254
+ +> (match application.argument with
255
+ | Oak. List _ | Oak. Record _ -> genOak application.argument
256
+ | _ -> indentAndNln (genOak application.argument) +> sepNln)
257
+ +> sepCloseT
202
258
in
203
259
expressionFitsOnRestOfLine short long
204
260
205
261
and genRecord (recordFields : Oak.namedField list ) : appendEvents =
206
262
let short =
207
- sepOpenR +> col genNamedField sepSemi recordFields +> sepCloseR
263
+ sepOpenR +> sepSpace
264
+ +> col genNamedField sepSemi recordFields
265
+ +> sepSpace +> sepCloseR
208
266
in
209
267
let long =
210
268
sepOpenR
@@ -232,9 +290,19 @@ module CodePrinter = struct
232
290
expressionFitsOnRestOfLine short long
233
291
234
292
and genList (items : Oak.oak list ) : appendEvents =
235
- let short = sepOpenL +> col genOak sepSemi items +> sepCloseL in
293
+ let genItem = function
294
+ | Oak. Tuple _ as item -> wrapInParentheses (genOak item)
295
+ | item -> genOak item
296
+ in
297
+ let short =
298
+ match items with
299
+ | [] -> sepOpenL +> sepCloseL
300
+ | _ ->
301
+ sepOpenL +> sepSpace +> col genItem sepSemi items +> sepSpace
302
+ +> sepCloseL
303
+ in
236
304
let long =
237
- sepOpenL +> indentAndNln (col genOak sepNln items) +> sepNln +> sepCloseL
305
+ sepOpenL +> indentAndNln (col genItem sepNln items) +> sepNln +> sepCloseL
238
306
in
239
307
expressionFitsOnRestOfLine short long
240
308
end
0 commit comments