Class: Cadenza::OperationNode

Inherits:
Object
  • Object
show all
Includes:
TreeNode
Defined in:
lib/cadenza/nodes/operation_node.rb

Overview

The OperationNode is a node which contains an evaluatable #left and #right subtree and an #operator with which to evaluate it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TreeNode

#to_tree

Constructor Details

#initialize(left, operator, right) ⇒ OperationNode

creates a new Cadenza::OperationNode with the given left and right subtrees and the given operator.


37
38
39
40
41
# File 'lib/cadenza/nodes/operation_node.rb', line 37

def initialize(left, operator, right)
   @left = left
   @right = right
   @operator = operator
end

Instance Attribute Details

#leftVariableNode|ConstantNode|OperationNode|BooleanInverseNode

Returns the root of the left subtree to evaluate for this operation.

Returns:


9
10
11
# File 'lib/cadenza/nodes/operation_node.rb', line 9

def left
  @left
end

#operatorString

The operation of this node to do when evaluating. The operator should be one of the following values:

  • “==” for equal

  • “!=” for not equal

  • “>=” for greater than or equal to

  • “<=” for less than or equal to

  • “>” for greater than

  • “<” for less than

  • “and” for the boolean 'and' conjunction

  • “or” for the boolean 'or' conjunction

  • “+” for the arithmetic addition operation

  • “-” for the arithmetic subtraction operation

  • “*” for the arithmetic multiplication operation

  • “/” for the arithmetic division operation

Returns:

  • (String)

    the operation for this node


30
31
32
# File 'lib/cadenza/nodes/operation_node.rb', line 30

def operator
  @operator
end

#rightVariableNode|ConstantNode|OperationNode|BooleanInverseNode

Returns the root of the right subtree to evaluate for this operation.

Returns:


13
14
15
# File 'lib/cadenza/nodes/operation_node.rb', line 13

def right
  @right
end

Instance Method Details

#==(rhs) ⇒ Boolean

Returns true if the given Cadenza::OperationNode is equivalent by value to this node.

Parameters:

Returns:


102
103
104
105
106
# File 'lib/cadenza/nodes/operation_node.rb', line 102

def ==(rhs)
   @operator == rhs.operator &&
   @left == rhs.left &&
   @right == rhs.right
end

#eval(context) ⇒ Object

Evalutes the left and right subtree in the given context and uses the #operator to manipulate the two values into a single output value which is then returned.

Parameters:

Returns:

  • (Object)

    the result of performing the operation on the evaluated left and right subtrees in the given context.


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cadenza/nodes/operation_node.rb', line 54

def eval(context)
   l = @left.eval(context)
   r = @right.eval(context)

   case @operator
      when '=='
         return l == r

      when '!='
         return l != r

      when '>='
         return l >= r

      when '<='
         return l <= r

      when '>'
         return l > r

      when '<'
         return l < r

      when 'and'
         return l && r

      when 'or'
         return l || r

      when '+'
         return l + r

      when '-'
         return l - r

      when '*'
         return l * r

      when '/'
         return l / r

      else throw "undefined operator: #{@operator}"
   end
end

#implied_globalsArray

Returns a list of variable names implied to be global in this node.

Returns:

  • (Array)

    a list of variable names implied to be global in this node


44
45
46
# File 'lib/cadenza/nodes/operation_node.rb', line 44

def implied_globals
   @left.implied_globals | @right.implied_globals
end