Skip to content

Commit

Permalink
Move instruction generation of MatchRequiredNode back into Pass1
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
herwinw committed Jun 24, 2024
1 parent 50149e1 commit 7484ded
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
6 changes: 4 additions & 2 deletions lib/natalie/compiler/pass1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 8 additions & 16 deletions lib/natalie/compiler/transformers/match_required_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7484ded

Please sign in to comment.