Class: SelfML::AST::Node

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

Overview

SelfML::AST::Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(head, tail = []) ⇒ Node

Returns a new instance of Node

Parameters:

  • head (String)

    the head of the node.

  • tail (Array) (defaults to: [])

    the tail of the node. optional.


10
11
12
# File 'lib/selfml/ast/node.rb', line 10

def initialize( head , tail = [] )
  @head , @tail = head , tail
end

Instance Attribute Details

#headObject

Returns the value of attribute head


6
7
8
# File 'lib/selfml/ast/node.rb', line 6

def head
  @head
end

#tailObject

Returns the value of attribute tail


6
7
8
# File 'lib/selfml/ast/node.rb', line 6

def tail
  @tail
end

Instance Method Details

#<=>(rhs) ⇒ Object

Parameters:


35
36
37
38
39
# File 'lib/selfml/ast/node.rb', line 35

def <=> rhs
  case rhs when Node       then  0
           when StringNode then  1
                           else -1 end
end

#serialize(level = 1) ⇒ Object Also known as: to_s

Parameters:

  • level (Integer) (defaults to: 1)

    used in recursion


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/selfml/ast/node.rb', line 15

def serialize( level = 1 )
  tail = @tail.sort.map do |t|
    case t
    when Node
      "\n" + " "*level*4 + (t.serialize level+1)
    when StringNode
      " "+t.serialize
    else
      nil
    end
  end
  
  # puts "head", head, @head.serialize
  # puts "tail", tail
  
  "(" + head.serialize + tail.join("") + ")"
end