Skip to content

Commit

Permalink
Combine if and unless guard conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Oct 24, 2024
1 parent fb82456 commit 72f946e
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions parser/prism/Translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1347,30 +1347,29 @@ unique_ptr<parser::Node> Translator::patternTranslate(pm_node_t *node) {
auto inlineIfSingle = true;
auto statements = translateStatements(inNode->statements, inlineIfSingle);

if (prismPattern != nullptr && PM_NODE_TYPE_P(prismPattern, PM_IF_NODE)) {
auto ifNode = reinterpret_cast<pm_if_node *>(prismPattern);
auto prismStatements = reinterpret_cast<pm_statements_node *>(ifNode->statements);

if (prismStatements->body.size != 1) {
unreachable("In pattern-matching's `in` clause, an `if` guard must have a single statement.");
if (prismPattern != nullptr &&
(PM_NODE_TYPE_P(prismPattern, PM_IF_NODE) || PM_NODE_TYPE_P(prismPattern, PM_UNLESS_NODE))) {
pm_statements_node *prismStatements = nullptr;
unique_ptr<parser::Node> sorbetGuard;

if (PM_NODE_TYPE_P(prismPattern, PM_IF_NODE)) {
auto ifNode = down_cast<pm_if_node>(prismPattern);
prismStatements = ifNode->statements;
sorbetGuard = make_unique<parser::IfGuard>(location, translate(ifNode->predicate));
} else { // PM_UNLESS_NODE
auto unlessNode = down_cast<pm_unless_node>(prismPattern);
prismStatements = unlessNode->statements;
sorbetGuard = make_unique<parser::UnlessGuard>(location, translate(unlessNode->predicate));
}

auto sorbetPattern = patternTranslate(prismStatements->body.nodes[0]);
auto sorbetGuard = make_unique<parser::IfGuard>(location, translate(ifNode->predicate));

return make_unique<parser::InPattern>(location, move(sorbetPattern), move(sorbetGuard), move(statements));
} else if (prismPattern != nullptr && PM_NODE_TYPE_P(prismPattern, PM_UNLESS_NODE)) {
auto unlessNode = reinterpret_cast<pm_unless_node *>(prismPattern);
auto prismStatements = reinterpret_cast<pm_statements_node *>(unlessNode->statements);

if (prismStatements->body.size != 1) {
unreachable("In pattern-matching's `in` clause, an `unless` guard must have a single statement.");
unreachable("In pattern-matching's `in` clause, a guard must have a single statement.");
}

auto sorbetPattern = patternTranslate(prismStatements->body.nodes[0]);
auto sorbetGuard = make_unique<parser::UnlessGuard>(location, translate(unlessNode->predicate));

return make_unique<parser::InPattern>(location, move(sorbetPattern), move(sorbetGuard), move(statements));
return make_unique<parser::InPattern>(location, move(sorbetPattern), move(sorbetGuard),
move(statements));
} else {
auto sorbetPattern = patternTranslate(prismPattern);

Expand Down

0 comments on commit 72f946e

Please sign in to comment.