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:



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/loxxy/datatype/number.rb', line 46

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:



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/loxxy/datatype/number.rb', line 14

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:



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/loxxy/datatype/number.rb', line 30

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)



76
77
78
# File 'lib/loxxy/datatype/number.rb', line 76

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

Parameters:

Returns:



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/loxxy/datatype/number.rb', line 62

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) ⇒ Datatype::Boolean

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

Parameters:

Returns:



128
129
130
# File 'lib/loxxy/datatype/number.rb', line 128

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

#<=(other) ⇒ Datatype::Boolean

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

Parameters:

Returns:



136
137
138
# File 'lib/loxxy/datatype/number.rb', line 136

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

#==(other) ⇒ Datatype::Boolean

Check the equality of a Lox number object with another object

Parameters:

Returns:



83
84
85
86
87
88
89
90
91
92
# File 'lib/loxxy/datatype/number.rb', line 83

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:



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

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:



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

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