Class: MiniRuby::AST::ProgramNode

Inherits:
Node
  • Object
show all
Defined in:
lib/miniruby/ast.rb

Overview

Represents a program

Instance Attribute Summary collapse

Attributes inherited from Node

#span

Instance Method Summary collapse

Constructor Details

#initialize(statements:, span: Span::ZERO) ⇒ ProgramNode

: (statements: Array, ?span: Span) -> void



47
48
49
50
# File 'lib/miniruby/ast.rb', line 47

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

Instance Attribute Details

#statementsObject (readonly)

: Array



44
45
46
# File 'lib/miniruby/ast.rb', line 44

def statements
  @statements
end

Instance Method Details

#==(other) ⇒ Object

: (Object other) -> bool



53
54
55
56
57
# File 'lib/miniruby/ast.rb', line 53

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

  @statements == other.statements
end

#inspect(indent = 0) ⇒ Object

: (?Integer indent) -> String



73
74
75
76
77
78
79
80
81
82
# File 'lib/miniruby/ast.rb', line 73

def inspect(indent = 0)
  buff = String.new

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

#to_s(indent = 0) ⇒ Object

: (?Integer indent) -> String



61
62
63
64
65
66
67
68
69
# File 'lib/miniruby/ast.rb', line 61

def to_s(indent = 0)
  buffer = String.new

  @statements.each do |stmt|
    buffer << stmt.to_s(indent)
  end

  buffer
end