From 02dee4545885a8bbfc3e685cea6fc6a5b457de83 Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Wed, 12 Jun 2013 19:19:06 +0200 Subject: [PATCH] Port literal float emitter to parser Also simplify emitter for special float cases Nan, Inf, -Inf --- lib/mutant/mutator/node/literal.rb | 31 ----------------------- lib/mutant/mutator/node/literal/fixnum.rb | 2 +- lib/mutant/mutator/node/literal/float.rb | 10 ++++++-- lib/mutant/node_helpers.rb | 6 +++++ 4 files changed, 15 insertions(+), 34 deletions(-) diff --git a/lib/mutant/mutator/node/literal.rb b/lib/mutant/mutator/node/literal.rb index 9b5bdc4c..9b39bf00 100644 --- a/lib/mutant/mutator/node/literal.rb +++ b/lib/mutant/mutator/node/literal.rb @@ -21,37 +21,6 @@ module Mutant end end - # Return AST representing NaN - # - # @return [Parser::AST::Node] - # - # @api private - # - def nan - zero_float = new_float(0) - new_send_with_arguments(zero_float, :/, zero_float) - end - - # Return AST representing negative infinity - # - # @return [Parser::AST::Node] - # - # @api private - # - def negative_infinity - new_send_with_arguments(new_float(-1), :/, new_float(0)) - end - - # Return AST representing infinity - # - # @return [Parser::AST::Node] - # - # @api private - # - def infinity - new_send_with_arguments(new_float(1), :/, new_float(0)) - end - end # Literal end # Node end # Mutator diff --git a/lib/mutant/mutator/node/literal/fixnum.rb b/lib/mutant/mutator/node/literal/fixnum.rb index 43609ee1..e32dff42 100644 --- a/lib/mutant/mutator/node/literal/fixnum.rb +++ b/lib/mutant/mutator/node/literal/fixnum.rb @@ -28,7 +28,7 @@ module Mutant # @api private # def values - [0, 1, -first_child] + [0, 1, -children.first] end end # Fixnuma diff --git a/lib/mutant/mutator/node/literal/float.rb b/lib/mutant/mutator/node/literal/float.rb index 4b74bcb6..8a1a64eb 100644 --- a/lib/mutant/mutator/node/literal/float.rb +++ b/lib/mutant/mutator/node/literal/float.rb @@ -20,6 +20,12 @@ module Mutant emit_new { new_self(Random.float) } end + SPECIAL = [ + NodeHelpers::NAN, + NodeHelpers::NEGATIVE_INFINITY, + NodeHelpers::INFINITY + ].freeze + # Emit special cases # # @return [undefined] @@ -27,7 +33,7 @@ module Mutant # @api private # def emit_special_cases - [infinity, negative_infinity, nan].each do |value| + SPECIAL.each do |value| emit(value) end end @@ -39,7 +45,7 @@ module Mutant # @api private # def values - [0.0, 1.0] << -node.value + [0.0, 1.0, -children.first] end end # Float diff --git a/lib/mutant/node_helpers.rb b/lib/mutant/node_helpers.rb index bfbd6cab..c9b9df94 100644 --- a/lib/mutant/node_helpers.rb +++ b/lib/mutant/node_helpers.rb @@ -13,6 +13,12 @@ module Mutant def s(type, *children) Parser::AST::Node.new(type, children) end + module_function :s + + + NAN = s(:send, s(:float, 0.0), :/, s(:args, s(:float, 0.0))) + NEGATIVE_INFINITY = s(:send, s(:float, -1.0), :/, s(:args, s(:float, 0.0))) + INFINITY = s(:send, s(:float, 1.0), :/, s(:args, s(:float, 0.0))) end # NodeHelpers end # Mutant