-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelimr.rb
More file actions
62 lines (52 loc) · 1.35 KB
/
delimr.rb
File metadata and controls
62 lines (52 loc) · 1.35 KB
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
53
54
55
56
57
58
59
60
61
62
require "delimr/version"
require "continuation"
module DelimR
class Hole
attr_accessor :ref, :marked
def initialize(ref, marked)
@ref = ref
@marked = marked
end
end
# I don't understand the semantics of these yet, but according to Oleg Kiselyov, they capture all four F operators.
# I don't yet grok the differences between reset/shift and prompt/control
@@is_shift = true
@@keep_delimiter_upon_effect = true
@@holes = []
def self.prompt(&block)
callcc do |outer_k|
@@holes.push Hole.new(outer_k, true)
abort_top(yield)
end
end
def self.control(&block)
callcc do |k_control|
holes_prefix = unwind_till_marked || []
holes_prefix.reverse
invoke_subcont = lambda { |v| #What is v here?
callcc do |k_return|
@@holes.push Hole.new(k_return, @@is_shift)
holes_prefix.each { |h| @@holes.push(h) }
k_control.call(v)
end
}
abort_top(yield(invoke_subcont))
end
end
module_function
def abort_top(v)
@@holes.pop.ref.call(v)
end
def unwind_till_marked
if @@holes.empty?
raise "No prompt set"
end
result = []
while !@@holes.empty? && !@@holes.last.marked
result << @@holes.pop
end
unless @@keep_delimiter_upon_effect
@@holes.last = Hole.new(hole.ref, false)
end
end
end