Class: MiniRuby::AST::WhileExpressionNode

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

Overview

Represents a while expression like ‘while 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) ⇒ WhileExpressionNode

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



686
687
688
689
690
# File 'lib/miniruby/ast.rb', line 686

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

Instance Attribute Details

#conditionObject (readonly)

: ExpressionNode



680
681
682
# File 'lib/miniruby/ast.rb', line 680

def condition
  @condition
end

#then_bodyObject (readonly)

: Array



683
684
685
# File 'lib/miniruby/ast.rb', line 683

def then_body
  @then_body
end

Instance Method Details

#==(other) ⇒ Object

: (Object other) -> bool



693
694
695
696
697
698
# File 'lib/miniruby/ast.rb', line 693

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

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

#inspect(indent = 0) ⇒ Object

: (?Integer indent) -> String



718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
# File 'lib/miniruby/ast.rb', line 718

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

  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



702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/miniruby/ast.rb', line 702

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

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

  buff << "#{indent_str}end"

  buff
end