Class: Loxxy::Datatype::Number

Inherits:
BuiltinDatatype show all
Defined in:
lib/loxxy/datatype/number.rb

Overview

Class for representing a Lox numeric value.

Instance Attribute Summary

Attributes inherited from BuiltinDatatype

#value

Instance Method Summary collapse

Methods inherited from BuiltinDatatype

#!, #!=, #accept, #and, #falsey?, #initialize, #or, #to_str, #truthy?

Constructor Details

This class inherits a constructor from Loxxy::Datatype::BuiltinDatatype

Instance Method Details

#*(other) ⇒ Loxxy::Datatype::Number

Perform the multiplication of two Lox numbers or one Lox number and a Ruby Numeric

Parameters:

Returns:



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/loxxy/datatype/number.rb', line 50

def *(other)
  case other
  when Number
    self.class.new(value * other.value)
  when Numeric
    self.class.new(value * other)
  else
    err_msg = "'*': Operands must be numbers."
    raise TypeError, err_msg
  end
end

#+(other) ⇒ Loxxy::Datatype::Number

Perform the addition of two Lox numbers or one Lox number and a Ruby Numeric

Parameters:

Returns:



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/loxxy/datatype/number.rb', line 18

def +(other)
  case other
  when Number
    self.class.new(value + other.value)
  when Numeric
    self.class.new(value + other)
  else
    err_msg = "`+': #{other.class} can't be coerced into #{self.class} (TypeError)"
    raise TypeError, err_msg
  end
end

#-(other) ⇒ Loxxy::Datatype::Number

Perform the subtraction of two Lox numbers or one Lox number and a Ruby Numeric

Parameters:

Returns:



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/loxxy/datatype/number.rb', line 34

def -(other)
  case other
  when Number
    self.class.new(value - other.value)
  when Numeric
    self.class.new(value - other)
  else
    err_msg = "`-': #{other.class} can't be coerced into #{self.class} (TypeError)"
    raise TypeError, err_msg
  end
end

#-@Loxxy::Datatype::Number

Unary minus (return value with changed sign)



91
92
93
# File 'lib/loxxy/datatype/number.rb', line 91

def -@
  self.class.new(-value)
end

#/(other) ⇒ Loxxy::Datatype::Number

Perform the division of two Lox numbers or one Lox number and a Ruby Numeric rubocop: disable Lint/BinaryOperatorWithIdenticalOperands

Parameters:

Returns:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/loxxy/datatype/number.rb', line 67

def /(other)
  case other
  when Number, Numeric
    if other.zero?
      if zero?
        # NaN case detected
        self.class.new(0.0 / 0.0)
      else
        raise ZeroDivisionError
      end
    elsif other.kind_of?(Number)
      self.class.new(value / other.value)
    else
      self.class.new(value / other)
    end
  else
    err_msg = "'/': Operands must be numbers."
    raise TypeError, err_msg
  end
end

#<(other) ⇒ Datatype::Boolean

Check whether this Lox number has a lesser value than given argument.

Parameters:

Returns:



143
144
145
# File 'lib/loxxy/datatype/number.rb', line 143

def <(other)
  !(self >= other)
end

#<=(other) ⇒ Datatype::Boolean

Check whether this Lox number has a lesser or equal value than given argument.

Parameters:

Returns:



151
152
153
# File 'lib/loxxy/datatype/number.rb', line 151

def <=(other)
  !(self > other)
end

#==(other) ⇒ Datatype::Boolean

Check the equality of a Lox number object with another object

Parameters:

Returns:



98
99
100
101
102
103
104
105
106
107
# File 'lib/loxxy/datatype/number.rb', line 98

def ==(other)
  case other
  when Number
    (value == other.value) ? True.instance : False.instance
  when Numeric
    (value == other) ? True.instance : False.instance
  else
    False.instance
  end
end

#>(other) ⇒ Datatype::Boolean

Check whether this Lox number has a greater value than given argument.

Parameters:

Returns:



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/loxxy/datatype/number.rb', line 112

def >(other)
  case other
  when Number
    (value > other.value) ? True.instance : False.instance
  when Numeric
    (value > other) ? True.instance : False.instance
  else
    msg = "'>': Operands must be numbers."
    raise StandardError, msg
  end
end

#>=(other) ⇒ Datatype::Boolean

Check whether this Lox number has a greater or equal value than given argument.

Parameters:

Returns:



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/loxxy/datatype/number.rb', line 128

def >=(other)
  case other
  when Number
    (value >= other.value) ? True.instance : False.instance
  when Numeric
    (value >= other) ? True.instance : False.instance
  else
    msg = "'>': Operands must be numbers."
    raise StandardError, msg
  end
end

#zero?Boolean

Returns:



10
11
12
# File 'lib/loxxy/datatype/number.rb', line 10

def zero?
  value.zero?
end