Skip to content

Commit 99e913d

Browse files
committed
refactor: Rename and generalise recorder to ppx_autoregister
Functorising to add parameters so that it can be used to inject printer registerers as well, for example.
1 parent f0e8346 commit 99e913d

File tree

6 files changed

+92
-68
lines changed

6 files changed

+92
-68
lines changed

src/ppx-metaquot/dune

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
(libraries ppx_tools compiler-libs)
2121
)
2222

23-
(library
24-
(name learnocaml_recorder)
25-
(wrapped false)
26-
(modules Recorder)
27-
(libraries ppxlib))
23+
;; (library
24+
;; (name learnocaml_recorder)
25+
;; (wrapped false)
26+
;; (modules Recorder)
27+
;; (libraries ppxlib))
2828

2929
(library
3030
(name learnocaml_ppx_metaquot)
3131
(wrapped false)
32-
(libraries learnocaml_ppx_metaquot_lib ty fun_ty ocaml-migrate-parsetree learnocaml_recorder)
33-
(modules Ppx_metaquot_register)
32+
(libraries learnocaml_ppx_metaquot_lib ty fun_ty ocaml-migrate-parsetree ppxlib)
33+
(modules Ppx_autoregister Ppx_metaquot_grader)
3434
(kind ppx_rewriter)
3535
)
3636

@@ -43,7 +43,7 @@
4343
(section libexec)
4444
(package learn-ocaml)
4545
(files
46-
(ppx_metaquot_main.exe as grading_ppx/learnocaml-ppx-metaquot))
46+
(ppx_metaquot_main.exe as grading_ppx/learnocaml-ppx-grader))
4747
)
4848

4949
(library

src/ppx-metaquot/ppx_autoregister.ml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
open Ppxlib
2+
3+
module type ARG = sig
4+
val val_prefix: string
5+
val inject_def: string -> string loc -> expression
6+
end
7+
8+
module Make (Arg: ARG) = struct
9+
10+
let pattern_defs =
11+
object
12+
inherit [(string * string loc) list] Ast_traverse.fold as super
13+
14+
method! pattern p acc =
15+
let acc = super#pattern p acc in
16+
match p.ppat_desc with
17+
| Ppat_var var | Ppat_alias (_, var) -> (
18+
match String.index_opt var.txt '_' with
19+
| Some i when String.sub var.txt 0 i = Arg.val_prefix ->
20+
let suffix =
21+
String.sub var.txt (i + 1) (String.length var.txt - i - 1)
22+
in
23+
(suffix, var) :: acc
24+
| _ -> acc)
25+
| _ -> acc
26+
end
27+
28+
let rec get_defs bindings acc =
29+
match bindings with
30+
| [] -> List.rev @@ List.flatten acc
31+
| binding :: rest ->
32+
get_defs rest @@ (pattern_defs#pattern binding.pvb_pat [] :: acc)
33+
34+
module Ast_builder = Ast_builder.Make (struct
35+
let loc = Location.none
36+
end)
37+
38+
let val_recorder s =
39+
let open Ast_builder in
40+
let create_val_registration defs =
41+
let gen_expr (name, e) = Arg.inject_def name e in
42+
let val_registration = List.map gen_expr defs |> esequence in
43+
let register_toplevel =
44+
[ value_binding ~pat:punit ~expr:val_registration ]
45+
in
46+
pstr_value Nonrecursive register_toplevel
47+
in
48+
List.fold_right
49+
(fun si acc ->
50+
match si.pstr_desc with
51+
| Pstr_value (_, bindings) -> (
52+
match get_defs bindings [] with
53+
| [] -> si :: acc
54+
| defs -> si :: create_val_registration defs :: acc)
55+
| _ -> si :: acc)
56+
s []
57+
58+
let expand = val_recorder
59+
60+
end

src/ppx-metaquot/ppx_autoregister.mli

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module type ARG = sig
2+
val val_prefix: string
3+
val inject_def: string -> string Ppxlib.loc -> Ppxlib.expression
4+
end
5+
6+
module Make (_: ARG): sig
7+
val expand: Ppxlib.structure -> Ppxlib.structure
8+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Sampler_recorder = Ppx_autoregister.Make(struct
2+
let val_prefix = "sample"
3+
let inject_def name var =
4+
let open Ppxlib in
5+
let open Ast_builder.Default in
6+
let loc = var.Location.loc in
7+
pexp_apply ~loc
8+
(evar ~loc "Introspection.register_sampler")
9+
[ Nolabel, estring ~loc name
10+
; Nolabel, evar ~loc var.txt]
11+
end)
12+
13+
let () =
14+
Migrate_parsetree.Driver.register ~name:"ppx_metaquot" (module Migrate_parsetree.OCaml_412)
15+
(fun _config _cookies -> Ppx_metaquot.Main.expander []);
16+
Ppxlib.Driver.register_transformation "sample_recorder" ~impl:Sampler_recorder.expand

src/ppx-metaquot/ppx_metaquot_register.ml

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/ppx-metaquot/recorder.ml

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)