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, #contains_a?, #deep_dup, #differentiated, #evaluate, #evaluate_assignments, #evaluated, #false?, #or, #replace, #replaced, #simplified, #to_cell, #to_node, #traverse, #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



70
71
72
73
74
75
76
77
78
# File 'lib/keisan/ast/number.rb', line 70

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

#&(other) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/keisan/ast/number.rb', line 80

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

#*(other) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/keisan/ast/number.rb', line 40

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

#**(other) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/keisan/ast/number.rb', line 60

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
31
32
33
34
# File 'lib/keisan/ast/number.rb', line 22

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

#+@Object



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

def +@
  Number.new(value)
end

#-(other) ⇒ Object



36
37
38
# File 'lib/keisan/ast/number.rb', line 36

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



50
51
52
53
54
55
56
57
58
# File 'lib/keisan/ast/number.rb', line 50

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

#<(other) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/keisan/ast/number.rb', line 154

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

#<<(other) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/keisan/ast/number.rb', line 114

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

#<=(other) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'lib/keisan/ast/number.rb', line 164

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

#>(other) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/keisan/ast/number.rb', line 134

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

#>=(other) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/keisan/ast/number.rb', line 144

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

#>>(other) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/keisan/ast/number.rb', line 124

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

#^(other) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/keisan/ast/number.rb', line 94

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

#differentiate(variable, context = nil) ⇒ Object



205
206
207
# File 'lib/keisan/ast/number.rb', line 205

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

#equal(other) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'lib/keisan/ast/number.rb', line 174

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

#not_equal(other) ⇒ Object



184
185
186
187
188
189
190
191
192
# File 'lib/keisan/ast/number.rb', line 184

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



194
195
196
197
198
199
200
201
202
203
# File 'lib/keisan/ast/number.rb', line 194

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



104
105
106
107
108
109
110
111
112
# File 'lib/keisan/ast/number.rb', line 104

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

#~Object



90
91
92
# File 'lib/keisan/ast/number.rb', line 90

def ~
  Number.new(~value)
end