Class: SelfML::AST::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



17
18
19
# File 'lib/selfml/ast.rb', line 17

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

Instance Attribute Details

#headObject

Returns the value of attribute head



15
16
17
# File 'lib/selfml/ast.rb', line 15

def head
  @head
end

#tailObject

Returns the value of attribute tail



15
16
17
# File 'lib/selfml/ast.rb', line 15

def tail
  @tail
end

Instance Method Details

#<=>(rhs) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/selfml/ast.rb', line 38

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

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/selfml/ast.rb', line 21

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
  
  "(#{head.serialize}#{tail})"
end