Closed kjosib closed 1 year ago
Related: The support.foundation
module now includes a Visitor
base-class which implements a limited form of multiple dispatch. This means if your syntax tree nodes are instances of different classes, then you can easily write top-down compilation passes. That still carries the notation overhead of defining a bunch of classes, but at least it's easier to organize the code. Despite a few quirks, typing.NamedTuple
makes this fairly easy for most cases.
Experience with the Visitor
class shows that it's a reasonable answer.
Because LR parse-actions run bottom-up, computing synthesized attributes is easy but doing anything with what would be called an an inherited attribute requires another run over -- whatever data structure you built, bottom up!
If a simple generic driver could build a simple generic structure, then all manner of nice simple generic algorithms might be available.