~tim/scheme-vm

ref: d947890410d3f5830161c11c89fdcb49aca9c10c scheme-vm/vm/pair.rb -rw-r--r-- 950 bytes
d9478904Tim Morgan Fix ruby-version file 6 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class VM
  class Pair
    include Enumerable

    attr_reader :address
    attr_accessor :next_node

    def initialize(address, next_node, heap:)
      @address = address
      @next_node = next_node
      @heap = heap
    end

    def raw
      to_a
    end

    def to_s
      "(#{raw.map(&:raw).join(' ')})"
    end

    def each
      current = self
      yield @heap[current.address]
      while (next_address = current.next_node)
        current = @heap[next_address]
        break if current == EmptyList.instance
        yield @heap[current.address]
      end
    end

    def size
      @size ||= to_a.size
    end

    def empty?
      false
    end

    def to_ruby
      to_a.tap do |ary|
        ary.each_with_index do |part, index|
          ary[index] = part.to_ruby
        end
      end
    end

    def inspect
      "#<VM::Pair @address=#{@address}, @next_node=#{@next_node}, @value=#{@heap[@address].inspect}>"
    end
  end
end