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