Hacker News new | ask | show | jobs
by conradludgate 1703 days 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

1 comments

Thanks, 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)
  }