Class: DDQL::Operator

Inherits:
Object
  • Object
show all
Defined in:
lib/ddql/operator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol, name, type, precedence, right, return_type) ⇒ Operator

Returns a new instance of Operator.



5
6
7
8
9
10
11
12
# File 'lib/ddql/operator.rb', line 5

def initialize(symbol, name, type, precedence, right, return_type)
  @symbol        = symbol
  @name          = name
  @type          = type
  @precedence    = precedence
  @associativity = right ? :right : :left
  @return_type   = return_type
end

Instance Attribute Details

#associativity ⇒ Object (readonly)

Returns the value of attribute associativity.



3
4
5
# File 'lib/ddql/operator.rb', line 3

def associativity
  @associativity
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/ddql/operator.rb', line 3

def name
  @name
end

#precedence ⇒ Object (readonly)

Returns the value of attribute precedence.



3
4
5
# File 'lib/ddql/operator.rb', line 3

def precedence
  @precedence
end

#return_type ⇒ Object (readonly)

Returns the value of attribute return_type.



3
4
5
# File 'lib/ddql/operator.rb', line 3

def return_type
  @return_type
end

#symbol ⇒ Object (readonly)

Returns the value of attribute symbol.



3
4
5
# File 'lib/ddql/operator.rb', line 3

def symbol
  @symbol
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/ddql/operator.rb', line 3

def type
  @type
end

Instance Method Details

#any_type? ⇒ Boolean

Returns:



14
15
16
# File 'lib/ddql/operator.rb', line 14

def any_type?
  return_type == :any
end

#boolean? ⇒ Boolean

Returns:



18
19
20
# File 'lib/ddql/operator.rb', line 18

def boolean?
  return_type == :boolean
end

#comparison? ⇒ Boolean

Returns:



22
23
24
# File 'lib/ddql/operator.rb', line 22

def comparison?
  (infix? || postfix?) && boolean?
end

#complex_comparison? ⇒ Boolean

Returns:



26
27
28
# File 'lib/ddql/operator.rb', line 26

def complex_comparison?
  comparison? && !simple_comparison?
end

#infix? ⇒ Boolean

Returns:



30
31
32
# File 'lib/ddql/operator.rb', line 30

def infix?
  @name == :infixoperator || @type == :infix
end

#left? ⇒ Boolean

Returns:



34
35
36
# File 'lib/ddql/operator.rb', line 34

def left?
  !right?
end

#math? ⇒ Boolean

Returns:



38
39
40
# File 'lib/ddql/operator.rb', line 38

def math?
  math_op?(symbol)
end

#parse(parser, token, expression: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/ddql/operator.rb', line 42

def parse(parser, token, expression: nil)
  case type
  when :infix; parse_infix(parser, token, expression)
  when :prefix; parse_prefix(parser, token, expression)
  when :postfix; parse_postfix(parser, token, expression)
  else
    raise "unsupported operator type[#{type}]"
  end
end

#pattern ⇒ Object



52
53
54
# File 'lib/ddql/operator.rb', line 52

def pattern
  symbol
end

#postfix? ⇒ Boolean

Returns:



56
57
58
# File 'lib/ddql/operator.rb', line 56

def postfix?
  @name == :postfixoperator || @type == :postfix
end

#prefix? ⇒ Boolean

Returns:



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

def prefix?
  @name == :prefixoperator || @type == :prefix
end

#register(hash) ⇒ Object



64
65
66
67
# File 'lib/ddql/operator.rb', line 64

def register(hash)
  hash[symbol] = self
  hash
end

#right? ⇒ Boolean

Returns:



69
70
71
# File 'lib/ddql/operator.rb', line 69

def right?
  @associativity == :right
end

#simple_comparison? ⇒ Boolean

Returns:



73
74
75
76
77
78
79
80
# File 'lib/ddql/operator.rb', line 73

def simple_comparison?
  case symbol
  when "==", "=", "!=", "<=", "<", ">=", ">"
    true
  else
    false
  end
end

#type?(incoming) ⇒ Boolean

Returns:



82
83
84
# File 'lib/ddql/operator.rb', line 82

def type?(incoming)
  type == incoming
end