Class: Kalc::Ast::Arithmetic

Inherits:
Object
  • Object
show all
Defined in:
lib/kalc/ast.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, right, operator) ⇒ Arithmetic

Returns a new instance of Arithmetic.



140
141
142
143
144
# File 'lib/kalc/ast.rb', line 140

def initialize(left, right, operator)
  @left = left
  @right = right
  @operator = operator
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



136
137
138
# File 'lib/kalc/ast.rb', line 136

def left
  @left
end

#operatorObject (readonly)

Returns the value of attribute operator.



138
139
140
# File 'lib/kalc/ast.rb', line 138

def operator
  @operator
end

#rightObject (readonly)

Returns the value of attribute right.



137
138
139
# File 'lib/kalc/ast.rb', line 137

def right
  @right
end

Instance Method Details

#eval(context) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/kalc/ast.rb', line 146

def eval(context)
  case @operator.to_s.strip
  when '&&'
    @left && @right
  when 'and'
    @left && @right
  when '||'
    @left || @right
  when 'or'
    @left || @right
  when '<='
    @left <= @right
  when '>='
    @left >= @right
  when '='
    @left == @right
  when '=='
    @left == @right
  when '!='
    @left != @right
  when '-'
    @left - @right
  when '+'
    @left + @right
  when '^'
    @left ** @right
  when '*'
    @left * @right
  when '/'
    @left / @right
  when '%'
    @left % @right
  when '<'
    @left < @right
  when '>'
    @left > @right
  end
end