@@ -50,12 +50,65 @@ module Oak = struct
5050 | Tobject _ -> Ident " type_desc.Tobject"
5151 | Tfield _ -> Ident " type_desc.Tfield"
5252 | 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}
5455 | 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}
5659 | Tunivar _ -> Ident " type_desc.Tunivar"
5760 | Tpoly _ -> Ident " type_desc.Tpoly"
5861 | 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"
59112end
60113
61114(* * Transform the Oak types to string *)
@@ -116,7 +169,7 @@ module CodePrinter = struct
116169
117170 let sepNln ctx =
118171 {ctx with events = WriteLine :: ctx.events; current_line_column = 0 }
119-
172+ let sepSpace ctx = ! - " " ctx
120173 let sepComma ctx = ! - " , " ctx
121174 let sepSemi ctx = ! - " ; " ctx
122175 let sepOpenT ctx = ! - " (" ctx
@@ -126,6 +179,7 @@ module CodePrinter = struct
126179 let sepOpenL ctx = ! - " [" ctx
127180 let sepCloseL ctx = ! - " ]" ctx
128181 let sepEq ctx = ! - " = " ctx
182+ let wrapInParentheses f = sepOpenT +> f +> sepCloseT
129183 let indent ctx =
130184 let nextIdent = ctx.current_indent + ctx.indent_size in
131185 {
@@ -197,14 +251,18 @@ module CodePrinter = struct
197251 in
198252 let long =
199253 ! - (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
202258 in
203259 expressionFitsOnRestOfLine short long
204260
205261 and genRecord (recordFields : Oak.namedField list ) : appendEvents =
206262 let short =
207- sepOpenR +> col genNamedField sepSemi recordFields +> sepCloseR
263+ sepOpenR +> sepSpace
264+ +> col genNamedField sepSemi recordFields
265+ +> sepSpace +> sepCloseR
208266 in
209267 let long =
210268 sepOpenR
@@ -232,9 +290,19 @@ module CodePrinter = struct
232290 expressionFitsOnRestOfLine short long
233291
234292 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
236304 let long =
237- sepOpenL +> indentAndNln (col genOak sepNln items) +> sepNln +> sepCloseL
305+ sepOpenL +> indentAndNln (col genItem sepNln items) +> sepNln +> sepCloseL
238306 in
239307 expressionFitsOnRestOfLine short long
240308end
0 commit comments