-
Notifications
You must be signed in to change notification settings - Fork 39
Inverted Match #379
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
Inverted Match #379
Conversation
LPTK
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but it's missing at least one test where this is actually used.
hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala
Outdated
Show resolved
Hide resolved
Co-authored-by: Lionel Parreaux <lionel.parreaux@gmail.com>
|
It's probably better to change the JSBuilder and not the IR, as this change make little sense with multiple branch. I will implement the change to JSBuilder directly in the handler branch as it is small enough. So we do not need to change the IR to allow inverted match then. |
|
But I don't think there's a compact/convenient way of representing that in the IR? Ideally we'd want to have such compact representation to facilitate analyses and optimizations. Now that I think of it, though, it seems that we should allow negative match cases regardless of the pattern, not just for literal patterns. |
|
Right now, I express it as |
|
We would associate a Using default branches technically works but isn't compact (can't merge several into the same match). Not sure it's that important, though. |
|
For a negative match, I think it's typically not used in multiple branches as the only useful scenario I can think of is for early exit, where you expect the match to sucess and return if it's not matched. |
|
Users can write things with negated patterns, eg fun test(x) = if x is
0 then "0"
~2 & ~3 then "not 2 and not 3"
~3 then "not 3"
_ then "other"
test(0)
//│ = "0"
test(1)
//│ = "not 2 and not 3"
test(2)
//│ = "not 3"
test(3)
//│ = "other"Though currently, it seems UCS lowering already does a good job producing a negation-free result: //│ JS (unsanitized):
//│ let test;
//│ test = function test(x2) {
//│ switch (x2) {
//│ case 0:
//│ return "0";
//│ break;
//│ case 2:
//│ return "not 3";
//│ break;
//│ case 3:
//│ return "other";
//│ break;
//│ default:
//│ return "not 2 and not 3";
//│ break;
//│ }
//│ }; |
Allow Match to be inverted, which will be used to create non null checks during handler lowering.