# Evaluate OCaml bytecode

**URL:** <https://discuss.ocaml.org/t/evaluate-ocaml-bytecode/2161>\
**Category:** Community\
**Created:** [June 16, 2018, 11:36pm UTC](https://discuss.ocaml.org/t/evaluate-ocaml-bytecode/2161 "2018-06-16T23:36:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Nielsen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jason_nielsen/32/2097_2.png) [@Jason\_Nielsen](https://discuss.ocaml.org/u/Jason_Nielsen)\
**Post date:** [June 16, 2018, 11:36pm UTC](https://discuss.ocaml.org/t/evaluate-ocaml-bytecode/2161/1 "2018-06-16T23:36:35Z")

</div>

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,  
Jason

---

<div class="post-metadata">

**Author:** ![objmagic](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/objmagic/32/9_2.png) [@objmagic](https://discuss.ocaml.org/u/objmagic)\
**Post date:** [June 17, 2018, 6:32am UTC](https://discuss.ocaml.org/t/evaluate-ocaml-bytecode/2161/2 "2018-06-17T06:32:45Z")

</div>

what is the type of the evaluation result?

---

<div class="post-metadata">

**Author:** ![rizo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/rizo/32/93_2.png) [@rizo](https://discuss.ocaml.org/u/rizo)\
**Post date:** [June 17, 2018, 7:43am UTC](https://discuss.ocaml.org/t/evaluate-ocaml-bytecode/2161/3 "2018-06-17T07:43:45Z")

</div>

You might want to learn more about how PPXs work: [A Guide to PreProcessor eXtensions](https://ocamlverse.github.io/content/ppx.html).

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](https://github.com/johnwhitington/ocamli/) works, it includes an extension called `ppx_eval` which might do what you want.

---

<div class="post-metadata">

**Author:** ![Jason\_Nielsen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jason_nielsen/32/2097_2.png) [@Jason\_Nielsen](https://discuss.ocaml.org/u/Jason_Nielsen)\
**Post date:** [June 17, 2018, 3:18pm UTC](https://discuss.ocaml.org/t/evaluate-ocaml-bytecode/2161/4 "2018-06-17T15:18:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Juloo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/juloo/32/881_2.png) [@Juloo](https://discuss.ocaml.org/u/Juloo)\
**Post date:** [June 18, 2018, 7:48am UTC](https://discuss.ocaml.org/t/evaluate-ocaml-bytecode/2161/5 "2018-06-18T07:48:38Z")

</div>

You should look at the ocaml’s toplevel sources [https://github.com/ocaml/ocaml/blob/trunk/toplevel/toploop.ml](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/](https://opam.ocaml.org/packages/ocaml-compiler-libs/) library but the documentation is hard to find

---

<div class="post-metadata">

**Author:** ![Jason\_Nielsen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jason_nielsen/32/2097_2.png) [@Jason\_Nielsen](https://discuss.ocaml.org/u/Jason_Nielsen)\
**Post date:** [June 18, 2018, 12:43pm UTC](https://discuss.ocaml.org/t/evaluate-ocaml-bytecode/2161/6 "2018-06-18T12:43:22Z")

</div>

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.
