Skip to content

Commit

Permalink
Implement Prism -> Sorbet conversion for if node
Browse files Browse the repository at this point in the history
  • Loading branch information
egiurleo committed Jul 24, 2024
1 parent c90d7f8 commit 867ee7d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
20 changes: 18 additions & 2 deletions main/pipeline/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ core::LocOffsets locOffset(pm_location_t *loc, pm_parser_t *parser) {
return core::LocOffsets{locStart, locEnd};
}

const unique_ptr<parser::Node> convertPrismToSorbet(pm_node_t *node, pm_parser_t *parser, core::GlobalState &gs) {
unique_ptr<parser::Node> convertPrismToSorbet(pm_node_t *node, pm_parser_t *parser, core::GlobalState &gs) {
switch (PM_NODE_TYPE(node)) {
case PM_FALSE_NODE: {
auto falseNode = reinterpret_cast<pm_false_node *>(node);
Expand All @@ -171,6 +171,23 @@ const unique_ptr<parser::Node> convertPrismToSorbet(pm_node_t *node, pm_parser_t

return make_unique<parser::Float>(locOffset(loc, parser), std::to_string(floatNode->value));
}
case PM_IF_NODE: {
auto ifNode = reinterpret_cast<pm_if_node *>(node);
auto *loc = &ifNode->base.location;

auto condition = convertPrismToSorbet(ifNode->predicate, parser, gs);

std::unique_ptr<parser::Node> ifTrue;

if (ifNode->statements == nullptr) {
ifTrue = nullptr;
} else {
ifTrue = convertPrismToSorbet(reinterpret_cast<pm_node *>(ifNode->statements), parser, gs);
}

// TODO: handle elsif/else clause
return make_unique<parser::If>(locOffset(loc, parser), std::move(condition), std::move(ifTrue), nullptr);
}
case PM_INTEGER_NODE: {
auto intNode = reinterpret_cast<pm_integer_node *>(node);
pm_location_t *loc = &intNode->base.location;
Expand Down Expand Up @@ -297,7 +314,6 @@ const unique_ptr<parser::Node> convertPrismToSorbet(pm_node_t *node, pm_parser_t
case PM_GLOBAL_VARIABLE_WRITE_NODE:
case PM_HASH_NODE:
case PM_HASH_PATTERN_NODE:
case PM_IF_NODE:
case PM_IMAGINARY_NODE:
case PM_IMPLICIT_NODE:
case PM_IMPLICIT_REST_NODE:
Expand Down
6 changes: 6 additions & 0 deletions test/prism_regression/if.parse-tree.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
If {
condition = True {
}
then_ = NULL
else_ = NULL
}
3 changes: 3 additions & 0 deletions test/prism_regression/if.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# typed: strict

if true; end
8 changes: 8 additions & 0 deletions test/prism_regression/if_with_stmt.parse-tree.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
If {
condition = False {
}
then_ = Integer {
val = "5"
}
else_ = NULL
}
5 changes: 5 additions & 0 deletions test/prism_regression/if_with_stmt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# typed: static

if false
5
end
17 changes: 17 additions & 0 deletions test/prism_regression/if_with_stmts.parse-tree.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
If {
condition = True {
}
then_ = Begin {
stmts = [
True {
}
Integer {
val = "4"
}
String {
val = <U string>
}
]
}
else_ = NULL
}
7 changes: 7 additions & 0 deletions test/prism_regression/if_with_stmts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# typed: strict

if true
true
4
"string"
end

0 comments on commit 867ee7d

Please sign in to comment.