Last active
August 29, 2015 14:14
-
-
Save objmagic/b09aada6e06c15fe7734 to your computer and use it in GitHub Desktop.
ppx_playaround
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
build: clean | |
ocamlbuild -use-ocamlfind -package compiler-libs.common,ppx_tools.metaquot \ | |
ppx_vb.native | |
infer: | |
ocamlbuild -use-ocamlfind -package compiler-libs.common,ppx_tools.metaquot \ | |
ppx_vb.inferred.mli | |
test: build | |
ocamlopt -I lib -ppx ./ppx_vb.native ./test_vb.ml -o ./test_vb.native | |
clean: | |
rm -f *.cmi *.cmx *.o *.native |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Ast_mapper | |
open Ast_helper | |
open Asttypes | |
open Parsetree | |
open Longident | |
open Location | |
let get_vb stru_item = | |
match stru_item with | |
|{pstr_desc = | |
Pstr_value (Nonrecursive, | |
[{pvb_pat = {ppat_desc = Ppat_var {txt = id}}; | |
pvb_expr = _}])} -> Some id | |
| _ -> None | |
let stru_mapper = fun mapper stru_items -> | |
match stru_items with | |
| [] -> [] | |
| stru_item :: rs -> | |
match stru_item with | |
| {pstr_desc = Pstr_extension (({txt ="getvb"}, PStr [stru_item']), _)}-> | |
begin | |
match get_vb stru_item' with | |
| Some id -> stru_item' :: [%str let p () = print_endline id] | |
| None -> stru_item' :: rs | |
end | |
| {pstr_desc = _} -> stru_items | |
| _ -> failwith "error" | |
let getvb_mapper argv = | |
{default_mapper with structure = stru_mapper} | |
let () = Ast_mapper.register "getvb" getvb_mapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{pstr_desc = | |
Pstr_extension | |
(({txt = "getvb"}, | |
PStr | |
[{pstr_desc = | |
Pstr_value (Nonrecursive, | |
[{pvb_pat = {ppat_desc = Ppat_var {txt = "f"}}; | |
pvb_expr = | |
{pexp_desc = | |
Pexp_fun ("", None, {ppat_desc = Ppat_var {txt = "x"}}, | |
{pexp_desc = | |
Pexp_apply ({pexp_desc = Pexp_ident {txt = Lident "+"}}, | |
[("", {pexp_desc = Pexp_ident {txt = Lident "x"}}); | |
("", {pexp_desc = Pexp_constant (Const_int 1)})])})}}])}]), | |
...)}; | |
{pstr_desc = | |
Pstr_value (Nonrecursive, | |
[{pvb_pat = {ppat_desc = Ppat_construct ({txt = Lident "()"}, None)}; | |
pvb_expr = | |
{pexp_desc = | |
Pexp_apply ({pexp_desc = Pexp_ident {txt = Lident "p"}}, | |
[("", {pexp_desc = Pexp_construct ({txt = Lident "()"}, None)})])}}])}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let%getvb f x = x + 1 | |
let () = p () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment