Ask HN: What data structure do I use for Lox AST in Rust? I'm working through the excellent Crafting Interpreters book and learning Rust at the same time by writing the interpreter in the first part of the book in Rust. For anyone who hasn't read the book, you totally should!, you implement an interpreter for a made up dynamically typed language called Lox. The scanner was relatively straightforward and now I'm having a hard time figuring out how I can implement a data structure to contain the AST for Lox. I'm pretty sure the core question is "How do I represent a tree of heterogeneous objects in Rust?" In the book he uses polymorphism. There is a base class of Expression, with subclasses for each expression type. At this point I think I'm holding Rust wrong and approaching the problem the wrong way. Potentially because I've used OOP for must of my life, with any functional-ish programming being pure functions, C#'s LINQ, and emulating concepts like free functions that act on data in C# or Java. This is the specific part of the book where I'm having trouble. http://craftinginterpreters.com/representing-code.html#implementing-syntax-trees I have the sense that I need to use a struct with an enum of expression type as the data field but I can't figure out to tie the data into it in a nice way. Is it as simple as having each variant of the enum contain the "complex" data structure that represents that expression kind? Can someone poke or prod me in the right direction? I'm doing this for the sole purpose of learning so I don't necessarily want to be given the answer. |