arcs.py
13 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
from functools import reduce
from operator import and_, or_
from itertools import chain
from . import ConstraintError
from .data import record
from .utils import autorepr
class Arc (object) :
def _nest (self, *arcs, **more) :
for a in chain(arcs, more.values()) :
if (not any(isinstance(a, base)
for base in self.__class__.__bases__)
or not any(isinstance(self, allowed)
for allowed in a._nestable_in)) :
raise ConstraintError("cannot nest %r into %s"
% (a, self.__class__.__name__))
self._nestable_in = reduce(and_,
(a._nestable_in for a in chain(arcs, more.values())),
self._nestable_in)
self._order = max(a._order for a in chain(arcs, more.values())) + 1
self.__dict__.update(more)
return arcs
def __add__ (self, other) :
return MultiArc(self, other)
def __sub__ (self, other) :
return NotImplementedError("%s does not support substraction"
% self.__class__.__name__)
class InputArc (Arc) :
pass
class OutputArc (Arc) :
pass
class _Ast (object) :
def _bind (self, type, ctx, name=None) :
if self.source in ctx.bound :
return ctx.bound[self.source], False
elif name is None :
name = ctx.bound[self.source] = ctx.declare.new(type)
return name, True
else :
ctx.declare[name] = type
ctx.bound[self.source] = name
return name, True
class _AstBindTestConsume (_Ast) :
def __astin__ (self, nest, place, ctx, **more) :
ctx.notempty.add(place)
varname, isnew = self._bind(place.type, ctx)
ctx.sub[place.name].append(varname)
if isnew :
nest.append(ctx.Assign(varname, ctx.Expr(self.source), **more))
node = ctx.IfToken(ctx.marking, place, varname, **more)
nest.append(node)
return node.body
def __astnotin__ (self, nest, place, ctx, guard, **more) :
varname, isnew = self._bind(place.type, ctx)
if isnew :
nest.append(ctx.Assign(varname, ctx.Expr(self.source), **more))
node = ctx.IfNoToken(ctx.marking, place, varname, **more)
nest.append(node)
return node.body
class _AstTypeProduce (_Ast) :
def __astout__ (self, nest, place, ctx, **more) :
varname, isnew = self._bind(place.type, ctx)
ctx.add[place.name].append(varname)
if isnew :
nest.append(ctx.Assign(varname, ctx.Expr(self.source), **more))
node = ctx.IfType(place, varname, **more)
nest.append(node)
return node.body
@autorepr
class Value (InputArc, OutputArc, _AstBindTestConsume, _AstTypeProduce) :
_order = 0
def __init__ (self, value) :
self.source = value
def copy (self) :
return self.__class__(self.source)
def vars (self) :
return set()
@autorepr
class Expression (InputArc, OutputArc, _AstBindTestConsume, _AstTypeProduce) :
_order = 20
def __init__ (self, code) :
self.source = code
def copy (self) :
return self.__class__(self.source)
def vars (self) :
return set()
@autorepr
class Variable (InputArc, OutputArc, _AstTypeProduce) :
_order = 10
def __init__ (self, name) :
self.source = name
def copy (self) :
return self.__class__(self.source)
def vars (self) :
return {self.source}
def __astin__ (self, nest, place, ctx, **more) :
ctx.notempty.add(place)
varname, isnew = self._bind(place.type, ctx, self.source)
ctx.sub[place.name].append(varname)
if isnew :
node = ctx.ForeachToken(ctx.marking, place, varname, **more)
else :
node = ctx.IfToken(ctx.marking, place, varname, **more)
nest.append(node)
return node.body
def __astnotin__ (self, nest, place, ctx, guard, **more) :
varname, isnew = self._bind(place.type, ctx, self.source)
if isnew and not guard :
node = ctx.IfEmpty(ctx.marking, place, **more)
del ctx.declare[varname]
elif isnew :
node = ctx.IfNoTokenSuchThat(ctx.marking, place, varname,
ctx.Expr(guard), **more)
elif guard :
raise ConstraintError("inhibition guard forbidden for %r"
" (bounded on another arc)" % self)
else :
node = ctx.IfNoToken(ctx.marking, place, varname, **more)
nest.append(node)
return node.body
class MultiArc (InputArc, OutputArc) :
def __init__ (self, first, *others) :
self.components = self._nest(first, *others)
def copy (self) :
return self.__class__(*(c.copy() for c in self.components))
def __repr__ (self) :
return "%s(%s)" % (self.__class__.__name__,
", ".join(repr(c) for c in self.components))
def vars (self) :
return reduce(or_, (c.vars() for c in self.components), set())
def __add__ (self, other) :
if isinstance(other, MultiArc) :
return self.__class__(*self.components, *other.components)
else :
return self.__class__(*self.components, other)
def __sub__ (self, other) :
if isinstance(other, MultiArc) :
todo = other.components
else :
todo = [other]
keep = list(self.components)
for arc in todo :
keep.remove(arc)
return self.__class__(*keep)
@autorepr
class Flush (InputArc, _Ast) :
_order = 15
def __init__ (self, name, notempty=False) :
self.source = name
self.notempty = notempty
def copy (self) :
return self.__class__(self.source, self.notempty)
def vars (self) :
return {self.source}
def __astin__ (self, nest, place, ctx, **more) :
if self.notempty :
ctx.notempty.add(place)
varname, isnew = self._bind(("mset", place.type), ctx, self.source)
ctx.sub[place.name].append(("mset", varname))
if isnew :
node = ctx.GetPlace(varname, ctx.marking, place, **more)
ret = nest
else :
node = ctx.IfPlace(ctx.marking, place, varname, **more)
ret = node.body
nest.append(node)
return ret
@autorepr
class Fill (OutputArc, _Ast) :
_order = 30
def __init__ (self, code) :
self.source = code
def copy (self) :
return self.__class__(self.source)
def vars (self) :
return set()
def __astout__ (self, nest, place, ctx, **more) :
varname, isnew = self._bind(("mset", place.type), ctx)
ctx.add[place.name].append(("mset", varname))
if isnew :
nest.append(ctx.Assign(varname, ctx.Expr(self.source), **more))
node = ctx.IfAllType(place, varname, **more)
nest.append(node)
return node.body
@autorepr
class Test (InputArc, OutputArc) :
def __init__ (self, label) :
self._nest(label=label)
def copy (self) :
return self.__class__(self.label)
def vars (self) :
return self.label.vars()
def __astin__ (self, nest, place, ctx, **more) :
if place.name in ctx.sub :
pos = len(ctx.sub[place.name])
else :
pos = 0
newnest = self.label.__astin__(nest, place, ctx, **more)
if place.name in ctx.sub :
tokens = ctx.sub[place.name]
new = len(tokens)
ctx.test[place.name].extend(tokens[pos - new:])
del tokens[pos - new:]
return newnest
def __astout__ (self, nest, place, ctx, **more) :
if place.name in ctx.add :
pos = len(ctx.add[place.name])
else :
pos = 0
newnest = self.label.__astout__(nest, place, ctx, **more)
if place.name in ctx.add :
tokens = ctx.add[place.name]
new = len(tokens)
del tokens[pos - new:]
return newnest
@autorepr
class Inhibitor (InputArc) :
def __init__ (self, label, guard=None) :
if isinstance(label, _AstBindTestConsume) and guard :
raise ConstraintError("inhibition guard forbidden together with %s"
% label)
self._nest(label=label)
self.guard = guard
def copy (self) :
return self.__class__(self.label, self.guard)
def vars (self) :
return set()
def __astin__ (self, nest, place, ctx, **more) :
return self.label.__astnotin__(nest, place, ctx, self.guard, **more)
class Tuple (InputArc, OutputArc) :
def __init__ (self, first, *others) :
self.components = self._nest(first, *others)
def copy (self) :
return self.__class__(*(c.copy() for c in self.components))
def __repr__ (self) :
return "%s(%s)" % (self.__class__.__name__,
", ".join(repr(c) for c in self.components))
def vars (self) :
return reduce(or_, (c.vars() for c in self.components), set())
def _walk (self, *others) :
l = len(self.components)
for t in others :
if not isinstance(t, tuple) :
raise ValueError("not a tuple %r" % t)
elif len(t) != l :
raise ConstraintError("tuple length do not match")
for i, (first, *z) in enumerate(zip(self.components, *others)) :
if isinstance(first, Tuple) :
for p, t in first._walk(*z) :
yield [i] + p, t
else :
yield [i], [first] + z
def _fold (self, items) :
slots = [None] * len(self.components)
for path, label in items :
if len(path) == 1 :
slots[path[0]] = label
elif slots[path[0]] is None :
slots[path[0]] = [(path[1:], label)]
else :
slots[path[0]].append((path[1:], label))
return tuple(c._fold(s) if isinstance(c, Tuple) else s
for c, s in zip(self.components, slots))
def _none (self) :
return tuple(c._none() if isinstance(c, Tuple) else None
for c in self.components)
def matchtype (self, type) :
try :
list(self._walk(type))
except :
return False
return True
def __astin__ (self, nest, place, ctx, **more) :
ctx.notempty.add(place)
match = []
guard = []
placetype = place.type or self._none()
pvar = ctx.declare.new(place.type)
nest.append(ctx.ForeachToken(ctx.marking, place, pvar, **more))
nest = nest[-1].body
if not self.matchtype(place.type) :
nest.append(ctx.IfTuple(record(name=place.name, type=placetype),
pvar, **more))
nest = nest[-1].body
for path, (label, type) in self._walk(placetype) :
if isinstance(label, (Value, Expression)) or label.source in ctx.bound :
var = ctx.declare.new(type)
nest.append(ctx.AssignItem(var, pvar, path, **more))
guard.append(ctx.Eq(ctx.Expr(var), ctx.Expr(label.source), **more))
else :
var = label.source
nest.append(ctx.AssignItem(var, pvar, path, **more))
ctx.declare[var] = type
match.append((path, var))
ctx.bound[label.source] = var
ctx.sub[place.name].append(ctx.Pattern(self, self._fold(match), placetype))
if guard :
node = ctx.If(ctx.And(guard, **more), **more)
nest.append(node)
nest = node.body
return nest
def __astnotin__ (self, nest, place, ctx, guard, **more) :
notempty = place not in ctx.notempty
bvar = ctx.declare.new(ctx.net.lang.BOOL)
nest.append(ctx.Assign(bvar, ctx.TrueConst(), **more))
child = self.__astin__(nest, place, ctx, **more)
ctx.sub[place.name].pop(-1)
if notempty :
ctx.notempty.discard(place)
child.append(ctx.Assign(bvar, ctx.FalseConst(), **more))
child.append(ctx.Break(**more))
nest.append(ctx.If(ctx.Expr(bvar), **more))
return nest[-1].body
def __astout__ (self, nest, place, ctx, **more) :
toadd = []
placetype = place.type or self._none()
for path, (label, type) in self._walk(placetype) :
nest = label.__astout__(nest, record(name=place.name, type=type), ctx,
**more)
toadd.append((path, ctx.add[place.name].pop(-1)))
ctx.add[place.name].append(ctx.Pattern(self, self._fold(toadd), placetype))
return nest
MultiArc._nestable_in = set()
Test._nestable_in = {MultiArc}
Inhibitor._nestable_in = {MultiArc}
Value._nestable_in = {Tuple, Test, Inhibitor, MultiArc}
Variable._nestable_in = {Tuple, Test, Inhibitor, MultiArc}
Expression._nestable_in = {Tuple, Test, Inhibitor, MultiArc}
Tuple._nestable_in = {Tuple, Test, Inhibitor, MultiArc}
Flush._nestable_in = {Test}
Fill._nestable_in = {Test, MultiArc}