Class: MiniRuby::AST::UnlessExpressionNode

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

Overview

Represents an unless expression like ‘unless foo; a = 5; end`

Instance Attribute Summary collapse

Attributes inherited from Node

#span

Instance Method Summary collapse

Constructor Details

#initialize(condition:, then_body:, span: Span::ZERO) ⇒ UnlessExpressionNode

: (condition: ExpressionNode, then_body: Array, ?span: Span) -> void



628
629
630
631
632
# File 'lib/miniruby/ast.rb', line 628

def initialize(condition:, then_body:, span: Span::ZERO)
  @span = span
  @condition = condition
  @then_body = then_body
end

Instance Attribute Details

#conditionObject (readonly)

: ExpressionNode



622
623
624
# File 'lib/miniruby/ast.rb', line 622

def condition
  @condition
end

#then_bodyObject (readonly)

: Array



625
626
627
# File 'lib/miniruby/ast.rb', line 625

def then_body
  @then_body
end

Instance Method Details

#==(other) ⇒ Object

: (Object other) -> bool



635
636
637
638
639
640
# File 'lib/miniruby/ast.rb', line 635

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

  @condition == other.condition &&
    @then_body == other.then_body
end

#inspect(indent = 0) ⇒ Object

: (?Integer indent) -> String



660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/miniruby/ast.rb', line 660

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

  buff << "\n" << @condition.inspect(indent + 1)

  buff << "\n#{INDENT_UNIT * (indent + 1)}(then"
  @then_body.each do |stmt|
    buff << "\n" << stmt.inspect(indent + 2)
  end
  buff << ')'

  buff << ')'
  buff
end

#to_s(indent = 0) ⇒ Object

: (?Integer indent) -> String



644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/miniruby/ast.rb', line 644

def to_s(indent = 0)
  buff = String.new
  indent_str = INDENT_UNIT * indent
  buff << "#{indent_str}unless #{@condition}\n"

  @then_body.each do |stmt|
    buff << stmt.to_s(indent + 1)
  end

  buff << "#{indent_str}end"

  buff
end