Class: BabelBridge::BinaryOperatorProcessor
- Inherits:
-
Object
- Object
- BabelBridge::BinaryOperatorProcessor
- Defined in:
- lib/babel_bridge/tools.rb
Instance Attribute Summary collapse
-
#exact_operator_precedence ⇒ Object
Returns the value of attribute exact_operator_precedence.
-
#node_class ⇒ Object
Returns the value of attribute node_class.
-
#regexp_operator_precedence ⇒ Object
Returns the value of attribute regexp_operator_precedence.
-
#right_operators ⇒ Object
Returns the value of attribute right_operators.
Instance Method Summary collapse
-
#generate_tree(operands, operators, parent_node) ⇒ Object
generates a tree of nodes of the specified node_class The nodes have access to the following useful methods: self.left -> return the left operand parse-tree-node self.right -> return the right operand parse-tree-node self.operator_node -> return the operator parse-tree-node self.operator -> return the operator as a ruby symbol.
-
#index_of_lowest_precedence(operators, associativity = :left) ⇒ Object
associativity = :left => operators of the same precidence execut from left to right :right => operators of the same precidence execut from right to left.
-
#initialize(operator_precedence, node_class, right_operators) ⇒ BinaryOperatorProcessor
constructor
A new instance of BinaryOperatorProcessor.
- #operator_precedence(operator_string) ⇒ Object
Constructor Details
#initialize(operator_precedence, node_class, right_operators) ⇒ BinaryOperatorProcessor
Returns a new instance of BinaryOperatorProcessor.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/babel_bridge/tools.rb', line 67 def initialize(operator_precedence,node_class,right_operators) @right_operators_regexp= right_operators && Tools::array_to_anchored_or_regexp(right_operators) @node_class=node_class @exact_operator_precedence={} @regexp_operator_precedence=[] operator_precedence.each_with_index do |op_level,i| (op_level.kind_of?(Array) ? op_level : [op_level]).each do |op| precedence = operator_precedence.length - i case op when String, Symbol then @exact_operator_precedence[op.to_s] = precedence when Regexp then @regexp_operator_precedence << [op,precedence] end end end end |
Instance Attribute Details
#exact_operator_precedence ⇒ Object
Returns the value of attribute exact_operator_precedence.
66 67 68 |
# File 'lib/babel_bridge/tools.rb', line 66 def exact_operator_precedence @exact_operator_precedence end |
#node_class ⇒ Object
Returns the value of attribute node_class.
66 67 68 |
# File 'lib/babel_bridge/tools.rb', line 66 def node_class @node_class end |
#regexp_operator_precedence ⇒ Object
Returns the value of attribute regexp_operator_precedence.
66 67 68 |
# File 'lib/babel_bridge/tools.rb', line 66 def regexp_operator_precedence @regexp_operator_precedence end |
#right_operators ⇒ Object
Returns the value of attribute right_operators.
66 67 68 |
# File 'lib/babel_bridge/tools.rb', line 66 def right_operators @right_operators end |
Instance Method Details
#generate_tree(operands, operators, parent_node) ⇒ Object
generates a tree of nodes of the specified node_class The nodes have access to the following useful methods:
self.left -> return the left operand parse-tree-node
self.right -> return the right operand parse-tree-node
self.operator_node -> return the operator parse-tree-node
self.operator -> return the operator as a ruby symbol
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/babel_bridge/tools.rb', line 116 def generate_tree(operands, operators, parent_node) return operands[0] if operands.length==1 i = index_of_lowest_precedence(operators) operator = operators[i] new_operand = node_class.new(parent_node) new_operand.add_match generate_tree(operands[0..i], operators[0..i-1],new_operand), :left new_operand.add_match operators[i], :operator_node new_operand.add_match generate_tree(operands[i+1..-1], operators[i+1..-1],new_operand), :right new_operand end |
#index_of_lowest_precedence(operators, associativity = :left) ⇒ Object
associativity =
:left => operators of the same precidence execut from left to right
:right => operators of the same precidence execut from right to left
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/babel_bridge/tools.rb', line 96 def index_of_lowest_precedence(operators,associativity=:left) lowest = lowest_precedence = nil operators.each_with_index do |operator,i| operator_string = operator.to_s precedence = operator_precedence(operator_string) right_associative = @right_operators_regexp && operator_string[@right_operators_regexp] if !lowest || (right_associative ? precedence < lowest_precedence : precedence <= lowest_precedence) lowest = i lowest_precedence = precedence end end lowest end |
#operator_precedence(operator_string) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/babel_bridge/tools.rb', line 84 def operator_precedence(operator_string) p = @exact_operator_precedence[operator_string] return p if p @regexp_operator_precedence.each do |regexp,p| return p if operator_string[regexp] end raise "operator #{operator_string.inspect} didn't match #{@exact_operator_precedence} or #{@regexp_operator_precedence}" end |