I'm trying to dump type information of all identifiers in an OCaml project, basically it's the same as traversing the typed abstract syntax tree(https://github.com/ocaml/ocaml/blob/trunk/typing/typedtree.mli). Since I'm new to OCaml compiler's codebase, I'm not sure whether the Compiler has provided apis so we could easily write a plugin to do the job or we have to hack the compiler code? Also how does this interact with OCamlbuild? Thanks for any hints or advices.
相关问题
- How do shader compilers work?
- C++ Builder - Difference between Lib and Res
- Writing an interpreter in OCaml [closed]
- Using Core.Std.List.fold_left without label
- Why does constructing std::string(0) not emit a co
相关文章
- How to force Delphi compiler to display all hints
- Xcode - How to change application/project name : w
- What other neat tricks does the SpecialNameAttribu
- What is the purpose of “-Wa,option” in GCC? [close
- How do I generate an AST from a string of C++ usin
- Why does the C++ compiler give errors after lines
- Can a compiler inline a virtual function if I use
- Which x86 C++ compilers are multithreaded by itsel
OCaml provides its own compiler as a library named
compiler-libs
. It has everything in it, allowing one to move from a concrete syntax towards executable, with all intermidiate steps under your control, including typedtree, of course.The bad news is that it is not documented. I would suggest you, to use
utop
ormerlin
to explore this library.You do not need anything special to do with ocamlbuild to use
compiler-libs
, it is a regular library.Assuming you have already got a typed AST of type
structure
somehow.The classic way is simply to write a big recursive function to traverse the AST by yourself.
But now there is module
TypedtreeIter
available in OCaml compiler source code and it is exposed tocompiler-libs
. For simple traversal, this is very handy.TypedtreeIter
provides a functor to build your own iterator over typed ASTs. Here is a very simple example to print all the pattern identifiers with their types:Module type
TypedtreeIter.IteratorArgument
is to specify what your iterator does for each AST construct. You have two points to execute your job: when the traversal enters a construct and when it exits from it. Forpattern
, for example, you haveenter_pattern
andexit_pattern
. You do not need to worry about the recursion traversal itself: it is the job of the functorMakeIterator
. Giving anIteratorArgument
module it wires up all theenter_*
andexit_*
recursively and returns a module with bunch of iterators.Normally you are interested only in some part of AST and want to skip the others.
DefaultIteratorArgument
is a module whoseenter_*
andexit_*
do nothing. YourIteratorArgument
module should includeDefaultIteratorArgument
to inherit this default behaviour, then implement only the parts which do something special.If you want not only to traverse typed ASTs but also to modify some part of them, use
TypedtreeMap
instead ofTypedtreeIter
. There is a small example ofTypedtreeMap
at https://bitbucket.org/camlspotter/compiler-libs-hack/src/340072a7c14cbce624b98a57bf8c4c6509c40a31/overload/mod.ml?at=default.(I do not use ocamlbuild, so I cannot help that point.)