forked from sorbet/sorbet
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #310 from Shopify/translate-pattern-matching-opera…
…tors Translate pattern matching operators
- Loading branch information
Showing
3 changed files
with
310 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
266 changes: 266 additions & 0 deletions
266
test/prism_regression/pattern_matching_operators.parse-tree.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,266 @@ | ||
Begin { | ||
stmts = [ | ||
MatchPatternP { | ||
lhs = Send { | ||
receiver = NULL | ||
method = <U match> | ||
args = [ | ||
] | ||
} | ||
rhs = Integer { | ||
val = "1" | ||
} | ||
} | ||
MatchPatternP { | ||
lhs = Send { | ||
receiver = NULL | ||
method = <U no_match> | ||
args = [ | ||
] | ||
} | ||
rhs = ArrayPattern { | ||
elts = [ | ||
Integer { | ||
val = "1" | ||
} | ||
Integer { | ||
val = "2" | ||
} | ||
] | ||
} | ||
} | ||
MatchPatternP { | ||
lhs = Array { | ||
elts = [ | ||
Send { | ||
receiver = NULL | ||
method = <U match_1> | ||
args = [ | ||
] | ||
} | ||
Send { | ||
receiver = NULL | ||
method = <U match_2> | ||
args = [ | ||
] | ||
} | ||
] | ||
} | ||
rhs = ArrayPattern { | ||
elts = [ | ||
Integer { | ||
val = "1" | ||
} | ||
Integer { | ||
val = "2" | ||
} | ||
] | ||
} | ||
} | ||
MatchPatternP { | ||
lhs = Integer { | ||
val = "1" | ||
} | ||
rhs = MatchVar { | ||
name = <U var> | ||
} | ||
} | ||
MatchPatternP { | ||
lhs = Array { | ||
elts = [ | ||
Integer { | ||
val = "1" | ||
} | ||
Integer { | ||
val = "2" | ||
} | ||
] | ||
} | ||
rhs = ArrayPattern { | ||
elts = [ | ||
MatchVar { | ||
name = <U var_a> | ||
} | ||
MatchVar { | ||
name = <U var_b> | ||
} | ||
] | ||
} | ||
} | ||
If { | ||
condition = Send { | ||
receiver = LVar { | ||
name = <U d> | ||
} | ||
method = <U ==> | ||
args = [ | ||
Send { | ||
receiver = LVar { | ||
name = <U c> | ||
} | ||
method = <U *> | ||
args = [ | ||
Integer { | ||
val = "2" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
then_ = MatchPatternP { | ||
lhs = Array { | ||
elts = [ | ||
Integer { | ||
val = "3" | ||
} | ||
Integer { | ||
val = "4" | ||
} | ||
] | ||
} | ||
rhs = ArrayPattern { | ||
elts = [ | ||
MatchVar { | ||
name = <U c> | ||
} | ||
MatchVar { | ||
name = <U d> | ||
} | ||
] | ||
} | ||
} | ||
else_ = NULL | ||
} | ||
MatchPattern { | ||
lhs = Send { | ||
receiver = NULL | ||
method = <U match> | ||
args = [ | ||
] | ||
} | ||
rhs = Integer { | ||
val = "10" | ||
} | ||
} | ||
MatchPattern { | ||
lhs = Send { | ||
receiver = NULL | ||
method = <U no_match> | ||
args = [ | ||
] | ||
} | ||
rhs = ArrayPattern { | ||
elts = [ | ||
Integer { | ||
val = "10" | ||
} | ||
Integer { | ||
val = "20" | ||
} | ||
] | ||
} | ||
} | ||
MatchPattern { | ||
lhs = Array { | ||
elts = [ | ||
Send { | ||
receiver = NULL | ||
method = <U match_1> | ||
args = [ | ||
] | ||
} | ||
Send { | ||
receiver = NULL | ||
method = <U match_2> | ||
args = [ | ||
] | ||
} | ||
] | ||
} | ||
rhs = ArrayPattern { | ||
elts = [ | ||
Integer { | ||
val = "10" | ||
} | ||
Integer { | ||
val = "20" | ||
} | ||
] | ||
} | ||
} | ||
MatchPattern { | ||
lhs = Integer { | ||
val = "10" | ||
} | ||
rhs = MatchVar { | ||
name = <U var_c> | ||
} | ||
} | ||
MatchPattern { | ||
lhs = Array { | ||
elts = [ | ||
Integer { | ||
val = "10" | ||
} | ||
Integer { | ||
val = "20" | ||
} | ||
] | ||
} | ||
rhs = ArrayPattern { | ||
elts = [ | ||
MatchVar { | ||
name = <U var_c> | ||
} | ||
MatchVar { | ||
name = <U var_d> | ||
} | ||
] | ||
} | ||
} | ||
If { | ||
condition = Send { | ||
receiver = LVar { | ||
name = <U var_d> | ||
} | ||
method = <U ==> | ||
args = [ | ||
Send { | ||
receiver = LVar { | ||
name = <U var_c> | ||
} | ||
method = <U *> | ||
args = [ | ||
Integer { | ||
val = "2" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
then_ = MatchPattern { | ||
lhs = Array { | ||
elts = [ | ||
Integer { | ||
val = "30" | ||
} | ||
Integer { | ||
val = "40" | ||
} | ||
] | ||
} | ||
rhs = ArrayPattern { | ||
elts = [ | ||
MatchVar { | ||
name = <U var_c> | ||
} | ||
MatchVar { | ||
name = <U var_d> | ||
} | ||
] | ||
} | ||
} | ||
else_ = NULL | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# typed: false | ||
|
||
# matching expressions to patterns | ||
match in 1 | ||
no_match in 1, 2 | ||
[match_1, match_2] in 1, 2 | ||
|
||
# assigning expressions to patterns | ||
1 in var | ||
[1, 2] in var_a, var_b | ||
|
||
# guards don't work outside of case-based pattern matching | ||
# so this should be normal conditional assignment | ||
[3, 4] in c, d if d == c*2 | ||
|
||
|
||
# matching expressions to patterns | ||
match => 10 | ||
no_match => 10, 20 | ||
[match_1, match_2] => 10, 20 | ||
|
||
# assigning expressions to patterns | ||
10 => var_c | ||
[10, 20] => var_c, var_d | ||
|
||
# guards don't work outside of case-based pattern matching | ||
# so this should be normal conditional assignment | ||
[30, 40] => var_c, var_d if var_d == var_c*2 |