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.
21 22 23 24 |
# File 'lib/arithmetic/nodes.rb', line 21 def initialize(operator, operands) @operator = operator @operands = operands end |
Instance Attribute Details
#operands ⇒ Object
Returns the value of attribute operands.
19 20 21 |
# File 'lib/arithmetic/nodes.rb', line 19 def operands @operands end |
#operator ⇒ Object
Returns the value of attribute operator.
19 20 21 |
# File 'lib/arithmetic/nodes.rb', line 19 def operator @operator end |
Instance Method Details
#eval ⇒ Object
38 39 40 |
# File 'lib/arithmetic/nodes.rb', line 38 def eval @operator.eval(*@operands.map(&:eval)) end |
#to_s(visitor, top = true) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/arithmetic/nodes.rb', line 26 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 |