I could not find the answer to this simple question, maybe I used the wrong keywords for the search.
To create an AST, I need nodes like Number, Add, Sub, Mul, Div, etc. Since many mathematical operations have the same structure, how can I handle all of them within the same template? For instance. the lines below are not said to be syntactically correct:
object AST { sealed abstract class Expr case class MathOp(e1: Expr, e2: Expr) extends Expr case class Number extends Expr case class Add(e1: Expr, e2: Expr) extends MathOp(e1, e2) case class Sub(e1: Expr, e2: Expr) extends MathOp(e1, e2) }
The goal is to do:
expr match { case MathOp(e1: Expr, e2: Expr) => //do something that would be done to Add, Sub, Mul, Div case Number => //do another thing }
user445107
source share