Class: Arithmetic::OperatorNode
- Inherits:
-
Object
- Object
- Arithmetic::OperatorNode
- Defined in:
- lib/arithmetic/nodes.rb
Instance Attribute Summary collapse
-
#operands ⇒ Object
Returns the value of attribute operands.
-
#operator ⇒ Object
Returns the value of attribute operator.
Instance Method Summary collapse
- #eval ⇒ Object
-
#initialize(operator, operands) ⇒ OperatorNode
constructor
A new instance of OperatorNode.
- #to_s(visitor, top = true) ⇒ Object
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
#operands ⇒ Object
Returns the value of attribute operands.
29 30 31 |
# File 'lib/arithmetic/nodes.rb', line 29 def operands @operands end |
#operator ⇒ Object
Returns the value of attribute operator.
29 30 31 |
# File 'lib/arithmetic/nodes.rb', line 29 def operator @operator end |
Instance Method Details
#eval ⇒ Object
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 |