Sha256: 05158d7c8fe69eef526377c49428bc793ca9cd66b7643691f7686ec4bc41e4b8

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module Carbon
  module Core
    module Integer
      # Defines zero functions.  These can be used to determine if an integer
      # is (or isn't) zero.  This defines all of these functions on all
      # integers except booleans:
      #
      # - `.zero?(self): Carbon::Boolean`
      # - `.nonzero?(self): Carbon::Boolean`
      #
      # @api private
      module Zero
        # Defines the zero function.  The function returns `true` if the value
        # is equal to zero.
        #
        # @param int [Core::Int] The (receiver) value.
        # @return [void]
        def define_zero_function(int)
          function_name = int.name.call("zero?", [int.name])
          Core.define(function: function_name) do |function|
            function[:return] = Carbon::Boolean
            function[:definition].add("entry").build do |b|
              this = function[:definition].params[0]
              this.name = "self"
              b.ret(b.icmp(:eq, this, 0).as(Carbon::Boolean))
            end
          end
        end

        # Defines the nonzero function.  The function returns `true` if the
        # value isn't equal to zero.
        #
        # @param int [Core::Int] The (receiver) value.
        # @return [void]
        def define_nonzero_function(int)
          function_name = int.name.call("nonzero?", [int.name])
          Core.define(function: function_name) do |function|
            function[:return] = Carbon::Boolean
            function[:definition].add("entry").build do |b|
              this = function[:definition].params[0]
              this.name = "self"
              b.ret(b.icmp(:ne, this, 0).as(Carbon::Boolean))
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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