Excerpt from draft of code using Structured Data
class Node(adt.Sum): __slots__ = () Inner: adt.Ctor[BinOp, Node, Node] Leaf: adt.Ctor[int] @match.function def eval(self) -> int: """Evaluate this node to an int.""" @Node.eval.when(self=Node.Inner(match.pat.opr, match.pat.left, match.pat.right)) def _eval_inner(opr, left, right): return opr(left.eval(), right.eval()) @Node.eval.when(self=Node.Leaf(match.pat.value)) def _eval_leaf(value): return value
Comments