Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Prism -> Sorbet conversion for True, False, and If nodes #31

Merged
merged 3 commits into from
Jul 29, 2024

Conversation

egiurleo
Copy link

@egiurleo egiurleo commented Jul 24, 2024

Best read commit-by-commit.

Motivation

This PR implements the conversion between Prism and Sorbet for True, False, and If node types.

If functionality is limited at the moment -- else/elsif clauses are not supported yet, but I wanted to get feedback on my approach before doing more.

Closes #87
Closes #102
Closes #174

Test plan

See included automated tests.

egiurleo added 2 commits July 24, 2024 15:28
Relocate it to the `PM_STATEMENTS_NODE` case so it can be reused
by multiple parent nodes.

auto size = stmts->body.size;
return convertPrismToSorbet(reinterpret_cast<pm_node *>(programNode->statements), parser, gs);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow GitHub chose the most confusing way to display this diff. I have not changed anything about the handling of PM_RATIONAL_NODE. I've moved most of the logic that was under PM_PROGRAM_NODE into PM_STATEMENTS_NODE.

@@ -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) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a C++ question here -- the reason I removed this const is because I was having trouble reassigning ifTrue on line 185 below. Is there a better way to write the code below such that I wouldn't have to remove const here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it error because of the std::move(ifTrue) later?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It errors because of the ifTrue = convertPrismToSorbet... It turns out that methods declared as const return things that can't be re-assigned. I think removing const is the right move after all.

@egiurleo egiurleo marked this pull request as ready for review July 24, 2024 21:06
@egiurleo egiurleo self-assigned this Jul 24, 2024
@@ -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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it error because of the std::move(ifTrue) later?

@amomchilov amomchilov changed the title Implement Prism -> Sorbet conversion for True, False, and If nodes Implement Prism → Sorbet conversion for True, False, and If nodes Jul 25, 2024
@@ -0,0 +1,3 @@
# typed: strict

false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most beautiful program 🤩

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly we should have just stopped there 😆

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to move the result into ifTrue:

Suggested change
ifTrue = convertPrismToSorbet(reinterpret_cast<pm_node *>(ifNode->statements), parser, gs);
ifTrue = std::move(convertPrismToSorbet(reinterpret_cast<pm_node *>(ifNode->statements), parser, gs));

Not sure though, give it a shot

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gets me: Moving a temporary object prevents copy elision (fix available).

When I add back the const in the method definition, then the error becomes: Object of type 'std::unique_ptr<parser::Node>' cannot be assigned because its copy assignment operator is implicitly deleted regardless of whether or not I call std::move.

😫 C++ why!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChatGPT recommends removing const from the method definition so I might just keep that:

Remove const from the Return Type: The typical use case for returning a unique_ptr is to transfer ownership of the dynamically allocated object to the caller. The unique_ptr itself being const is not usually necessary or helpful because you want to allow the caller to own and possibly modify (or reassign) the pointer. The immutability of the pointed-to object (if necessary) should be expressed through the pointed-to type (e.g., unique_ptr), not through the unique_ptr itself.

@egiurleo egiurleo merged commit 44e414f into proj-parsing-w-prism-in-sorbet Jul 29, 2024
1 check passed
@egiurleo egiurleo deleted the emily/if-node branch July 29, 2024 22:07
@amomchilov amomchilov changed the title Implement Prism → Sorbet conversion for True, False, and If nodes Implement Prism -> Sorbet conversion for True, False, and If nodes Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants