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.



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

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

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



134
135
136
# File 'lib/kalc/ast.rb', line 134

def left
  @left
end

#operatorObject (readonly)

Returns the value of attribute operator.



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

def operator
  @operator
end

#rightObject (readonly)

Returns the value of attribute right.



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

def right
  @right
end

Instance Method Details

#eval(context) ⇒ Object



144
145
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
# File 'lib/kalc/ast.rb', line 144

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