Sha256: ade0a6a0a3bceaa3e0a0f79a6142e0f0ed26ca96c86e9873ec05e3d3bc056de4

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Preval
  class Visitors
    class Arithmetic < Visitor
      using(
        Module.new do
          refine Node do
            def int?(value)
              is?(:@int) && to_int == value
            end

            def to_int
              body[0].to_i
            end
          end
        end
      )

      OPERATORS = %i[+ - * / % **].freeze

      def on_binary(node)
        left, operation, right = node.body

        if left.is?(:@int) && OPERATORS.include?(operation) && right.is?(:@int)
          value = left.to_int.public_send(operation, right.to_int).to_s
          node.update(:@int, value)
        elsif %i[+ -].include?(operation)
          if right.int?(0)
            node.replace(left)
          elsif left.int?(0)
            node.replace(right)
          end
        elsif %i[* /].include?(operation)
          if right.int?(1)
            node.replace(left)
          elsif left.int?(1)
            node.replace(right)
          end
        elsif operation == :**
          if left.is?(:@int) && right.int?(0)
            node.update(:@int, left.to_int < 0 ? -1 : 1)
          elsif right.int?(1)
            node.replace(left)
          elsif left.int?(1)
            node.replace(left)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
preval-0.6.1 lib/preval/visitors/arithmetic.rb
preval-0.6.0 lib/preval/visitors/arithmetic.rb
preval-0.5.0 lib/preval/visitors/arithmetic.rb
preval-0.4.1 lib/preval/visitors/arithmetic.rb
preval-0.4.0 lib/preval/visitors/arithmetic.rb