/
fake.ml
408 lines (342 loc) · 12.2 KB
/
fake.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
open Parsetree
let default_loc = function
| None -> Location.none
| Some loc -> loc
let mkoptloc opt x =
match opt with
| None -> Location.mknoloc x
| Some l -> Location.mkloc x l
let app a b =
let pexp_loc = { b.pexp_loc with Location.loc_ghost = true } in
{ pexp_desc = Pexp_apply (a, ["", b]) ; pexp_loc }
let pat_app f (pat,expr) = pat, app f expr
let prim_ident prim = Longident.parse ("_." ^ prim)
let prim prim = {
pexp_desc = Pexp_ident (Location.mknoloc (prim_ident prim));
pexp_loc = Location.none
}
let any_val' = prim "Any.val'"
type type_scheme = [
| `Var of string
| `Arrow of Asttypes.label * type_scheme * type_scheme
| `Named of type_scheme list * string
]
(* extend as needed *)
type ast = [
| `Let of binding list
| `Fun of string list * ast
| `App of ast * ast
| `Ident of string
| `AnyVal (* wild card ident *)
| `Val of string * type_scheme (* TODO: use something similar to [binding] type? *)
]
and binding = {
ident : string ;
typesig : type_scheme ;
body : ast ;
}
let rec translate_ts ?ghost_loc = function
| `Var ident ->
{ ptyp_desc = Ptyp_var ident ; ptyp_loc = default_loc ghost_loc }
| `Arrow (label, a, b) ->
let a = translate_ts ?ghost_loc a in
let b = translate_ts ?ghost_loc b in
{ ptyp_desc = Ptyp_arrow(label, a, b) ; ptyp_loc = default_loc ghost_loc }
| `Named (params, id) ->
let id = Longident.parse id in
let params = List.map (translate_ts ?ghost_loc) params in
{
ptyp_desc = Ptyp_constr (mkoptloc ghost_loc id, params) ;
ptyp_loc = default_loc ghost_loc ;
}
let translate_declaration ?ghost_loc = function
| `Val (id, sign) ->
let pval_ty = {
pval_type = translate_ts ?ghost_loc sign ;
pval_prim = [] ;
pval_loc = default_loc ghost_loc ;
} in
let psig_desc = Psig_value (mkoptloc ghost_loc id, pval_ty) in
{ psig_desc ; psig_loc = default_loc ghost_loc }
let rec translate_binding ?ghost_loc { ident ; typesig ; body } =
let pat = {
ppat_desc = Ppat_var (mkoptloc ghost_loc ident) ;
ppat_loc = default_loc ghost_loc ;
}
in
let typesig_opt = Some (translate_ts ?ghost_loc typesig) in
let body = translate_to_expr ?ghost_loc body in
let pexp = {
pexp_desc = Pexp_constraint (body, typesig_opt, None) ;
pexp_loc = default_loc ghost_loc ;
}
in
(pat, pexp)
and translate_to_str ?ghost_loc = function
| `Let lst ->
let p = Pstr_value (Asttypes.Nonrecursive, List.map translate_binding lst) in
{ pstr_desc = p ; pstr_loc = default_loc ghost_loc }
and translate_to_expr ?ghost_loc = function
| `Let _ -> failwith "not allowed at this level"
| `Val _ -> failwith "clearly not allowed here" (* TODO: refine [ast] *)
| `Fun (simple_patterns, body) ->
List.fold_right
(fun simple_pattern body ->
let patt = {
ppat_desc = Ppat_var (mkoptloc ghost_loc simple_pattern) ;
ppat_loc = default_loc ghost_loc ;
}
in
{
pexp_desc = Pexp_function ("", None, [patt, body]) ;
pexp_loc = default_loc ghost_loc ;
})
simple_patterns
(translate_to_expr ?ghost_loc body)
| `App (f, x) ->
app (translate_to_expr ?ghost_loc f) (translate_to_expr ?ghost_loc x)
| `Ident i -> {
pexp_desc = Pexp_ident (mkoptloc ghost_loc (Longident.parse i)) ;
pexp_loc = default_loc ghost_loc ;
}
| `AnyVal -> any_val'
module Lwt = struct
let un_lwt = prim "Lwt.un_lwt"
let to_lwt = prim "Lwt.to_lwt"
let in_lwt = prim "Lwt.in_lwt"
let unit_lwt = prim "Lwt.unit_lwt"
let un_stream = prim "Lwt.un_stream"
let finally' = prim "Lwt.finally'"
let raise_lwt' = prim_ident "Lwt.raise_lwt'"
end
(* tools used in the next few modules *)
let format_params ~f =
List.map (function None -> f "_" | Some id -> f id.Location.txt)
let mk_fun ~args = `Fun (args, `App (`Ident "Obj.magic", `AnyVal))
module Sexp : sig
type ty = string Location.loc * Parsetree.type_declaration
module Struct : sig
val make_funs : ty -> [ `Let of binding list ]
end
module Sig : sig
val make_decls : ty -> [ `Val of string * type_scheme ] list
end
end = struct
type ty = string Location.loc * Parsetree.type_declaration
let t = `Named ([], "Sexplib.Sexp.t")
module TypeSig = struct
let mk_arrow x y = `Arrow ("", x, y)
let sexp_of params ty =
let params = format_params ~f:(fun v -> `Var v) params in
List.fold_right (fun var acc -> mk_arrow (mk_arrow var t) acc) params
(mk_arrow (`Named (params, ty)) t)
let of_sexp params ty =
let params = format_params ~f:(fun v -> `Var v) params in
List.fold_right (fun var acc -> mk_arrow (mk_arrow t var) acc) params
(mk_arrow t (`Named (params, ty)))
end
module Struct = struct
let sexp_of_ (located_name, type_infos) =
let ty = located_name.Location.txt in
let args = format_params ~f:(fun x -> "sexp_of_" ^ x) type_infos.ptype_params
in
{
ident = "sexp_of_" ^ ty ;
typesig = TypeSig.sexp_of type_infos.ptype_params ty;
body = mk_fun ~args ;
}
let _of_sexp (located_name, type_infos) =
let ty = located_name.Location.txt in
let args = format_params ~f:(fun x -> x ^ "_of_sexp") type_infos.ptype_params
in
{
ident = ty ^ "_of_sexp" ;
typesig = TypeSig.of_sexp type_infos.ptype_params ty;
body = mk_fun ~args ;
}
let make_funs ty = `Let [ sexp_of_ ty ; _of_sexp ty ]
end
module Sig = struct
let sexp_of_ (located_name, type_infos) =
let ty = located_name.Location.txt in
let typesig = TypeSig.sexp_of type_infos.ptype_params ty in
`Val ("sexp_of_" ^ ty, typesig)
let _of_sexp (located_name, type_infos) =
let ty = located_name.Location.txt in
let typesig = TypeSig.of_sexp type_infos.ptype_params ty in
`Val (ty ^ "_of_sexp", typesig)
let make_decls ty = [ sexp_of_ ty ; _of_sexp ty ]
end
end
module Binprot = struct
let make_struct ~prefix ~typesig (located_name, ty_infos) =
let tyname = located_name.Location.txt in
let args = format_params ~f:(fun x -> prefix ^ x) ty_infos.ptype_params in
{ ident = prefix ^ tyname ; typesig = typesig ; body = mk_fun ~args }
module Sizer = struct
let int = `Named ([], "int")
let typesig (name, ty_infos) =
let params = format_params ~f:(fun x -> x) ty_infos.ptype_params in
List.fold_right (fun v acc -> `Arrow ("", `Arrow ("", `Var v, int), acc)) params
(`Arrow ("", `Named (List.map (fun x -> `Var x) params, name.Location.txt), int))
let make_struct ty =
let typesig = typesig ty in
make_struct ~prefix:"bin_size_" ~typesig ty
let make_sig ty =
let tyname = (fst ty).Location.txt in
let typesig = typesig ty in
`Val ("bin_size_" ^ tyname, typesig)
end
module Write = struct
let typesig (name, ty_infos) =
let params = format_params ~f:(fun x -> x) ty_infos.ptype_params in
let acc =
`Arrow ("", `Named ([], "Bin_prot.Common.buf"),
`Arrow ("pos", `Named ([], "Bin_prot.Common.pos"),
`Arrow ("", `Named (List.map (fun x -> `Var x) params, name.Location.txt),
`Named ([], "Bin_prot.Common.pos"))))
in
let make_var str =
`Arrow ("", `Named ([], "Bin_prot.Unsafe_common.sptr"),
`Arrow ("", `Named ([], "Bin_prot.Unsafe_common.eptr"),
`Arrow ("", `Var str,
`Named ([], "Bin_prot.Unsafe_common.sptr"))))
in
List.fold_right (fun v acc -> `Arrow ("", make_var v, acc)) params acc
let make_struct ty =
let typesig = typesig ty in
make_struct ~prefix:"bin_write_" ~typesig ty
let make_sig ty =
let tyname = (fst ty).Location.txt in
let typesig = typesig ty in
`Val ("bin_write_" ^ tyname, typesig)
end
module Writer = struct
let typesig (name, ty_infos) =
let params = format_params ~f:(fun x -> `Var x) ty_infos.ptype_params in
List.fold_right
(fun param acc -> `Arrow ("", `Named ([param], "Bin_prot.Type_class.writer"), acc))
params
(`Named ([`Named (params, name.Location.txt)], "Bin_prot.Type_class.writer"))
let make_struct ty =
let typesig = typesig ty in
make_struct ~prefix:"bin_writer_" ~typesig ty
let make_sig ty =
let tyname = (fst ty).Location.txt in
let typesig = typesig ty in
`Val ("bin_writer_" ^ tyname, typesig)
end
module Read = struct
let typesig (name, ty_infos) =
let params = format_params ~f:(fun x -> x) ty_infos.ptype_params in
let acc =
`Arrow ("", `Named ([], "Bin_prot.Common.buf"),
`Arrow ("pos_ref", `Named ([`Named ([], "Bin_prot.Common.pos")], "ref"),
`Named (List.map (fun x -> `Var x) params, name.Location.txt)))
in
let make_var str =
`Arrow ("", `Named ([], "Bin_prot.Unsafe_common.sptr_ptr"),
`Arrow ("", `Named ([], "Bin_prot.Unsafe_common.eptr"),
`Var str))
in
List.fold_right (fun v acc -> `Arrow ("", make_var v, acc)) params acc
let make_struct ty =
let typesig = typesig ty in
make_struct ~prefix:"bin_read_" ~typesig ty
let make_sig ty =
let tyname = (fst ty).Location.txt in
let typesig = typesig ty in
`Val ("bin_read_" ^ tyname, typesig)
end
module Reader = struct
let typesig (name, ty_infos) =
let params = format_params ~f:(fun x -> `Var x) ty_infos.ptype_params in
List.fold_right
(fun param acc -> `Arrow ("", `Named ([param], "Bin_prot.Type_class.reader"), acc))
params
(`Named ([`Named (params, name.Location.txt)], "Bin_prot.Type_class.reader"))
let make_struct ty =
let typesig = typesig ty in
make_struct ~prefix:"bin_reader_" ~typesig ty
let make_sig ty =
let tyname = (fst ty).Location.txt in
let typesig = typesig ty in
`Val ("bin_reader_" ^ tyname, typesig)
end
end
module TypeWith = struct
type generator = string
let rec generate_definitions ~ty ?ghost_loc = function
| "sexp" ->
let funs = List.map Sexp.Struct.make_funs ty in
List.map (translate_to_str ?ghost_loc) funs
| "bin_write" ->
let funs =
let open Binprot in
List.map (fun ty ->
`Let [ Sizer.make_struct ty ; Write.make_struct ty ; Writer.make_struct ty ]
) ty
in
List.map (translate_to_str ?ghost_loc) funs
| "bin_read" ->
let funs =
let open Binprot in
List.map (fun ty -> `Let [ Read.make_struct ty ; Reader.make_struct ty ]) ty
in
List.map (translate_to_str ?ghost_loc) funs
| "bin_io" ->
let funs =
let open Binprot in
List.map (fun ty ->
`Let [
Sizer.make_struct ty ;
Write.make_struct ty ;
Writer.make_struct ty ;
Read.make_struct ty ;
Reader.make_struct ty ;
]
) ty
in
List.map (translate_to_str ?ghost_loc) funs
| _unsupported_ext -> []
let generate_definitions ~ty ?ghost_loc extensions =
Misc.list_concat_map (generate_definitions ~ty ?ghost_loc) extensions
let rec generate_sigs ~ty ?ghost_loc = function
| "sexp" ->
let sigs = Misc.list_concat_map Sexp.Sig.make_decls ty in
List.rev_map (translate_declaration ?ghost_loc) sigs
| "bin_write" ->
let sigs =
let open Binprot in
Misc.list_concat_map
(fun ty ->
[ Sizer.make_sig ty ; Write.make_sig ty ; Writer.make_sig ty ])
ty
in
List.rev_map (translate_declaration ?ghost_loc) sigs
| "bin_read" ->
let sigs =
let open Binprot in
Misc.list_concat_map
(fun ty -> [ Read.make_sig ty ; Reader.make_sig ty ]) ty
in
List.rev_map (translate_declaration ?ghost_loc) sigs
| "bin_io" ->
let sigs =
let open Binprot in
Misc.list_concat_map
(fun ty ->
[
Sizer.make_sig ty ;
Write.make_sig ty ;
Writer.make_sig ty ;
Read.make_sig ty ;
Reader.make_sig ty ;
])
ty
in
List.rev_map (translate_declaration ?ghost_loc) sigs
| _unsupported_ext -> []
let generate_sigs ~ty ?ghost_loc extensions =
Misc.list_concat_map (generate_sigs ~ty ?ghost_loc) extensions
end