Sha256: 095e53b0f901b9eef6e08d76c29012e42827deb3bd524e0c95843830c9d24df2

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module Carbon
  module Core
    module Integer
      # 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:
      #
      # - `.signed?(): Carbon::Boolean`
      # - `.signed?(self): Carbon::Boolean`
      # - `.unsigned?(): Carbon::Boolean`
      # - `.unsigned?(self): Carbon::Boolean`
      #
      # @api private
      module Sign
        # 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]
        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

        # 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.
        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
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
carbon-core-0.2.1 lib/carbon/core/integer/sign.rb
carbon-core-0.2.0 lib/carbon/core/integer/sign.rb
carbon-core-0.1.1 lib/carbon/core/integer/sign.rb
carbon-core-0.1.0 lib/carbon/core/integer/sign.rb