Skip to content

Commit

Permalink
Merge pull request #191 from Shopify/Alex/translate-local-vars
Browse files Browse the repository at this point in the history
Implement Prism -> Sorbet translation for local variables
  • Loading branch information
egiurleo authored Aug 22, 2024
2 parents b6d9981 + 28c552e commit 4ae8f69
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
21 changes: 19 additions & 2 deletions parser/prism/Translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,25 @@ std::unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {

return make_unique<parser::Kwrestarg>(parser.translateLocation(loc), gs.enterNameUTF8(name));
}
case PM_LOCAL_VARIABLE_READ_NODE: {
auto localVarReadNode = reinterpret_cast<pm_local_variable_read_node *>(node);
pm_location_t *loc = &localVarReadNode->base.location;

std::string_view name = parser.resolveConstant(localVarReadNode->name);

return make_unique<parser::LVar>(parser.translateLocation(loc), gs.enterNameUTF8(name));
}
case PM_LOCAL_VARIABLE_WRITE_NODE: {
auto localVarWriteNode = reinterpret_cast<pm_local_variable_write_node *>(node);
pm_location_t *loc = &localVarWriteNode->base.location;
pm_location_t *nameLoc = &localVarWriteNode->name_loc;

auto name = parser.resolveConstant(localVarWriteNode->name);
auto lhs = make_unique<parser::LVarLhs>(parser.translateLocation(nameLoc), gs.enterNameUTF8(name));
auto rhs = translate(localVarWriteNode->value);

return make_unique<parser::Assign>(parser.translateLocation(loc), std::move(lhs), std::move(rhs));
}
case PM_MODULE_NODE: { // Modules declarations, like `module A::B::C; ...; end`
auto moduleNode = reinterpret_cast<pm_module_node *>(node);
pm_location_t *loc = &moduleNode->base.location;
Expand Down Expand Up @@ -620,9 +639,7 @@ std::unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
case PM_LOCAL_VARIABLE_AND_WRITE_NODE:
case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE:
case PM_LOCAL_VARIABLE_OR_WRITE_NODE:
case PM_LOCAL_VARIABLE_READ_NODE:
case PM_LOCAL_VARIABLE_TARGET_NODE:
case PM_LOCAL_VARIABLE_WRITE_NODE:
case PM_MATCH_LAST_LINE_NODE:
case PM_MATCH_PREDICATE_NODE:
case PM_MATCH_REQUIRED_NODE:
Expand Down
31 changes: 31 additions & 0 deletions test/prism_regression/local_variables.parse-tree.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Begin {
stmts = [
Assign {
lhs = LVarLhs {
name = <U local_variable1>
}
rhs = Integer {
val = "123"
}
}
Assign {
lhs = LVarLhs {
name = <U local_variable2>
}
rhs = Send {
receiver = NULL
method = <U this_is_a_method_call>
args = [
]
}
}
Assign {
lhs = LVarLhs {
name = <U local_variable2>
}
rhs = LVar {
name = <U local_variable1>
}
}
]
}
8 changes: 8 additions & 0 deletions test/prism_regression/local_variables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# typed: true

local_variable1 = 123

local_variable2 = this_is_a_method_call
# ^^^^^^^^^^^^^^^^^^^^^ error: Method `this_is_a_method_call` does not exist on `T.class_of(<root>)`

local_variable2 = local_variable1 # should parse as local variable lookup

0 comments on commit 4ae8f69

Please sign in to comment.