Skip to content

Instantly share code, notes, and snippets.

@objmagic
Last active August 29, 2015 14:14
Show Gist options
  • Save objmagic/9fda09b92d970d6d137a to your computer and use it in GitHub Desktop.
Save objmagic/9fda09b92d970d6d137a to your computer and use it in GitHub Desktop.
just playing with ppx
build:
ocamlbuild -use-ocamlfind -package compiler-libs.common,ppx_tools.metaquot \
ppx_component.native
infer:
ocamlbuild -use-ocamlfind -package compiler-libs.common,ppx_tools.metaquot \
ppx_component.inferred.mli
test: build
ocamlopt -I lib -ppx ./ppx_component.native ./test_ppx_component.ml -o ./test_ppx_component.native
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 = {pexp_desc = Pexp_fun (_, _,
{ppat_desc = Ppat_extension ({txt = "getvb"}, PStr [])},_)}}])} ->
Some id
| _ -> None
let stru_mapper = fun mapper stru_items ->
match stru_items with
| [] -> default_mapper.structure mapper stru_items
| stru_item :: _ ->
match get_vb stru_item with
| Some id -> let pr_stru = [%str let p () = print_endline id] in
(default_mapper.structure mapper stru_items) @ pr_stru
| None -> default_mapper.structure mapper stru_items
let getvb_mapper argv =
{default_mapper with structure = stru_mapper}
let () = Ast_mapper.register "getvb" getvb_mapper
let f[%getvb] x = x + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment