Use and improve child nameing for send mutator
This commit is contained in:
parent
10c611f5e3
commit
d364c7b519
3 changed files with 29 additions and 46 deletions
|
@ -43,15 +43,19 @@ module Mutant
|
|||
|
||||
# Define remaining children
|
||||
#
|
||||
# @param [Fixnum] from_index
|
||||
# @param [Array<Symbol>] names
|
||||
#
|
||||
# @return [undefined]
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
def self.define_remaining_children(from_index)
|
||||
def self.define_remaining_children(names)
|
||||
define_method(:remaining_children_with_index) do
|
||||
children.each_with_index.drop(names.length)
|
||||
end
|
||||
|
||||
define_method(:remaining_children) do
|
||||
children[from_index..-1]
|
||||
children.drop(names.length)
|
||||
end
|
||||
end
|
||||
private_class_method :define_remaining_children
|
||||
|
@ -66,7 +70,7 @@ module Mutant
|
|||
names.each_with_index do |name, index|
|
||||
define_named_child(name, index)
|
||||
end
|
||||
define_remaining_children(names.length)
|
||||
define_remaining_children(names)
|
||||
end
|
||||
private_class_method :children
|
||||
|
||||
|
|
|
@ -7,8 +7,9 @@ module Mutant
|
|||
|
||||
handle(:send)
|
||||
|
||||
RECEIVER_INDEX, SELECTOR_INDEX = 0, 1
|
||||
ARGUMENTS_INDEX = 2..-1.freeze
|
||||
children :receiver, :selector
|
||||
|
||||
alias_method :arguments, :remaining_children
|
||||
|
||||
private
|
||||
|
||||
|
@ -23,6 +24,16 @@ module Mutant
|
|||
run(Binary)
|
||||
return
|
||||
end
|
||||
normal_dispatch
|
||||
end
|
||||
|
||||
# Perform normal, non special case dispatch
|
||||
#
|
||||
# @return [undefined]
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
def normal_dispatch
|
||||
emit(receiver) if receiver
|
||||
mutate_receiver
|
||||
emit_argument_propagation
|
||||
|
@ -52,8 +63,7 @@ module Mutant
|
|||
def mutate_arguments
|
||||
return if arguments.empty?
|
||||
emit_self(receiver, selector)
|
||||
children.each_index do |index|
|
||||
next if index <= SELECTOR_INDEX
|
||||
remaining_children_with_index.each do |node, index|
|
||||
mutate_child(index)
|
||||
delete_child(index)
|
||||
end
|
||||
|
@ -79,7 +89,7 @@ module Mutant
|
|||
def mutate_receiver
|
||||
return unless receiver
|
||||
emit_implicit_self
|
||||
mutate_child(RECEIVER_INDEX)
|
||||
emit_receiver_mutations
|
||||
end
|
||||
|
||||
# Emit implicit self mutation
|
||||
|
@ -90,41 +100,10 @@ module Mutant
|
|||
#
|
||||
def emit_implicit_self
|
||||
if receiver.type == :self and !KEYWORDS.include?(selector)
|
||||
emit_child_update(RECEIVER_INDEX, nil)
|
||||
emit_receiver(nil)
|
||||
end
|
||||
end
|
||||
|
||||
# Return receiver
|
||||
#
|
||||
# @return [Parser::Node::AST]
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
def receiver
|
||||
children[RECEIVER_INDEX]
|
||||
end
|
||||
|
||||
# Return selector
|
||||
#
|
||||
# @return [Symbol]
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
def selector
|
||||
children[SELECTOR_INDEX]
|
||||
end
|
||||
|
||||
# Return arguments
|
||||
#
|
||||
# @return [Array<Parser::AST::Node>]
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
def arguments
|
||||
children[ARGUMENTS_INDEX]
|
||||
end
|
||||
memoize :arguments
|
||||
|
||||
end # Send
|
||||
end # Node
|
||||
end # Mutator
|
||||
|
|
|
@ -6,7 +6,7 @@ module Mutant
|
|||
# Mutator for sends that correspond to a binary operator
|
||||
class Binary < self
|
||||
|
||||
RIGHT_INDEX = SELECTOR_INDEX+1
|
||||
children :left, :operator, :right
|
||||
|
||||
private
|
||||
|
||||
|
@ -17,10 +17,10 @@ module Mutant
|
|||
# @api private
|
||||
#
|
||||
def dispatch
|
||||
emit(receiver)
|
||||
mutate_child(RECEIVER_INDEX) # left
|
||||
mutate_child(RIGHT_INDEX)
|
||||
emit(arguments.first)
|
||||
emit(left)
|
||||
emit_left_mutations
|
||||
emit(right)
|
||||
emit_right_mutations
|
||||
end
|
||||
|
||||
end # Binary
|
||||
|
|
Loading…
Reference in a new issue