Class: MiniRuby::AST::ModifierExpressionNode

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

Overview

Represents a modifier expression like ‘return 5 if foo`

Instance Attribute Summary collapse

Attributes inherited from Node

#span

Instance Method Summary collapse

Constructor Details

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

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



223
224
225
226
227
228
# File 'lib/miniruby/ast.rb', line 223

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

Instance Attribute Details

#leftObject (readonly)

: ExpressionNode



217
218
219
# File 'lib/miniruby/ast.rb', line 217

def left
  @left
end

#operatorObject (readonly)

: Token



214
215
216
# File 'lib/miniruby/ast.rb', line 214

def operator
  @operator
end

#rightObject (readonly)

: ExpressionNode



220
221
222
# File 'lib/miniruby/ast.rb', line 220

def right
  @right
end

Instance Method Details

#==(other) ⇒ Object

: (Object other) -> bool



231
232
233
234
235
236
237
# File 'lib/miniruby/ast.rb', line 231

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

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

#inspect(indent = 0) ⇒ Object

: (?Integer indent) -> String



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/miniruby/ast.rb', line 247

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

  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



241
242
243
# File 'lib/miniruby/ast.rb', line 241

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