Class: MiniRuby::AST::BinaryExpressionNode

Inherits:
ExpressionNode show all
Defined in:
lib/miniruby/ast.rb

Overview

Represents an expression with a binary operator like ‘2 + 3`, `a == 4`, `5 < 2`

Instance Attribute Summary collapse

Attributes inherited from Node

#span

Instance Method Summary collapse

Constructor Details

#initialize(operator:, left:, right:, span: Span::ZERO) ⇒ BinaryExpressionNode

: (operator: Token, left: ExpressionNode, right: ExpressionNode, ?span: Span) -> void



174
175
176
177
178
179
# File 'lib/miniruby/ast.rb', line 174

def initialize(operator:, left:, right:, span: Span::ZERO)
  @span = span
  @operator = operator
  @left = left
  @right = right
end

Instance Attribute Details

#leftObject (readonly)

: ExpressionNode



168
169
170
# File 'lib/miniruby/ast.rb', line 168

def left
  @left
end

#operatorObject (readonly)

: Token



165
166
167
# File 'lib/miniruby/ast.rb', line 165

def operator
  @operator
end

#rightObject (readonly)

: ExpressionNode



171
172
173
# File 'lib/miniruby/ast.rb', line 171

def right
  @right
end

Instance Method Details

#==(other) ⇒ Object

: (Object other) -> bool



182
183
184
185
186
187
188
# File 'lib/miniruby/ast.rb', line 182

def ==(other)
  return false unless other.is_a?(BinaryExpressionNode)

  @operator == other.operator &&
    @left == other.left &&
    @right == other.right
end

#inspect(indent = 0) ⇒ Object

: (?Integer indent) -> String



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/miniruby/ast.rb', line 198

def inspect(indent = 0)
  buff = String.new
  buff << "#{INDENT_UNIT * indent}(bin_expr"

  buff << "\n#{INDENT_UNIT * (indent + 1)}#{@operator}"
  buff << "\n" << @left.inspect(indent + 1)
  buff << "\n" << @right.inspect(indent + 1)

  buff << ')'
  buff
end

#to_s(indent = 0) ⇒ Object

: (?Integer indent) -> String



192
193
194
# File 'lib/miniruby/ast.rb', line 192

def to_s(indent = 0)
  "#{INDENT_UNIT * indent}#{@left} #{@operator} #{@right}"
end