bosatsu
bosatsu copied to clipboard
Improving pattern compilation
See: https://compiler.club/compiling-pattern-matching/
Currently in Matchless we compile pattern matches into nest if/else. But a better algorithm can pretty much always compile into a switch which can be encoded in Python with a dict or directly in C or Java.
One nice thing would be automatic expanding out tuple matches:
match (a, b, c):
case (Foo(x), Bar(_), Baz(_)): ..
could be nested to avoid the allocation and deallocation of the intermediate tuple:
match a:
case Foo(x):
match b:
...
To do this, we have to make sure we expand out the product match in the correct order.