Class: Keisan::AST::Number

Inherits:
ConstantLiteral show all
Defined in:
lib/keisan/ast/number.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ConstantLiteral

#==, #evaluate, #to_s

Methods inherited from Node

#!, #and, #coerce, #deep_dup, #evaluate, #evaluate_assignments, #evaluated, #false?, #or, #replace, #simplified, #to_cell, #to_node, #true?, #unbound_functions, #unbound_variables, #well_defined?

Constructor Details

#initialize(number) ⇒ Number

Returns a new instance of Number.



6
7
8
# File 'lib/keisan/ast/number.rb', line 6

def initialize(number)
  @number = number
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



4
5
6
# File 'lib/keisan/ast/number.rb', line 4

def number
  @number
end

Instance Method Details

#%(other) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/keisan/ast/number.rb', line 66

def %(other)
  other = other.to_node
  case other
  when Number
    Number.new(value % other.value)
  else
    super
  end
end

#&(other) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/keisan/ast/number.rb', line 76

def &(other)
  other = other.to_node
  case other
  when Number
    Number.new(value & other.value)
  else
    super
  end
end

#*(other) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/keisan/ast/number.rb', line 36

def *(other)
  other = other.to_node
  case other
  when Number
    Number.new(value * other.value)
  else
    super
  end
end

#**(other) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/keisan/ast/number.rb', line 56

def **(other)
  other = other.to_node
  case other
  when Number
    Number.new(value ** other.value)
  else
    super
  end
end

#+(other) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/keisan/ast/number.rb', line 22

def +(other)
  other = other.to_node
  case other
  when Number
    Number.new(value + other.value)
  else
    super
  end
end

#+@Object



18
19
20
# File 'lib/keisan/ast/number.rb', line 18

def +@
  Number.new(value)
end

#-(other) ⇒ Object



32
33
34
# File 'lib/keisan/ast/number.rb', line 32

def -(other)
  self + (-other.to_node)
end

#-@Object



14
15
16
# File 'lib/keisan/ast/number.rb', line 14

def -@
  Number.new(-value)
end

#/(other) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/keisan/ast/number.rb', line 46

def /(other)
  other = other.to_node
  case other
  when Number
    Number.new(Rational(value, other.value))
  else
    super
  end
end

#<(other) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/keisan/ast/number.rb', line 130

def <(other)
  other = other.to_node
  case other
  when Number
    Boolean.new(value < other.value)
  else
    super
  end
end

#<=(other) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/keisan/ast/number.rb', line 140

def <=(other)
  other = other.to_node
  case other
  when Number
    Boolean.new(value <= other.value)
  else
    super
  end
end

#>(other) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/keisan/ast/number.rb', line 110

def >(other)
  other = other.to_node
  case other
  when Number
    Boolean.new(value > other.value)
  else
    super
  end
end

#>=(other) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/keisan/ast/number.rb', line 120

def >=(other)
  other = other.to_node
  case other
  when Number
    Boolean.new(value >= other.value)
  else
    super
  end
end

#^(other) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/keisan/ast/number.rb', line 90

def ^(other)
  other = other.to_node
  case other
  when Number
    Number.new(value ^ other.value)
  else
    super
  end
end

#differentiate(variable, context = nil) ⇒ Object



181
182
183
# File 'lib/keisan/ast/number.rb', line 181

def differentiate(variable, context = nil)
  0.to_node
end

#equal(other) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/keisan/ast/number.rb', line 150

def equal(other)
  other = other.to_node
  case other
  when Number
    Boolean.new(value == other.value)
  else
    super
  end
end

#not_equal(other) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/keisan/ast/number.rb', line 160

def not_equal(other)
  other = other.to_node
  case other
  when Number
    Boolean.new(value != other.value)
  else
    super
  end
end

#simplify(context = nil) ⇒ Object



170
171
172
173
174
175
176
177
178
179
# File 'lib/keisan/ast/number.rb', line 170

def simplify(context = nil)
  case number
  when Rational
    if number.denominator == 1
      @number = number.numerator
    end
  end

  self
end

#value(context = nil) ⇒ Object



10
11
12
# File 'lib/keisan/ast/number.rb', line 10

def value(context = nil)
  number
end

#|(other) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/keisan/ast/number.rb', line 100

def |(other)
  other = other.to_node
  case other
  when Number
    Number.new(value | other.value)
  else
    super
  end
end

#~Object



86
87
88
# File 'lib/keisan/ast/number.rb', line 86

def ~
  Number.new(~value)
end