Hello,
I’m writing a compiler in OCaml, and have finished implementing lexer, parser, type inference, for a small subset of my language.
Currently I’m implementing a borrow checker like Rust.
Unlike other languages which choose to lower their AST to a simpler form and then do semantic analysis on them, I’m currently doing all analysis on the raw AST, because I want to compile down to C. Not Assembly.
As of now, I have been successful, as OCaml allows us to write deeply nested mutually recursive code easily, however this has caused my design to be a monolithic blob of code.
For example, the type inference itself is [514 lines of recursive code (Github Link)]
While I’m okay with this for the type inference, this design has become somewhat prohibitive for more advanced semantic analysis, such as borrow checking, because this monolithic design,
- Forces me to do all tests in a single go, and I can not run smaller passes through the AST.
- Even if I want to implement smaller passes in future, I can not do it, because this design would prohibit it.
What can I do?
I do not want to rewrite the “walks” through the AST. I’m looking for a “visitors” pattern solution that will help me simplify my design.
I tried using the visitors library since it seemed promising, but since my AST is somewhat complicated, it does not work “out of the box”, as they say.
I’m currently looking at ppx_deriving library to see if I can use its simpler iter, fold methods to write a custom visitor for my AST.
Do you know of any other library or tutorial that I can use to write my own custom visitors?
Or any other sources that teaches how to simplify walks through ASTs in OCaml?
Thank You
~oxcrow