Evaluate OCaml bytecode

Hi all,

Suppose I have a function called “dsl_to_ocaml_ast” that can create a valid OCaml Parsetree (ver 4.06.1). I’ve been digging around the OCaml source and can’t find any function that can evaluate/compile a Parsetree. I don’t fully understand the ppx extension interface but is this the only way to take a Parsetree and evaluate/compile it? I’ve played around a bit with “ppx_tools.metaquot” which seems to convince me that I can generate correct Parsetree’s. This tool also doesn’t seem to have an [%eval …] to evaluate the Parsetree’s that it generates. Hope I’m missing something simple.

Many thanks,

what is the type of the evaluation result?

You might want to learn more about how PPXs work: A Guide to PreProcessor eXtensions.

Evaluation of the AST in compile-time with PPXs is not a very common use case. If you do really need it I suggest looking at how ocamli works, it includes an extension called ppx_eval which might do what you want.

Great, thank you for the link.

In the future I’ll have to dig deeper into ppx extensions. The biggest problem I’ve had is the examples I’ve been able to find are either too simple or full on extensions so figuring out how to use it fully isn’t so straightforward.

You should look at the ocaml’s toplevel sources https://github.com/ocaml/ocaml/blob/trunk/toplevel/toploop.ml
Part of the compiler are accessible with the https://opam.ocaml.org/packages/ocaml-compiler-libs/ library but the documentation is hard to find

Thanks. I looked at toploop.ml before posting but didn’t see any obvious hooks at the parsetree level. I’ll have to take another look at it.