I could not find an answer to this simple question, maybe I used the wrong keywords to search.
To create an AST I need nodes such as Number, Add, Sub, Mul, Div and so forth. Since many math operations share the same structure, how can I treat them all under the same pattern matching case? E.g. the lines below are said to be not 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 intention is to be able 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
}