From 76da0fc44b428b941853811aaee311ef3f760c5c Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 9 Aug 2024 16:49:01 -0400 Subject: [PATCH 1/2] Implement translation for local variable writes --- parser/prism/Translator.cc | 12 +++++++++- .../local_variables.parse-tree.exp | 23 +++++++++++++++++++ test/prism_regression/local_variables.rb | 6 +++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 test/prism_regression/local_variables.parse-tree.exp create mode 100644 test/prism_regression/local_variables.rb diff --git a/parser/prism/Translator.cc b/parser/prism/Translator.cc index f57b2d29fc6..76af91b2ac2 100644 --- a/parser/prism/Translator.cc +++ b/parser/prism/Translator.cc @@ -282,6 +282,17 @@ std::unique_ptr Translator::translate(pm_node_t *node) { return make_unique(parser.translateLocation(loc), gs.enterNameUTF8(name)); } + case PM_LOCAL_VARIABLE_WRITE_NODE: { + auto localVarWriteNode = reinterpret_cast(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.translateLocation(nameLoc), gs.enterNameUTF8(name)); + auto rhs = translate(localVarWriteNode->value); + + return make_unique(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(node); pm_location_t *loc = &moduleNode->base.location; @@ -622,7 +633,6 @@ std::unique_ptr Translator::translate(pm_node_t *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: diff --git a/test/prism_regression/local_variables.parse-tree.exp b/test/prism_regression/local_variables.parse-tree.exp new file mode 100644 index 00000000000..f060e6dbbc7 --- /dev/null +++ b/test/prism_regression/local_variables.parse-tree.exp @@ -0,0 +1,23 @@ +Begin { + stmts = [ + Assign { + lhs = LVarLhs { + name = + } + rhs = Integer { + val = "123" + } + } + Assign { + lhs = LVarLhs { + name = + } + rhs = Send { + receiver = NULL + method = + args = [ + ] + } + } + ] +} diff --git a/test/prism_regression/local_variables.rb b/test/prism_regression/local_variables.rb new file mode 100644 index 00000000000..53be7fd4111 --- /dev/null +++ b/test/prism_regression/local_variables.rb @@ -0,0 +1,6 @@ +# 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()` From 28c552e85ee6102501932539546635785cd019cf Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 9 Aug 2024 16:53:51 -0400 Subject: [PATCH 2/2] Implement translation for local variable reads --- parser/prism/Translator.cc | 9 ++++++++- test/prism_regression/local_variables.parse-tree.exp | 8 ++++++++ test/prism_regression/local_variables.rb | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/parser/prism/Translator.cc b/parser/prism/Translator.cc index 76af91b2ac2..99657cb32f0 100644 --- a/parser/prism/Translator.cc +++ b/parser/prism/Translator.cc @@ -282,6 +282,14 @@ std::unique_ptr Translator::translate(pm_node_t *node) { return make_unique(parser.translateLocation(loc), gs.enterNameUTF8(name)); } + case PM_LOCAL_VARIABLE_READ_NODE: { + auto localVarReadNode = reinterpret_cast(node); + pm_location_t *loc = &localVarReadNode->base.location; + + std::string_view name = parser.resolveConstant(localVarReadNode->name); + + return make_unique(parser.translateLocation(loc), gs.enterNameUTF8(name)); + } case PM_LOCAL_VARIABLE_WRITE_NODE: { auto localVarWriteNode = reinterpret_cast(node); pm_location_t *loc = &localVarWriteNode->base.location; @@ -631,7 +639,6 @@ std::unique_ptr 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: diff --git a/test/prism_regression/local_variables.parse-tree.exp b/test/prism_regression/local_variables.parse-tree.exp index f060e6dbbc7..ba7ab99918f 100644 --- a/test/prism_regression/local_variables.parse-tree.exp +++ b/test/prism_regression/local_variables.parse-tree.exp @@ -19,5 +19,13 @@ Begin { ] } } + Assign { + lhs = LVarLhs { + name = + } + rhs = LVar { + name = + } + } ] } diff --git a/test/prism_regression/local_variables.rb b/test/prism_regression/local_variables.rb index 53be7fd4111..6a1e85b3dca 100644 --- a/test/prism_regression/local_variables.rb +++ b/test/prism_regression/local_variables.rb @@ -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()` + +local_variable2 = local_variable1 # should parse as local variable lookup