diff --git a/main/pipeline/pipeline.cc b/main/pipeline/pipeline.cc index 79b50d4262e..ec7793881ca 100644 --- a/main/pipeline/pipeline.cc +++ b/main/pipeline/pipeline.cc @@ -157,7 +157,7 @@ core::LocOffsets locOffset(pm_location_t *loc, pm_parser_t *parser) { return core::LocOffsets{locStart, locEnd}; } -const unique_ptr convertPrismToSorbet(pm_node_t *node, pm_parser_t *parser, core::GlobalState &gs) { +unique_ptr 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(node); @@ -171,6 +171,21 @@ const unique_ptr convertPrismToSorbet(pm_node_t *node, pm_parser_t return make_unique(locOffset(loc, parser), std::to_string(floatNode->value)); } + case PM_IF_NODE: { + auto ifNode = reinterpret_cast(node); + auto *loc = &ifNode->base.location; + + auto predicate = convertPrismToSorbet(ifNode->predicate, parser, gs); + + std::unique_ptr ifTrue; + + if (ifNode->statements != nullptr) { + ifTrue = convertPrismToSorbet(reinterpret_cast(ifNode->statements), parser, gs); + } + + // TODO: handle elsif/else clause + return make_unique(locOffset(loc, parser), std::move(predicate), std::move(ifTrue), nullptr); + } case PM_INTEGER_NODE: { auto intNode = reinterpret_cast(node); pm_location_t *loc = &intNode->base.location; @@ -297,7 +312,6 @@ const unique_ptr 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: diff --git a/test/prism_regression/if.parse-tree.exp b/test/prism_regression/if.parse-tree.exp new file mode 100644 index 00000000000..87ce4001a3e --- /dev/null +++ b/test/prism_regression/if.parse-tree.exp @@ -0,0 +1,6 @@ +If { + condition = True { + } + then_ = NULL + else_ = NULL +} diff --git a/test/prism_regression/if.rb b/test/prism_regression/if.rb new file mode 100644 index 00000000000..b77da0b04a1 --- /dev/null +++ b/test/prism_regression/if.rb @@ -0,0 +1,3 @@ +# typed: strict + +if true; end diff --git a/test/prism_regression/if_with_stmt.parse-tree.exp b/test/prism_regression/if_with_stmt.parse-tree.exp new file mode 100644 index 00000000000..6ae127f91fd --- /dev/null +++ b/test/prism_regression/if_with_stmt.parse-tree.exp @@ -0,0 +1,8 @@ +If { + condition = False { + } + then_ = Integer { + val = "5" + } + else_ = NULL +} diff --git a/test/prism_regression/if_with_stmt.rb b/test/prism_regression/if_with_stmt.rb new file mode 100644 index 00000000000..e8a87d8f6ab --- /dev/null +++ b/test/prism_regression/if_with_stmt.rb @@ -0,0 +1,5 @@ +# typed: static + +if false + 5 +end diff --git a/test/prism_regression/if_with_stmts.parse-tree.exp b/test/prism_regression/if_with_stmts.parse-tree.exp new file mode 100644 index 00000000000..20a8aaa50ed --- /dev/null +++ b/test/prism_regression/if_with_stmts.parse-tree.exp @@ -0,0 +1,17 @@ +If { + condition = True { + } + then_ = Begin { + stmts = [ + True { + } + Integer { + val = "4" + } + String { + val = + } + ] + } + else_ = NULL +} diff --git a/test/prism_regression/if_with_stmts.rb b/test/prism_regression/if_with_stmts.rb new file mode 100644 index 00000000000..7285cd0a5b0 --- /dev/null +++ b/test/prism_regression/if_with_stmts.rb @@ -0,0 +1,7 @@ +# typed: strict + +if true + true + 4 + "string" +end