Skip to content

Commit

Permalink
Add a ppx facility to test expressions (#403)
Browse files Browse the repository at this point in the history
* embed compiler-libs/pprintast into the grading environment

* add a ppx shortcut to avoid code duplication for printable_fun (cf. issue #379)

* add a ppx shortcut [%code e] to build the tuple (Code.(e), Solution.(e), [%expr e])
  • Loading branch information
lsylvestre committed Sep 17, 2021
1 parent 83630d5 commit 526bc07
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/grader/dune
Expand Up @@ -146,7 +146,8 @@
%{ocaml-config:standard_library}/compiler-libs/ast_mapper.cmi
%{ocaml-config:standard_library}/compiler-libs/parsetree.cmi
%{ocaml-config:standard_library}/compiler-libs/location.cmi
%{ocaml-config:standard_library}/compiler-libs/parse.cmi)
%{ocaml-config:standard_library}/compiler-libs/parse.cmi
%{ocaml-config:standard_library}/compiler-libs/pprintast.cmi)
(:generated-cmis
../ppx-metaquot/.ty.objs/byte/ty.cmi
../ppx-metaquot/.fun_ty.objs/byte/fun_ty.cmi
Expand Down
20 changes: 20 additions & 0 deletions src/ppx-metaquot/ppx_metaquot.ml
Expand Up @@ -274,6 +274,22 @@ module Main : sig val expander: string list -> Ast_mapper.mapper end = struct
fun_ty_next
(Typ.constr fun_ty_id [glob_cty_ty; ucty; ret])
| _ -> invalid_arg "fun_ty_of: not an arrow type"

let printable_of this e =
(* [%printable e] is a shortcut for
[Test_lib.printable_fun e (Pprintast.string_of_expression [%expr e])] *)
app (evar "Test_lib.printable_fun")
[app (evar "Pprintast.string_of_expression")
[(exp_lifter !loc this) # lift_Parsetree_expression e]; e]

let code_of this e =
(* [%code e] is a shortcut for [(Code.(e), Solution.(e), [%expr e])] *)
let open_module name e =
Exp.open_ (Opn.mk (Mod.ident (lid name))) e
in
tuple [open_module "Code" e;
open_module "Solution" e;
(exp_lifter !loc this) # lift_Parsetree_expression e]
(* ------ </edited for learn-ocaml> ------ *)

let expander _args =
Expand Down Expand Up @@ -303,6 +319,10 @@ module Main : sig val expander: string list -> Ast_mapper.mapper end = struct
ty_of this ty
| Pexp_extension({txt="funty";loc=l}, e) ->
fun_ty_of this l e
| Pexp_extension({txt="printable";loc=l}, e) ->
printable_of this (get_exp l e)
| Pexp_extension({txt="code";loc=l}, e) ->
code_of this (get_exp l e)
(* ------ </edited for learn-ocaml> ------ *)
| _ ->
super.expr this e
Expand Down

0 comments on commit 526bc07

Please sign in to comment.