module Carbon::Core::Integer::Sign

Defines signage functions. This can be used to determine if an integer is signed or unsigned. This defines the following functions on all integers except boolean:

@api private

Public Instance Methods

define_sign_function(int, sign, args) click to toggle source

Defines a sign function. If the integer is the same sign as the given sign, the defined function returns `true`; otherwise, it returns `false`.

@param int [Core::Int] The (receiver) value. @param sign [::Symbol] The sign function to define. @param args [<Concrete::Type>] The arguments that the function

takes.  These are ignored.
# File lib/carbon/core/integer/sign.rb, line 40
def define_sign_function(int, sign, args)
  function_name = int.name.call("#{sign}?", args)
  Core.define(function: function_name) do |function|
    function[:return] = Carbon::Boolean
    function[:definition].add("entry").build do |b|
      int.sign == sign ? b.ret(1) : b.ret(0)
    end
  end
end
define_sign_functions(int) click to toggle source

Defines the sign functions on the given integer. This produces the four variants of the sign functions: signed vs. unsigned, and no vs. one parameter.

@param int [Core::Int] The (receiver) value. @return [void]

# File lib/carbon/core/integer/sign.rb, line 24
def define_sign_functions(int)
  params = [[], [int.name]]
  signs = [:signed, :unsigned]
  params.product(signs).each do |(param, sign)|
    define_sign_function(int, sign, param)
  end
end