Class: Janeway::AST::BinaryOperator

Inherits:
Expression show all
Defined in:
lib/janeway/ast/binary_operator.rb

Overview

AST node for binary operators:

== != <= >= < > || &&

Instance Attribute Summary collapse

Attributes inherited from Expression

#next, #value

Instance Method Summary collapse

Methods inherited from Expression

#indented, #literal?, #singular_query?, #type

Constructor Details

#initialize(operator, left = nil, right = nil) ⇒ BinaryOperator

Returns a new instance of BinaryOperator.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
# File 'lib/janeway/ast/binary_operator.rb', line 10

def initialize(operator, left = nil, right = nil)
  super(nil)
  raise ArgumentError, "expect symbol, got #{operator.inspect}" unless operator.is_a?(Symbol)

  @name = operator # eg. :equal
  self.left = left if left
  self.right = right if right
end

Instance Attribute Details

#leftObject

Returns the value of attribute left.



8
9
10
# File 'lib/janeway/ast/binary_operator.rb', line 8

def left
  @left
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/janeway/ast/binary_operator.rb', line 8

def name
  @name
end

#rightObject

Returns the value of attribute right.



8
9
10
# File 'lib/janeway/ast/binary_operator.rb', line 8

def right
  @right
end

Instance Method Details

#comparison_operator?Boolean

True if this operator is a comparison operator

Returns:



69
70
71
# File 'lib/janeway/ast/binary_operator.rb', line 69

def comparison_operator?
  operator_type == :comparison
end

#logical_operator?Boolean

True if this operator is a logical operator

Returns:



75
76
77
# File 'lib/janeway/ast/binary_operator.rb', line 75

def logical_operator?
  operator_type == :logical
end

#to_sObject



52
53
54
55
# File 'lib/janeway/ast/binary_operator.rb', line 52

def to_s
  # Make precedence explicit by adding parentheses
  "(#{@left} #{operator_to_s} #{@right})"
end

#tree(level) ⇒ Array

Parameters:

  • level (Integer)

Returns:

  • (Array)


59
60
61
62
63
64
65
# File 'lib/janeway/ast/binary_operator.rb', line 59

def tree(level)
  [
    indented(level, to_s),
    @left.tree(level + 1),
    @right.tree(level + 1),
  ]
end