# 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