Class: RazyK::Node

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

Overview

Combinator Expression is implemented as DAG (Directed Acyclic Graph) There is two type of node

Combinator - leaf node which has no child node.
             express elemental combinator.
Pair       - non leaf node which always has 2 child node.
             express term of two combinators or terms

ex) In pair expression like Lisp/Scheme ““ski” => ( ( S . K ) . I ) “‘s`ki” => ( S . ( K . I ) )

S, K, I are Combinator
(S . K) is Pair

Direct Known Subclasses

Combinator, Pair

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, from = [], to = []) ⇒ Node

Returns a new instance of Node.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/razyk/node.rb', line 19

def initialize(label, from=[], to=[])
  @label = label
  @from = []
  @to = []
  from.each do |f|
    self.class.connect(f, self)
  end
  to.each do |t|
    self.class.connect(self, t)
  end
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



30
31
32
# File 'lib/razyk/node.rb', line 30

def from
  @from
end

#labelObject (readonly)

Returns the value of attribute label.



30
31
32
# File 'lib/razyk/node.rb', line 30

def label
  @label
end

#toObject (readonly)

Returns the value of attribute to.



30
31
32
# File 'lib/razyk/node.rb', line 30

def to
  @to
end

Class Method Details

.connect(a, b) ⇒ Object

create connectivity from a to b (a -> b) TODO: circularity check



34
35
36
37
# File 'lib/razyk/node.rb', line 34

def self.connect(a, b)
  a.to.push(b)
  b.from.push(a)
end

.disconnect(a, b) ⇒ Object

destroy connectivity from a to b (a -x-> b)



40
41
42
43
# File 'lib/razyk/node.rb', line 40

def self.disconnect(a, b)
  a.to.delete(b)
  b.from.delete(a)
end

.list(*args, terminator: nil, memory: {}) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/razyk/node.rb', line 78

def self.list(*args, terminator: nil, memory: {})
  cons = lambda{|x, y| Pair.cons(x, y, memory) }
  term = (terminator || cons[:K, 256])
  args.reverse.each do |i|
    term = cons[cons[:S, cons[cons[:S, :I], cons[:K, i]]], cons[:K, term]]
  end
  term
end

Instance Method Details

#as_jsonObject



74
75
76
# File 'lib/razyk/node.rb', line 74

def as_json
  {name: @label.to_s}
end

#integerObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/razyk/node.rb', line 62

def integer
  unless integer?
    raise "#{self} is not a integer"
  end
  case @label
  when Integer
    @label
  else
    Integer(@label.to_s)
  end
end

#integer?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/razyk/node.rb', line 58

def integer?
  @label.is_a?(Integer) or (/\A\d+\z/ =~ @label.to_s)
end

#replace(new_node) ⇒ Object

replace parent nodes’ reference of self to new_node



46
47
48
49
50
# File 'lib/razyk/node.rb', line 46

def replace(new_node)
  @from.dup.each do |f|
    f.replace_child(self, new_node)
  end
end

#replace_child(a, b) ⇒ Object

replace child node from a to b



53
54
55
56
# File 'lib/razyk/node.rb', line 53

def replace_child(a, b)
  self.class.disconnect(self, a)
  self.class.connect(self, b)
end