Module: Carbon::Core::Pointer::Math Private

Included in:
Carbon::Core::Pointer
Defined in:
lib/carbon/core/pointer/math.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Defines pointer arithmatic. This allows for addition and subtraction, as well as comparison. This module defines the following functions:

  • .+<N: Carbon::Numeric>(self, other: N): self
  • .-<N: Carbon::Numeric>(self, other: N): self
  • .<<N: Carbon::Numeric>(self, other: N): Carbon::Boolean
  • .><N: Carbon::Numeric>(self, other: N): Carbon::Boolean
  • .<=<N: Carbon::Numeric>(self, other: N): Carbon::Boolean
  • .>=<N: Carbon::Numeric>(self, other: N): Carbon::Boolean
  • .<=><N: Carbon::Numeric>(self, other: N): Carbon::Int8
  • .<(self, other: self): Carbon::Boolean
  • .>(self, other: self): Carbon::Boolean
  • .<=(self, other: self): Carbon::Boolean
  • .>=(self, other: self): Carbon::Boolean
  • .<=>(self, other: self): Carbon::Int8

These are defined for all integers except boolean.

Constant Summary collapse

MATH_OPERATIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The math operations that can be applied to pointers.

Returns:

  • (<::Symbol>)
%i(+ -).freeze
COMP_OPERATIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The comparison operations that can be applied to pointers.

Returns:

  • (<::Symbol>)
%i(< > <= >=).freeze

Instance Method Summary collapse

Instance Method Details

#define_comp_function(int, op) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Defines a given comparison operation on a pointer.

Parameters:

  • int (Core::Int)

    The integer type that the operation uses.

  • op (::Symbol)

    The comparison operation to perform. This should be one of COMP_OPERATIONS.



124
125
126
127
128
129
130
# File 'lib/carbon/core/pointer/math.rb', line 124

def define_comp_function(int, op)
  function_name = PTYPE.call(op, [PTYPE, int.name])
  Core.define(function: function_name) do |function|
    function[:return] = Carbon::Boolean
    define_comp_definition(int, op, function[:definition])
  end
end

#define_comp_pointer_function(op) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Defines a comparison function for two pointers.

Parameters:

  • op (::Symbol)

    The operation that the function performs. This should be one of COMP_OPERATIONS.



83
84
85
86
87
88
89
# File 'lib/carbon/core/pointer/math.rb', line 83

def define_comp_pointer_function(op)
  function_name = PTYPE.call(op, [PTYPE, PTYPE])
  Core.define(function: function_name) do |function|
    function[:return] = Carbon::Boolean
    define_comp_pointer_definition(op, function[:definition])
  end
end

#define_math_function(int, op) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Defines a given math function on a pointer.

Parameters:

  • int (Core::Int)

    The integer type that the operation uses.

  • op (::Symbol)

    The math operation to perform. This should be one of MATH_OPERATIONS.



111
112
113
114
115
116
117
# File 'lib/carbon/core/pointer/math.rb', line 111

def define_math_function(int, op)
  function_name = PTYPE.call(op, [PTYPE, int.name])
  Core.define(function: function_name) do |function|
    function[:return] = PTYPE
    define_math_definition(int, op, function[:definition])
  end
end

#define_math_functionsvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Comprehensively defines all of the math, comparison, and spaceship operations for pointers.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/carbon/core/pointer/math.rb', line 44

def define_math_functions
  Ints.each do |int|
    next if int.size == 1
    MATH_OPERATIONS.each { |op| define_math_function(int, op) }
    COMP_OPERATIONS.each { |op| define_comp_function(int, op) }
    define_space_function(int)
  end

  COMP_OPERATIONS.each { |op| define_comp_pointer_function(op) }
  define_space_pointer_function
  define_null_function
end

#define_null_functionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



57
58
59
60
61
62
63
# File 'lib/carbon/core/pointer/math.rb', line 57

def define_null_function
  function_name = PTYPE.call(:null, [])
  Core.define(function: function_name) do |function|
    function[:return] = PTYPE
    define_null_definition(function[:definition])
  end
end

#define_space_function(int) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Defines the space function (<=>) for a pointer and an integer. It returns -1, 0, or 1, if the receiver is less than, equal to, or greater than the parameter, respectively.

Parameters:

  • int (Core::Int)

    The integer type to compare to.



97
98
99
100
101
102
103
# File 'lib/carbon/core/pointer/math.rb', line 97

def define_space_function(int)
  function_name = PTYPE.call(:<=>, [PTYPE, int.name])
  Core.define(function: function_name) do |function|
    function[:return] = Carbon::Type("Carbon::Int8")
    define_space_definition(int, function[:definition])
  end
end

#define_space_pointer_functionvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Defines the <=> function for two pointers. This returns -1, 0, or 1, if the receiver is less than, equal to, or greater than the parameter, respectively.



70
71
72
73
74
75
76
# File 'lib/carbon/core/pointer/math.rb', line 70

def define_space_pointer_function
  function_name = PTYPE.call(:<=>, [PTYPE, PTYPE])
  Core.define(function: function_name) do |function|
    function[:return] = Carbon::Type("Carbon::Int8")
    define_space_pointer_definition(function[:definition])
  end
end