All examples must be compiled using ``camlp4''. As they use the
``EXTEND'' instruction, Camlp4 must load
``pa_extend.cmo''. As they use the quotations to create
abstract syntax tree nodes, Camlp4 must load ``q_MLast.cmo''. The
compilation command is therefore:
ocamlc -pp "camlp4o pa_extend.cmo q_MLast.cmo" -I camlp4-lib-dir -c file.ml
After this, the syntax extension takes place in Camlp4 when
``file.cmo'' is loaded.
This is an example to add the infix operator ``o'', composition
of two functions. For the meaning of the quotation expr used
here, see appendix A.
open Pcaml;;
EXTEND
expr: AFTER "apply"
[[ f = expr; "o"; g = expr -> <:expr< fun x -> $f$ ($g$ x) >> ]];
END;;
The ``repeat...until'' loop of Pascal is closed to the ``while'' loop except that it is executed at least once. We can implement it like this:
open Pcaml;;
EXTEND
expr: LEVEL "let"
[[ "repeat"; e1 = expr; "until"; e2 = expr ->
<:expr< do $e1$; return while not $e2$ do $e1$; done >> ]];
END;;