Skip to content

Commit

Permalink
Implement translation for local variable reads
Browse files Browse the repository at this point in the history
  • Loading branch information
amomchilov authored and egiurleo committed Aug 22, 2024
1 parent 98961ee commit 85654b8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion parser/prism/Translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ 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;
Expand Down Expand Up @@ -622,7 +630,6 @@ 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_MATCH_LAST_LINE_NODE:
case PM_MATCH_PREDICATE_NODE:
Expand Down
8 changes: 8 additions & 0 deletions test/prism_regression/local_variables.parse-tree.exp
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@ Begin {
]
}
}
Assign {
lhs = LVarLhs {
name = <U local_variable2>
}
rhs = LVar {
name = <U local_variable1>
}
}
]
}
2 changes: 2 additions & 0 deletions test/prism_regression/local_variables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

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 85654b8

Please sign in to comment.