Class: Cadenza::OperationNode
- Inherits:
-
Object
- Object
- Cadenza::OperationNode
- 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
-
#left ⇒ VariableNode|ConstantNode|OperationNode|BooleanInverseNode
The root of the left subtree to evaluate for this operation.
-
#operator ⇒ String
The operation of this node to do when evaluating.
-
#right ⇒ VariableNode|ConstantNode|OperationNode|BooleanInverseNode
The root of the right subtree to evaluate for this operation.
Instance Method Summary collapse
-
#==(rhs) ⇒ Boolean
True if the given OperationNode is equivalent by value to this node.
-
#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.
-
#implied_globals ⇒ Array
A list of variable names implied to be global in this node.
-
#initialize(left, operator, right) ⇒ OperationNode
constructor
creates a new OperationNode with the given left and right subtrees and the given operator.
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
#left ⇒ VariableNode|ConstantNode|OperationNode|BooleanInverseNode
Returns the root of the left subtree to evaluate for this operation.
7 8 9 |
# File 'lib/cadenza/nodes/operation_node.rb', line 7 def left @left end |
#operator ⇒ String
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
28 29 30 |
# File 'lib/cadenza/nodes/operation_node.rb', line 28 def operator @operator end |
#right ⇒ VariableNode|ConstantNode|OperationNode|BooleanInverseNode
Returns the root of the right subtree to evaluate for this operation.
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.
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.
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_globals ⇒ Array
Returns 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 |