Sha256: 1a2100e0fcd5b374d6028c403e7b9b8b6229ca197b9753764d39f4e1196a0ba7
Contents?: true
Size: 1.25 KB
Versions: 1
Compression:
Stored size: 1.25 KB
Contents
# frozen_string_literal: true module Preval class Visitors class Arithmetic < Visitor module IntNode refine Node do def int?(value) is?(:@int) && to_int == value end def to_int body[0].to_i end end end using IntNode 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
preval-0.3.0 | lib/preval/visitors/arithmetic.rb |