What are some approaches you guys use to interpret your parse tree?
you can use multimethods dispatching on node type
use a zipper to navigate the tree
or heck, just use https://clojure.github.io/clojure/clojure.walk-api.html
recommend using postwalk, and then writing a function to do whatever you’re thinking
depends on how you want to eval, it’s a wee bit harder for compilation, but not much. You just need to make sure you emit a string. One solution would be to treat it as a side effect of whatever function you use
admittedly, when I last did this I wasn’t using an instaparse’d parse tree, so maybe the technique varies a bit
think I re-implemented postwalk also
seems instaparse you could maybe just use insta/transform
which must do a walk of the tree, and takes a map of node type to function
@marcofiset insta/transform
has always been sufficient for my use cases. Is there more sophisticated functionality you're looking for? If so, clojure.walk/[pre|post]walk
might be the next step up.
I am not looking for anything particular, just wanted to start a discussion on the subject. I'm using multi methods for the moment and I was curious about what other people might be using.
I didn't know about insta/transform, I'll definitely take a look.
Yeah, instaparse.core/transform
is a simple function that does the trick for simple parse tree consumption.
Hey, that rhymes
@marcofiset Take a look at this section of the readme: https://github.com/engelberg/instaparse#transforming-the-tree
Wow, I'm really impressed with the arithmetic example! Very straightforward and simple. My multi methods solution is going to the trash and will be replaced by something similar 😃