Class: Cadenza::OperationNode

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(left, operator, right) ⇒ OperationNode

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



35
36
37
38
39
# File 'lib/cadenza/nodes/operation_node.rb', line 35

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:



7
8
9
# File 'lib/cadenza/nodes/operation_node.rb', line 7

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



28
29
30
# File 'lib/cadenza/nodes/operation_node.rb', line 28

def operator
  @operator
end

#rightVariableNode|ConstantNode|OperationNode|BooleanInverseNode

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

Returns:



11
12
13
# File 'lib/cadenza/nodes/operation_node.rb', line 11

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:



100
101
102
103
104
# File 'lib/cadenza/nodes/operation_node.rb', line 100

def ==(rhs)
   @operator == rhs.operator and
   @left == rhs.left and
   @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.



52
53
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
# File 'lib/cadenza/nodes/operation_node.rb', line 52

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



42
43
44
# File 'lib/cadenza/nodes/operation_node.rb', line 42

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