Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/compile pattern matching #187

Merged
merged 12 commits into from
Nov 15, 2022
Merged

Conversation

b-studios
Copy link
Collaborator

@b-studios b-studios commented Nov 14, 2022

Over the weekend, I implemented a first draft of a match compiler. This should make life easier for all backend developers,
including LLVM (@phischu), JIT (@marzipankaiser), and MLTon (@JonathanStarup).

The match compiler is based on these notes by Jules Jacobs (@julesjacobs -- thanks a lot for these notes!):

https://julesjacobs.com/notes/patternmatching/patternmatching.pdf

Right now, the only implemented backend is JS (so LLVM and Chez are still missing for the tests to pass).

@@ -139,7 +139,7 @@ object Transformer {
case lifted.App(lifted.BlockVar(id), List(), args) =>
// TODO deal with BlockLit
id match {
case symbols.UserFunction(_, _, _, _, _, _, _) =>
case symbols.UserFunction(_, _, _, _, _, _, _) | symbols.TmpBlock(_) =>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The matching here on symbols here is a smell. What is it that you really need here?

};
transform(scrutinee).run { value =>
Switch(value, constructors.map {
constructor => clauses.find { _._1 == constructor } match {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be necessary anymore, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://llvm.org/docs/LangRef.html#id39

switch i32 %val, label %otherwise [ i32 0, label %onzero
                                    i32 1, label %onone
                                    i32 2, label %ontwo ]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The machine Switch just also needs a default label:

Currently:

case Switch(scrutinee: Variable, clauses: List[Clause])

Proposed:

case Switch(scrutinee: Variable, clauses: List[(Int, Clause)], default: Clause)

Note, that each clause also has an integer value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, unsurprisingly, nested matching now also works in LLVM:

image

@b-studios b-studios merged commit 64ed4eb into master Nov 15, 2022
@b-studios b-studios deleted the feature/compile-pattern-matching branch November 15, 2022 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants