Sha256: 6c9cbc4ef4984ffcaebbb774d4db971359c93f4703ce3acc0683284f25bf3dc9

Contents?: true

Size: 1.65 KB

Versions: 17

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module Mutant
  class Mutator
    class Node
      # Mutation emitter to handle binary connectives
      class Binary < self

        INVERSE = {
          and: :or,
          or:  :and
        }.freeze

        handle(*INVERSE.keys)

        children :left, :right

      private

        # Emit mutations
        #
        # @return [undefined]
        def dispatch
          emit_singletons
          emit_promotions
          emit_operator_mutations
          emit_left_negation
          emit_left_mutations
          emit_right_mutations
        end

        # Emit operator mutations
        #
        # @return [undefined]
        def emit_operator_mutations
          emit(s(INVERSE.fetch(node.type), left, right))
        end

        # Emit promotions
        #
        # @return [undefined]
        def emit_promotions
          emit(left)
          emit(right)
        end

        # Emit left negation
        #
        # We do not emit right negation as the `and` and `or` nodes
        # in ruby are also used for control flow.
        #
        # Irrespective of their syntax, aka `||` parses internally to `or`.
        #
        # `do_a or do_b`. Negating left makes sense, negating right
        # only when the result is actually used.
        #
        # It *would* be possible to emit the right negation in case the use of the result is proved.
        # Like parent is an assignment to an {l,i}var. Dunno if we ever get the time to do that.
        #
        # @return [undefined]
        def emit_left_negation
          emit(s(node.type, n_not(left), right))
        end

      end # Binary
    end # Node
  end # Mutator
end # Mutant

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
mutant-0.9.8 lib/mutant/mutator/node/binary.rb
mutant-0.9.7 lib/mutant/mutator/node/binary.rb
mutant-0.9.6 lib/mutant/mutator/node/binary.rb
mutant-0.9.5 lib/mutant/mutator/node/binary.rb
mutant-0.9.4 lib/mutant/mutator/node/binary.rb
mutant-0.9.3 lib/mutant/mutator/node/binary.rb
mutant-0.9.2 lib/mutant/mutator/node/binary.rb
mutant-0.9.1 lib/mutant/mutator/node/binary.rb
mutant-0.9.0 lib/mutant/mutator/node/binary.rb
mutant-0.8.24 lib/mutant/mutator/node/binary.rb
mutant-0.8.23 lib/mutant/mutator/node/binary.rb
mutant-0.8.22 lib/mutant/mutator/node/binary.rb
mutant-0.8.21 lib/mutant/mutator/node/binary.rb
mutant-0.8.20 lib/mutant/mutator/node/binary.rb
mutant-0.8.19 lib/mutant/mutator/node/binary.rb
mutant-0.8.18 lib/mutant/mutator/node/binary.rb
mutant-0.8.17 lib/mutant/mutator/node/binary.rb