Module: Affect::Cont

Extended by:
Cont
Included in:
Cont
Defined in:
lib/affect/cont.rb

Constant Summary collapse

@@stack =

holds objects of the form [bool, Continuation] where bool siginifies a

[]

Instance Method Summary collapse

Instance Method Details

#abort(v) ⇒ Object



16
17
18
# File 'lib/affect/cont.rb', line 16

def abort(v)
  (@@stack.pop)[1].(v)
end

#capture(&block) ⇒ Object



20
21
22
23
24
25
# File 'lib/affect/cont.rb', line 20

def capture(&block)
  callcc { |outer|
    @@stack << [true, outer]
    abort(block.())
  }
end

#escapeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/affect/cont.rb', line 27

def escape
  callcc do |esc|
    unwound_continuations = unwind_stack
    cont_proc = lambda { |v|
      callcc do |ret|
        @@stack << [true, ret]
        unwound_continuations.each { |c| @@stack << [nil, c] }
        esc.call(v)
      end
    }
    abort(yield(cont_proc))
  end
end

#unwind_stackObject



41
42
43
44
45
46
47
# File 'lib/affect/cont.rb', line 41

def unwind_stack
unwound = []
  while @@stack.last && !(@@stack.last)[0]
    unwound << (@@stack.pop)[1]
  end
  unwound.reverse
end