Class: Janeway::AST::BinaryOperator
- Inherits:
-
Expression
- Object
- Expression
- Janeway::AST::BinaryOperator
- Defined in:
- lib/janeway/ast/binary_operator.rb
Overview
AST node for binary operators:
== != <= >= < > || &&
Constant Summary collapse
- OPERATOR_META =
Single source of truth for what binary operators exist and how they render / classify. Adding a new operator here fills both call sites.
{ and: { str: '&&', type: :logical }, or: { str: '||', type: :logical }, equal: { str: '==', type: :comparison }, not_equal: { str: '!=', type: :comparison }, less_than: { str: '<', type: :comparison }, less_than_or_equal: { str: '<=', type: :comparison }, greater_than: { str: '>', type: :comparison }, greater_than_or_equal: { str: '>=', type: :comparison }, }.freeze
Instance Attribute Summary collapse
-
#left ⇒ Object
Returns the value of attribute left.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#right ⇒ Object
Returns the value of attribute right.
Attributes inherited from Expression
Instance Method Summary collapse
-
#comparison_operator? ⇒ Boolean
True if this operator is a comparison operator.
-
#initialize(operator, left = nil, right = nil) ⇒ BinaryOperator
constructor
A new instance of BinaryOperator.
-
#logical_operator? ⇒ Boolean
True if this operator is a logical operator.
- #to_s ⇒ Object
- #tree(level) ⇒ Array
-
#validate! ⇒ Object
Validate the currently-set left and right operands as a well-formed binary expression.
Methods inherited from Expression
#chain_of?, #indented, #literal?, #singular_query?, #type, type_name
Constructor Details
#initialize(operator, left = nil, right = nil) ⇒ BinaryOperator
Returns a new instance of BinaryOperator.
11 12 13 14 15 16 17 18 |
# File 'lib/janeway/ast/binary_operator.rb', line 11 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 @left = left @right = right end |
Instance Attribute Details
#left ⇒ Object
Returns the value of attribute left.
9 10 11 |
# File 'lib/janeway/ast/binary_operator.rb', line 9 def left @left end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/janeway/ast/binary_operator.rb', line 8 def name @name end |
#right ⇒ Object
Returns the value of attribute right.
9 10 11 |
# File 'lib/janeway/ast/binary_operator.rb', line 9 def right @right end |
Instance Method Details
#comparison_operator? ⇒ Boolean
True if this operator is a comparison operator
52 53 54 |
# File 'lib/janeway/ast/binary_operator.rb', line 52 def comparison_operator? operator_type == :comparison end |
#logical_operator? ⇒ Boolean
True if this operator is a logical operator
58 59 60 |
# File 'lib/janeway/ast/binary_operator.rb', line 58 def logical_operator? operator_type == :logical end |
#to_s ⇒ Object
35 36 37 38 |
# File 'lib/janeway/ast/binary_operator.rb', line 35 def to_s # Make precedence explicit by adding parentheses "(#{@left} #{operator_to_s} #{@right})" end |
#tree(level) ⇒ Array
42 43 44 45 46 47 48 |
# File 'lib/janeway/ast/binary_operator.rb', line 42 def tree(level) [ indented(level, to_s), @left.tree(level + 1), @right.tree(level + 1), ] end |
#validate! ⇒ Object
Validate the currently-set left and right operands as a well-formed binary expression. Called by the parser once both sides are assigned — the previous per-setter validation was order-dependent (left= couldn't see right and vice-versa) and missed the both-literal case when the parser assigned left first.
27 28 29 30 31 32 33 |
# File 'lib/janeway/ast/binary_operator.rb', line 27 def validate! raise Error, 'BinaryOperator requires both left and right' unless @left && @right validate_side(@left) validate_side(@right) validate_pair end |