Class: BinaryOperationNode

Inherits:
Node
  • Object
show all
Defined in:
lib/nodes/basenodes.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#value

Instance Method Summary collapse

Constructor Details

#initialize(lhs, op, rhs) ⇒ BinaryOperationNode

Returns a new instance of BinaryOperationNode.



162
163
164
165
# File 'lib/nodes/basenodes.rb', line 162

def initialize(lhs, op, rhs)
  super(op)
  @lhs, @op, @rhs = lhs, op, rhs
end

Instance Attribute Details

#lhsObject

Returns the value of attribute lhs.



160
161
162
# File 'lib/nodes/basenodes.rb', line 160

def lhs
  @lhs
end

#opObject

Returns the value of attribute op.



160
161
162
# File 'lib/nodes/basenodes.rb', line 160

def op
  @op
end

#rhsObject

Returns the value of attribute rhs.



160
161
162
# File 'lib/nodes/basenodes.rb', line 160

def rhs
  @rhs
end

Instance Method Details

#evaluateObject



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/nodes/basenodes.rb', line 171

def evaluate
  if @rhs.evaluate.is_a?(ArrayNode) && @op == '+'
    return @value = @rhs.evaluate + @lhs.evaluate
  end

  if @op == '/'
    @value = @lhs.evaluate.to_f.send(@op, @rhs.evaluate)
  else
    @value = @lhs.evaluate.send(@op, @rhs.evaluate)
  end
end

#to_sObject



167
168
169
# File 'lib/nodes/basenodes.rb', line 167

def to_s
  "#{@lhs} #{@op} #{@rhs}"
end