I am happy to announce the first release of
metapp, yet another preprocessor for OCaml. Similarly to
metapp is a PPX rewriter. But instead of introducing a specific DSL for preprocessor directives,
metapp provides a
[%meta ...] extension, where the dots
... are arbitrary OCaml expressions that are substituted at compile-time by the AST nodes they evaluate into. These expressions build AST nodes either by (anti-)quoting some code directly, or by using
let option_get o = [%meta if Sys.ocaml_version >= "4.08.0" then [%e Option.get o] else [%e match o with | None -> invalid_arg "option_get" | Some x -> x]]
In this example, the code between
[%e ... ] is “anti-quoted”: it is the code that is inserted (conditionally) in the rewritten module. Of course, the anti-quoted code can contain itself some
[%meta ...] code.
[%meta ...] can even itself contain other levels of
[%meta ...] code for multi-stage programming.
An example of usage of
metapp is the
metaquot package, which implements the same quoters as
[%type: ...], etc. These quoters are implemented by meta-programming: the meta-code introspects
compiler-libs to generate the code matching the current OCaml version.