cultural_sublimation a écrit :
>
>
> Camels,
>
> Thank you everyone for your help. Now I know it should at least be
> possible to achieve what I want. However, I've never used camlp4
> (and I'm running Ocaml 3.10) so I'm afraid that some of your suggestions
> just went over my head. All I want is something as simple as this:
>
> ifdef DEBUG
> then Printf.printf "debug!\n"
> else ()
>
> Surely that camlp4 allows for this with just a few lines, no?
>
yes.
pa_macro is distributed with Objective Caml.
Here are all the allowed constructions added by pa_macro. It was copied
from the source file.
> At toplevel (structure item):
>
> DEFINE <uident>
> DEFINE <uident> = <expression>
> DEFINE <uident> (<parameters>) = <expression>
> IFDEF <uident> THEN <structure_items> [ ELSE <structure_items> ] (END | ENDIF)
> IFNDEF <uident> THEN <structure_items> [ ELSE <structure_items> ] (END | ENDIF)
> INCLUDE <string>
>
> At toplevel (signature item):
>
> DEFINE <uident>
> IFDEF <uident> THEN <signature_items> [ ELSE <signature_items> ] (END | ENDIF)
> IFNDEF <uident> THEN <signature_items> [ ELSE <signature_items> ] (END | ENDIF)
> INCLUDE <string>
>
> In expressions:
>
> IFDEF <uident> THEN <expression> [ ELSE <expression> ] (END | ENDIF)
> IFNDEF <uident> THEN <expression> [ ELSE <expression> ] (END | ENDIF)
> DEFINE <lident> = <expression> IN <expression>
> __FILE__
> __LOCATION__
>
> In patterns:
>
> IFDEF <uident> THEN <pattern> ELSE <pattern> (END | ENDIF)
> IFNDEF <uident> THEN <pattern> ELSE <pattern> (END | ENDIF)
>
> As Camlp4 options:
>
> -D<uident> or -D<uident>=expr define <uident> with optional value <expr>
> -U<uident> undefine it
> -I<dir> add <dir> to the search path for INCLUDE'd files
>
> After having used a DEFINE <uident> followed by "= <expression>", you
> can use it in expressions *and* in patterns. If the expression defining
> the macro cannot be used as a pattern, there is an error message if
> it is used in a pattern.
>
> You can also define a local macro in an expression usigng the DEFINE ... IN form.
> Note that local macros have lowercase names and can not take parameters.
>
> If a macro is defined to = NOTHING, and then used as an argument to a function,
> this will be equivalent to function taking one less argument. Similarly,
> passing NOTHING as an argument to a macro is equivalent to "erasing" the
> corresponding parameter from the macro body.
>
> The toplevel statement INCLUDE <string> can be used to include a
> file containing macro definitions and also any other toplevel items.
> The included files are looked up in directories passed in via the -I
> option, falling back to the current directory.
>
> The expression __FILE__ returns the current compiled file name.
> The expression __LOCATION__ returns the current location of itself.
With OCaml 3.10, -pp "camlp4o pa_macro.cmo" is also ok to have pa_macro
used to preprocess your files.
Salutations
Matt