Class: Electr::AST

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

Overview

Base class for the abstract syntax tree.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ AST

Returns a new instance of AST.



6
7
8
9
10
11
12
13
14
# File 'lib/electr/ast/ast.rb', line 6

def initialize(name)
  if name
    @name = name
  else
    @name = self.class.name.split('::').last
  end
  @children = []
  @value = nil
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



16
17
18
# File 'lib/electr/ast/ast.rb', line 16

def children
  @children
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/electr/ast/ast.rb', line 16

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



16
17
18
# File 'lib/electr/ast/ast.rb', line 16

def value
  @value
end

Instance Method Details

#add_child(child) ⇒ Object

Public: Add a child node to the end of the children’s list.

child - An AST node to add to the list of children.

Returns self.



23
24
25
26
# File 'lib/electr/ast/ast.rb', line 23

def add_child child
  @children << child
  self
end

#display(indent = 0) ⇒ Object



37
38
39
40
41
42
# File 'lib/electr/ast/ast.rb', line 37

def display(indent = 0)
  print name_for_display(indent)
  print value_for_display
  puts
  @children.each {|child| child.display(indent + 2) }
end

#leaf?Boolean

Public: Returns True if this node is a leaf.

Returns:

  • (Boolean)


29
30
31
# File 'lib/electr/ast/ast.rb', line 29

def leaf?
  @children.empty?
end

#sizeObject



33
34
35
# File 'lib/electr/ast/ast.rb', line 33

def size
  @children.size
end

#to_pnObject



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

def to_pn
  [* Pn.new(@value, @name)] + @children.map {|child| child.to_pn }.flatten
end