Skip to content

Commit

Permalink
Implement translation for alternation patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
amomchilov committed Oct 10, 2024
1 parent b6c780e commit 66be04b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
18 changes: 13 additions & 5 deletions parser/prism/Translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1008,14 +1008,14 @@ unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
return make_unique<parser::Yield>(location, move(yieldArgs));
}

case PM_ARRAY_PATTERN_NODE: // An array pattern such as the `[head, *tail]` in the `a in [head, *tail]`
case PM_FIND_PATTERN_NODE: // A find pattern such as the `[*, middle, *]` in the `a in [*, middle, *]`
case PM_HASH_PATTERN_NODE: // An hash pattern such as the `{ k: Integer }` in the `h in { k: Integer }`
case PM_IN_NODE: // An `in` pattern such as in a `case` statement, or as a standalone expression.
case PM_ALTERNATION_PATTERN_NODE: // A pattern like `1 | 2`
case PM_ARRAY_PATTERN_NODE: // An array pattern such as the `[head, *tail]` in the `a in [head, *tail]`
case PM_FIND_PATTERN_NODE: // A find pattern such as the `[*, middle, *]` in the `a in [*, middle, *]`
case PM_HASH_PATTERN_NODE: // An hash pattern such as the `{ k: Integer }` in the `h in { k: Integer }`
case PM_IN_NODE: // An `in` pattern such as in a `case` statement, or as a standalone expression.
unreachable(
"These pattern-match related nodes are handled separately in `Translator::patternTranslate()`.");

case PM_ALTERNATION_PATTERN_NODE:
case PM_BACK_REFERENCE_READ_NODE:
case PM_BLOCK_LOCAL_VARIABLE_NODE:
case PM_CAPTURE_PATTERN_NODE:
Expand Down Expand Up @@ -1097,6 +1097,14 @@ unique_ptr<parser::Node> Translator::patternTranslate(pm_node_t *node) {
auto location = translateLoc(node->location);

switch (PM_NODE_TYPE(node)) {
case PM_ALTERNATION_PATTERN_NODE: { // A pattern like `1 | 2`
auto alternationPatternNode = reinterpret_cast<pm_alternation_pattern_node *>(node);

auto left = translate(alternationPatternNode->left);
auto right = translate(alternationPatternNode->right);

return make_unique<parser::MatchAlt>(location, std::move(left), std::move(right));
}
case PM_ARRAY_PATTERN_NODE: { // An array pattern such as the `[head, *tail]` in the `a in [head, *tail]`
auto arrayPatternNode = reinterpret_cast<pm_array_pattern_node *>(node);

Expand Down
20 changes: 20 additions & 0 deletions test/prism_regression/case_match.parse-tree.exp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ Begin {
]
}
}
InPattern {
pattern = MatchAlt {
left = Integer {
val = "3"
}
right = Integer {
val = "4"
}
}
guard = NULL
body = Send {
receiver = NULL
method = <U puts>
args = [
String {
val = <U three or four!>
}
]
}
}
]
elseBody = Send {
receiver = NULL
Expand Down
2 changes: 2 additions & 0 deletions test/prism_regression/case_match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
puts "one!"
in 2
puts "two!"
in 3 | 4
puts "three or four!"
else
puts "Who knows!"
end
Expand Down

0 comments on commit 66be04b

Please sign in to comment.