Class: Romanesco::Operator

Inherits:
Expression show all
Defined in:
lib/romanesco/elements/operator.rb

Direct Known Subclasses

BinaryOperator, UnaryOperator

Instance Attribute Summary collapse

Attributes inherited from Expression

#parent

Instance Method Summary collapse

Methods inherited from Expression

#initialize

Constructor Details

This class inherits a constructor from Romanesco::Expression

Instance Attribute Details

#symbolObject

Returns the value of attribute symbol.



6
7
8
# File 'lib/romanesco/elements/operator.rb', line 6

def symbol
  @symbol
end

Instance Method Details

#check_for_blank_symbolObject

Raises:



16
17
18
# File 'lib/romanesco/elements/operator.rb', line 16

def check_for_blank_symbol
  raise NoSymbolError if @symbol.nil? || @symbol.gsub(/\s+/, '').empty?
end

#connect(last_operator, last_operand) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/romanesco/elements/operator.rb', line 28

def connect(last_operator, last_operand)
  if last_operator && last_operator.is_a?(ParenthesesOperator) && last_operator.precedence > self.precedence
    connect_in_place(last_operator, last_operand)
  elsif last_operator && last_operator.is_a?(ParenthesesOperator) && self.is_a?(ParenthesesOperator)
    last_operator.connect_to_left(self)
  elsif last_operator && last_operator.parent && last_operator.precedence >= self.precedence
    connect_in_place_with_parent(last_operator, last_operand)
  elsif last_operator && last_operator.parent && last_operator.precedence < self.precedence
    connect_in_place_with_parent(last_operator, last_operand)
  elsif last_operator && last_operator.is_a?(ParenthesesOperator) && last_operator.precedence < self.precedence
    connect_up_tree(last_operator)
  elsif last_operator && last_operator.precedence >= self.precedence
    connect_up_tree(last_operator)
  elsif last_operator && self.is_a?(ParenthesesOperator)
    connect_to_right(last_operator)
  elsif last_operator
    connect_in_place(last_operator, last_operand)
  else
    connect_to_left(last_operand)
  end
end

#connect_in_place(last_operator, last_operand) ⇒ Object



55
56
57
58
# File 'lib/romanesco/elements/operator.rb', line 55

def connect_in_place(last_operator, last_operand)
  self.insert_element_to_left(last_operand)
  last_operator.insert_element_to_right(self)
end

#connect_in_place_with_parent(last_operator, last_operand) ⇒ Object



50
51
52
53
# File 'lib/romanesco/elements/operator.rb', line 50

def connect_in_place_with_parent(last_operator, last_operand)
  last_operator.parent.insert_element_to_right(self)
  self.insert_element_to_left(last_operand)
end

#connect_to_left(element) ⇒ Object



64
65
66
# File 'lib/romanesco/elements/operator.rb', line 64

def connect_to_left(element)
  self.insert_element_to_left(element)
end

#connect_to_right(element) ⇒ Object



68
69
70
# File 'lib/romanesco/elements/operator.rb', line 68

def connect_to_right(element)
  element.insert_element_to_right(self)
end

#connect_up_tree(last_operator) ⇒ Object



60
61
62
# File 'lib/romanesco/elements/operator.rb', line 60

def connect_up_tree(last_operator)
  self.insert_element_to_left(last_operator)
end

#default_precedenceObject

Raises:

  • (NotImplementedError)


24
25
26
# File 'lib/romanesco/elements/operator.rb', line 24

def default_precedence
  raise NotImplementedError
end

#evaluate(options) ⇒ Object

Raises:

  • (NotImplementedError)


8
9
10
# File 'lib/romanesco/elements/operator.rb', line 8

def evaluate(options)
  raise NotImplementedError
end

#precedenceObject



12
13
14
# File 'lib/romanesco/elements/operator.rb', line 12

def precedence
  @precedence || default_precedence
end

#precedence=(value) ⇒ Object



20
21
22
# File 'lib/romanesco/elements/operator.rb', line 20

def precedence=(value)
  @precedence = value
end