Following https://github.com/AnyDSL/artic/pull/3 and https://github.com/AnyDSL/artic/projects/7, this PR adds the ability to use let in combination with if and while to do pattern-matching in place of a boolean condition. The patterns have to be refutable, meaning you cannot use this syntax to perform simple deconstruction (for that you are invited to use regular let statements, which have the opposite restriction: they must be trivial).
let x: (i32, i32) = ... ;
if let (0, five) = x {
foo();
} else { bar(); }
while let Some(x) = get_next() {
process(x);
}
Since this feature relies on the same pattern compiler that match uses, some re-factoring was performed: PtrnCompiler now has a MatchCase helper struct used in place of CaseExpr internally, this was done so that we did not have to generate dummy CaseExprs in the if let/while let cases, and also because CaseExpr owns ptrn and expr, while MatchCase does not, meaning IfLetExpr and WhileLetExpr can retain ownership of those two without any copying or extraneous moving. Additionally PtrnCompiler is now invoked via a static emit method which takes ownership of a vector of PtrnCompiler::MatchCases and perform redundancy checks.
Tests are provided for this functionality, and the coverage metrics are slightly better than on master.
Looks good. Please merge master into this branch (just run git merge master when on the branch) to resolve conflicts. Then I'll merge this PR into master.
Following https://github.com/AnyDSL/artic/pull/3 and https://github.com/AnyDSL/artic/projects/7, this PR adds the ability to use
let
in combination withif
andwhile
to do pattern-matching in place of a boolean condition. The patterns have to be refutable, meaning you cannot use this syntax to perform simple deconstruction (for that you are invited to use regularlet
statements, which have the opposite restriction: they must be trivial).Since this feature relies on the same pattern compiler that
match
uses, some re-factoring was performed:PtrnCompiler
now has aMatchCase
helper struct used in place ofCaseExpr
internally, this was done so that we did not have to generate dummyCaseExpr
s in theif let
/while let
cases, and also becauseCaseExpr
ownsptrn
andexpr
, whileMatchCase
does not, meaningIfLetExpr
andWhileLetExpr
can retain ownership of those two without any copying or extraneous moving. AdditionallyPtrnCompiler
is now invoked via a staticemit
method which takes ownership of a vector ofPtrnCompiler::MatchCase
s and perform redundancy checks.Tests are provided for this functionality, and the coverage metrics are slightly better than on master.