This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
continuation.py
114 lines (95 loc) · 3.45 KB
/
continuation.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from pypy.rlib.objectmodel import specialize
class Continuation(object):
__slots__ = ('prop', 'mark')
_immutable_ = True
def __init__(self):
pass
def is_done(self):
return False
def activate(*args):
raise NameError
class PropContinuation(Continuation):
__slots__ = ('prop', 'succ', 'fail')
_immutable_ = True
def __init__(self, prop, succ, fail, mark=False):
self.prop = prop
self.succ = succ
self.fail = fail
self.mark = mark
def activate(self, state):
import propositions
# grumble grumble special cases
if isinstance(self.prop, propositions.NonCachingProposition):
return self.prop.evaluate(state, self.succ, self.fail)
label = self.prop.label()
if label in state.labels:
if state.labels[label]:
return self.succ, self.fail, state
else:
return self.fail, self.succ, state
state.labels[label] = self.mark
m1 = MarkContinuation(self.prop, state, self.succ, self.succ, True)
m2 = MarkContinuation(self.prop, state, self.fail, self.fail, False)
return self.prop.evaluate(state, m1, m2)
class EUContinuation(PropContinuation):
__slots__ = ('states', 'i')
def __init__(self, prop, s, f):
PropContinuation.__init__(self, prop, s, f)
self.i = -1
self.states = None
def activate(self, state):
if self.states is None:
self.states = state.successors()
self.i = -1
self.i += 1
if self.i >= len(self.states):
return self.fail, self.succ, state
return PropContinuation(self.prop, self.succ, self), self.fail, self.states[self.i]
class EGContinuation(PropContinuation):
_immutable_fields_ = ["states"]
__slots__ = ('states', 'i')
def __init__(self, prop, s, f):
PropContinuation.__init__(self, prop, s, f)
self.i = -1
self.states = None
def activate(self, state):
if self.states is None:
self.states = state.successors()
self.i = -1
self.i += 1
if self.i >= len(self.states):
return self.succ, self.fail, state
return PropContinuation(self.prop, self, self.fail, True), self.fail, self.states[self.i]
class EXContinuation(PropContinuation):
_immutable_fields_ = ["states"]
__slots__ = ('states', 'i')
def __init__(self, prop, state, s, f):
PropContinuation.__init__(self, prop, s, f)
self.states = state.successors()
self.i = len(self.states)
def activate(self, state):
self.i -= 1
if self.i >= 0:
return PropContinuation(self.prop, self.succ, self), self.fail, self.states[self.i]
else:
return self.fail, self.succ, state
class MarkContinuation(PropContinuation):
_immutable_ = True
__slots__ = ('state')
@specialize.arg(5)
def __init__(self, prop, state, succ, fail, mark=False):
PropContinuation.__init__(self, prop, succ, fail)
self.state = state
self.mark = mark
def activate(self, state):
self.state.labels[self.prop.label()] = self.mark
return self.succ, self.fail, state
class EndContinuation(Continuation):
_immutable_ = True
__slots__ = ('result')
@specialize.arg(1)
def __init__(self, result):
Continuation.__init__(self)
self.result = result
def is_done(self):
return True