20 ms·
Enums are almost certainly the way to go here. Something like enum Expr { Literal(Literal), Binary(Op, Box<Expr>, Box<Expr>), //...
by conradludgate 5y ago
Enums are almost certainly the way to go here. Something like
enum Expr {
Literal(Literal),
Binary(Op, Box<Expr>, Box<Expr>),
//...
}
A value with type of Expr could be a literal or a binary expression. Binary expressions contain an operator and two other expressions (boxed to prevent infinite sized types).
This is the building block for your tree
- backslash_16 5y agoThanks, this makes a lot of sense. I'd like to think I was slowly closing in on this. Mine isn't as clean as what you put but I made the data within each of the enum possibilities a discrete struct. pub enum Expr { Binary(BinaryExpr), Grouping(GroupingExpr), LiteralBool(bool), LiteralNil, LiteralNumber(f32), LiteralString(String), Unary(UnaryExpr) }