Skip to content

Commit 3cd75f5

Browse files
hernoufMAltGr
authored andcommitted
feat(ppx-metaquot): Add transformation introducing the register_sampler calls
* A new transformation has been added that inserts `let () = Introspection.register_sampler name fun` for each toplevel binding prefixed with `sample_*` in test.ml. * Compilation units stored in `demo-repository/exercises/exercise_name/` during the precompilation are no longer staged.
1 parent 46631d8 commit 3cd75f5

File tree

7 files changed

+73
-5
lines changed

7 files changed

+73
-5
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,8 @@ tests/corpuses/*
3535
detect-libs.*
3636

3737
docs/odoc.html
38+
39+
demo-repository/exercises/**/*.cmo
40+
demo-repository/exercises/**/*.cmi
41+
demo-repository/exercises/**/*.cma
42+
demo-repository/exercises/**/*.js

src/grader/grading_cli.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* included LICENSE file for details. *)
88

99
(** Take an exercise, a solution, and return the report, stdout,
10-
stderr and outcomes of the toplevel, or raise ont of the
10+
stderr and outcomes of the toplevel, or raise one of the
1111
exceptions defined in module {!Grading}. *)
1212
val get_grade:
1313
?callback:(string -> unit) ->

src/ppx-metaquot/dune

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@
2020
(libraries ppx_tools compiler-libs)
2121
)
2222

23+
(library
24+
(name learnocaml_recorder)
25+
(wrapped false)
26+
(modules Recorder)
27+
(libraries ppxlib))
28+
2329
(library
2430
(name learnocaml_ppx_metaquot)
2531
(wrapped false)
26-
(libraries learnocaml_ppx_metaquot_lib ty fun_ty ocaml-migrate-parsetree)
32+
(libraries learnocaml_ppx_metaquot_lib ty fun_ty ocaml-migrate-parsetree learnocaml_recorder)
2733
(modules Ppx_metaquot_register)
2834
(kind ppx_rewriter)
2935
)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
let () =
22
Migrate_parsetree.Driver.register ~name:"ppx_metaquot" (module Migrate_parsetree.OCaml_412)
3-
(fun _config _cookies -> Ppx_metaquot.Main.expander [])
3+
(fun _config _cookies -> Ppx_metaquot.Main.expander []);
4+
Ppxlib.Driver.register_transformation "sample_recorder" ~impl:Recorder.expand

src/ppx-metaquot/recorder.ml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
open Ppxlib
2+
3+
let pattern_samplers =
4+
object
5+
inherit [string list] Ast_traverse.fold as super
6+
7+
method! pattern p acc =
8+
let acc = super#pattern p acc in
9+
match p.ppat_desc with
10+
| Ppat_var var -> (
11+
match String.index_opt var.txt '_' with
12+
| Some i when String.sub var.txt 0 i = "sample" ->
13+
let suffix =
14+
String.sub var.txt (i + 1) (String.length var.txt - i - 1)
15+
in
16+
suffix :: acc
17+
| _ -> acc)
18+
| _ -> acc
19+
end
20+
21+
let rec get_samplers bindings acc =
22+
match bindings with
23+
| [] -> List.rev @@ List.flatten acc
24+
| binding :: rest ->
25+
get_samplers rest @@ (pattern_samplers#pattern binding.pvb_pat [] :: acc)
26+
27+
module Ast_builder = Ast_builder.Make (struct
28+
let loc = Location.none
29+
end)
30+
31+
let sampler_recorder s =
32+
let open Ast_builder in
33+
let create_samplers_registration samplers =
34+
let sampler_expr sampler =
35+
pexp_apply
36+
(evar @@ "Introspection.register_sampler")
37+
[ Nolabel,estring sampler
38+
; Nolabel,evar @@ "sample_" ^ sampler]
39+
in
40+
let samplers_registration = List.map sampler_expr samplers |> esequence in
41+
let register_toplevel =
42+
[ value_binding ~pat:punit ~expr:samplers_registration ]
43+
in
44+
pstr_value Nonrecursive register_toplevel
45+
in
46+
List.fold_right
47+
(fun si acc ->
48+
match si.pstr_desc with
49+
| Pstr_value (_, bindings) -> (
50+
match get_samplers bindings [] with
51+
| [] -> si :: acc
52+
| samplers -> si :: create_samplers_registration samplers :: acc)
53+
| _ -> si :: acc)
54+
s []
55+
56+
let expand = sampler_recorder

src/repo/learnocaml_precompile_exercise.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ let precompile ~exercise_dir =
6161
jsoo ~dir [] ~source:"exercise.cma" ~target:"exercise.js");
6262
(ocamlc ~dir ["-c";
6363
"-I"; "+compiler-libs";
64-
"-ppx"; Filename.concat !grading_ppx_dir "learnocaml-ppx-metaquot" ]
64+
"-ppx"; Filename.concat !grading_ppx_dir "learnocaml-ppx-metaquot"]
6565
~opn:["Learnocaml_callback"; "Prelude"; "Prepare"; "Test_lib.Open_me"]
6666
~source:["test.ml"]
6767
~target:"test.cmo"

src/toploop/toploop_unix.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ val flush_redirected_channel : redirection -> unit
3333
(** Flushes the channel and then cancel the redirection.
3434
The redirection must be the last one performed, otherwise an
3535
[Invalid_argument] will be raised.
36-
A stack of redirections is maintained for all fire descriptors. So
36+
A stack of redirections is maintained for all file descriptors. So
3737
the channel is then restored to either the previous redirection or
3838
to the original file descriptor. *)
3939
val stop_channel_redirection : redirection -> unit

0 commit comments

Comments
 (0)