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.



123
124
125
126
# File 'lib/nodes/basenodes.rb', line 123

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

Instance Attribute Details

#lhsObject

Returns the value of attribute lhs.



121
122
123
# File 'lib/nodes/basenodes.rb', line 121

def lhs
  @lhs
end

#opObject

Returns the value of attribute op.



121
122
123
# File 'lib/nodes/basenodes.rb', line 121

def op
  @op
end

#rhsObject

Returns the value of attribute rhs.



121
122
123
# File 'lib/nodes/basenodes.rb', line 121

def rhs
  @rhs
end

Instance Method Details

#evaluateObject



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/nodes/basenodes.rb', line 132

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



128
129
130
# File 'lib/nodes/basenodes.rb', line 128

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