Sha256: 97ee3c527ffe302a09ca10cb0621176016133a53439e6057a9bdb0c687ecd81e

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 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))
            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))
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carbon-core-0.1.0 lib/carbon/core/integer/zero.rb