3 ms·
And yet, it's a lot of boilerplate to write recursive ast walkers manually. ADTs are not enough, some (ideally, compile-time) reflection is also needed.
by sklogic 11y ago
And yet, it's a lot of boilerplate to write recursive ast walkers manually. ADTs are not enough, some (ideally, compile-time) reflection is also needed.
- ufo 11y agoThats true, reflection tools like Haskell's Uniplate and SYB are very helpful. But its still possible to get very far with just ADTs if you use "parameterized functors" instead of recursive data types for the syntax tree and write some reusable fold functions for the walkers. Another neat thing about this approach is that its super easy to add metadata to the syntax tree (for example, line numbers for error messages). http://lambda-the-ultimate.org/node/4170#comment-63836 http://lambda-the-ultimate.org/node/4170#comment-63836
- pjmlp 11y agoThat is why for prototyping it is much better to use tools like JavaCC, ANTLR, attribute grammars,... than doing everything from scratch.
- sklogic 11y agoNone of these helps with implementing AST visitors. I was rather talking about things like Scrap Your Boilerplate or Nanopass.
- pjmlp 11y agoThey are able to generate part of the AST visitors boilerplate. For example, JJTree on JavaCC.
- sklogic 11y agoIt's not very useful - it's generating only one visitor structure, while you'd need many for different passes. E.g., a visitor for extracting bound and free identifiers lists would be different from a visitor for lowering various syntax sugar.
- pjmlp 11y agoAh ok, but you could also do the multiple passes at once, at least in some cases. I am talking about prototyping, not going with a state of art implementation.
- sklogic 11y agoDoing multiple passes at once is a (premature) optimisation. For prototyping you'd rather split the process into as small and simple passes as possible.