In a previous article, we played around with implementing a tiny programming language for generating random art. In that article and the accompanying code, we implemented a trick I learned from this post, specifically matklad’s response. In learning more about programming language implementation, I came across this wonderful post by Professor Adrian Sampson, which examines the effects of flattening the abstract syntax tree of a similar little language. I thought it’d be interesting to perform a similar analysis with our random art language implementation, so I put together a Rust project with four different implementations.
As mentioned last time, it can be more efficient store a single pointer (i.e. Box), which my code confusingly calls the “branch” version (apologies, but I wanted the names to be in alphabetical order and it was the best I could come up with). In this version, the main Expr looks like
After that, we move to a flat version similar to the one explored in Dr. Sampson’s post. In this, we go back to the basic structure, wherein we store (potentially) multiple things in a single Expr variant: