Skip to content

Commit

Permalink
Merge pull request #2398 from herwinw/transform_locals
Browse files Browse the repository at this point in the history
Move VariableDeclareInstructions to beginning of block
  • Loading branch information
herwinw committed Dec 16, 2024
2 parents 69f562a + c885123 commit d97f269
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions lib/natalie/compiler/pass1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,9 @@ def transform_block_node(node, used:, is_lambda:)
instructions << transform_block_args(node.parameters, used: true)
end

with_locals(node.locals) do
instructions += with_locals(node.locals) do
body = node.body || Prism.nil_node(location: node.location)
instructions << transform_expression(body, used: true)
transform_expression(body, used: true)
end

instructions << EndInstruction.new(:define_block)
Expand Down Expand Up @@ -833,8 +833,8 @@ def transform_class_node(node, used:)
file: @file.path,
line: node.location.start_line,
)
with_locals(node.locals) do
instructions += transform_body(node.body, used: true, location: node.location)
instructions += with_locals(node.locals) do
transform_body(node.body, used: true, location: node.location)
end
instructions << EndInstruction.new(:define_class)
instructions << PopInstruction.new unless used
Expand Down Expand Up @@ -1318,7 +1318,6 @@ def transform_defn_args(node, used:, for_block: false, check_args: true, local_o

args_compiler = Args.new(node:, pass: self, check_args:, local_only:, for_block:)
instructions << args_compiler.transform
instructions += locals.map { |name| VariableDeclareInstruction.new(name) }
instructions
end

Expand Down Expand Up @@ -1382,8 +1381,6 @@ def transform_for_declare_args(args)
instructions = []

case args.type
when :local_variable_target_node
instructions << VariableDeclareInstruction.new(args.name)
when :multi_target_node
targets = args.lefts + [args.rest].compact + args.rights
targets.each do |arg|
Expand All @@ -1396,6 +1393,7 @@ def transform_for_declare_args(args)
:implicit_rest_node,
:index_target_node,
:instance_variable_target_node,
:local_variable_target_node,
:splat_node
:noop
else
Expand All @@ -1406,8 +1404,7 @@ def transform_for_declare_args(args)
end

def transform_for_node(node, used:)
instructions = @locals_stack.fetch(-1, []).map { |name| VariableDeclareInstruction.new(name) }
instructions += transform_for_declare_args(node.index)
instructions = transform_for_declare_args(node.index)
instructions << DefineBlockInstruction.new(arity: 1)
instructions += transform_block_args_for_for(node.index, used: true)
instructions += transform_expression(node.statements, used: true) if node.statements
Expand Down Expand Up @@ -2081,10 +2078,7 @@ def transform_local_variable_read_node(node, used:)
end

def transform_local_variable_write_node(node, used:)
instructions = [
VariableDeclareInstruction.new(node.name),
transform_expression(node.value, used: true),
]
instructions = transform_expression(node.value, used: true)
instructions << DupInstruction.new if used
instructions << VariableSetInstruction.new(node.name)
instructions
Expand Down Expand Up @@ -2213,8 +2207,8 @@ def transform_module_node(node, used:)
file: @file.path,
line: node.location.start_line,
)
with_locals(node.locals) do
instructions += transform_body(node.body, used: true, location: node.location)
instructions += with_locals(node.locals) do
transform_body(node.body, used: true, location: node.location)
end
instructions << EndInstruction.new(:define_module)
instructions << PopInstruction.new unless used
Expand Down Expand Up @@ -2818,7 +2812,10 @@ def is_inline_macro_call_node?(node)

def with_locals(locals)
@locals_stack << locals
yield
instructions = []
instructions += locals.map { |name| VariableDeclareInstruction.new(name) } unless locals.nil?
instructions += yield
instructions
ensure
@locals_stack.pop
end
Expand Down

0 comments on commit d97f269

Please sign in to comment.