From 7484ded99eea5ad118baf35783b58b19bb989ad7 Mon Sep 17 00:00:00 2001 From: Herwin Date: Mon, 24 Jun 2024 10:54:52 +0200 Subject: [PATCH] Move instruction generation of MatchRequiredNode back into Pass1 The MatchRequiredNode transformer had a bit of feature envy for the compiler. With the previous bugfix and refactor, this code could be moved back into the compiler. --- lib/natalie/compiler/pass1.rb | 6 +++-- .../transformers/match_required_node.rb | 24 +++++++------------ 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/lib/natalie/compiler/pass1.rb b/lib/natalie/compiler/pass1.rb index 38deb3b4c..bc4cf8730 100644 --- a/lib/natalie/compiler/pass1.rb +++ b/lib/natalie/compiler/pass1.rb @@ -2005,8 +2005,10 @@ def transform_match_last_line_node(node, used:) end def transform_match_required_node(node, used:) - match_required_node_compiler = Transformers::MatchRequiredNode.new(self) - instructions = match_required_node_compiler.call(node) + match_required_node_compiler = Transformers::MatchRequiredNode.new + code_str = match_required_node_compiler.call(node) + parser = Natalie::Parser.new(code_str, @file.path, locals: current_locals) + instructions = transform_expression(parser.ast.statements, used: false) instructions << PushNilInstruction.new if used instructions end diff --git a/lib/natalie/compiler/transformers/match_required_node.rb b/lib/natalie/compiler/transformers/match_required_node.rb index 9649fcb90..14720f35c 100644 --- a/lib/natalie/compiler/transformers/match_required_node.rb +++ b/lib/natalie/compiler/transformers/match_required_node.rb @@ -4,23 +4,15 @@ module Natalie class Compiler module Transformers class MatchRequiredNode - attr_reader :compiler - - def initialize(compiler) - @compiler = compiler - end - def call(node) - code_str = case node.pattern.type - when :array_pattern_node - transform_array_pattern_node(node.pattern, node.value) - when :local_variable_target_node - transform_local_variable_target_node(node.pattern, node.value) - else - transform_eqeqeq_check(node.pattern, node.value) - end - parser = Natalie::Parser.new(code_str, compiler.file.path, locals: compiler.current_locals) - compiler.transform_expression(parser.ast.statements, used: false) + case node.pattern.type + when :array_pattern_node + transform_array_pattern_node(node.pattern, node.value) + when :local_variable_target_node + transform_local_variable_target_node(node.pattern, node.value) + else + transform_eqeqeq_check(node.pattern, node.value) + end end private