Class: Arithmetic::OperatorNode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operator, operands) ⇒ OperatorNode

Returns a new instance of OperatorNode.



31
32
33
34
# File 'lib/arithmetic/nodes.rb', line 31

def initialize(operator, operands)
  @operator = operator
  @operands = operands
end

Instance Attribute Details

#operandsObject

Returns the value of attribute operands.



29
30
31
# File 'lib/arithmetic/nodes.rb', line 29

def operands
  @operands
end

#operatorObject

Returns the value of attribute operator.



29
30
31
# File 'lib/arithmetic/nodes.rb', line 29

def operator
  @operator
end

Instance Method Details

#evalObject



48
49
50
# File 'lib/arithmetic/nodes.rb', line 48

def eval
  @operator.eval(*@operands.map(&:eval))
end

#to_s(visitor, top = true) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/arithmetic/nodes.rb', line 36

def to_s(visitor, top=true)
  strs = @operands.map {|o| o.to_s(visitor, false) }

  if @operator.arity == 1
    "#{visitor.call(@operator)}#{strs.first}"
  else
    result = [strs.first, visitor.call(@operator), *strs[1..-1]].join(" ")
    result = visitor.call("(") + result + visitor.call(")") unless top
    result
  end
end