Skip to content

Commit

Permalink
Merge pull request #214 from Shopify/emily/begin
Browse files Browse the repository at this point in the history
Implement translation for `PM_BEGIN_NODE`
  • Loading branch information
egiurleo authored Aug 23, 2024
2 parents c9cad6e + 5610242 commit 12e07c9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
19 changes: 18 additions & 1 deletion parser/prism/Translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ std::unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {

return make_unique<parser::Pair>(parser.translateLocation(loc), std::move(key), std::move(value));
}
case PM_BEGIN_NODE: {
auto beginNode = reinterpret_cast<pm_begin_node *>(node);
auto loc = &beginNode->base.location;

NodeVec statements;

if (beginNode->statements != nullptr) {
auto body = beginNode->statements->body;
auto prismStmts = absl::MakeSpan(body.nodes, body.size);
statements.reserve(prismStmts.size());

for (auto &prismStmt : prismStmts) {
statements.emplace_back(translate(prismStmt));
}
}

return make_unique<parser::Kwbegin>(parser.translateLocation(loc), std::move(statements));
}
case PM_BLOCK_ARGUMENT_NODE: { // A block arg passed into a method call, e.g. the `&b` in `a.map(&b)`
auto blockArg = reinterpret_cast<pm_block_argument_node *>(node);
auto loc = &blockArg->base.location;
Expand Down Expand Up @@ -577,7 +595,6 @@ std::unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
case PM_ARRAY_PATTERN_NODE:
case PM_ASSOC_SPLAT_NODE:
case PM_BACK_REFERENCE_READ_NODE:
case PM_BEGIN_NODE:
case PM_BLOCK_LOCAL_VARIABLE_NODE:
case PM_BREAK_NODE:
case PM_CALL_AND_WRITE_NODE:
Expand Down
21 changes: 21 additions & 0 deletions test/prism_regression/begin.parse-tree.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Begin {
stmts = [
Kwbegin {
stmts = [
Integer {
val = "1"
}
Integer {
val = "2"
}
Integer {
val = "3"
}
]
}
Kwbegin {
stmts = [
]
}
]
}
10 changes: 10 additions & 0 deletions test/prism_regression/begin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# typed: true

begin
1
2
3
end

begin
end

0 comments on commit 12e07c9

Please sign in to comment.