-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol.cr
352 lines (270 loc) · 7.45 KB
/
protocol.cr
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
347
348
349
350
351
352
abstract struct ExpressionResult
end
record OkResult < ExpressionResult
record ErrResult < ExpressionResult, error : Lua::LuaError | ArgumentError, rule : Rule do
def index
rule.index
end
end
# Raised when a receiver cell wants to commit suicide.
class CommitSuicide < Exception
end
# Rules are named bit of computer code, in Synapse of Lua code.
abstract class Rule
def initialize(@lua : Excerpt)
end
def index
@lua.start
end
end
abstract class RuleSignature
end
class KeywordRuleSignature < RuleSignature
getter keyword, arity
def initialize(@keyword : String, @arity : Int32)
end
def_equals_and_hash keyword, arity
end
class HeartbeatRuleSignature < RuleSignature
getter period
def initialize(@period : Time::Span?)
end
def_equals_and_hash period
end
class WildcardSignature < RuleSignature
getter arity
def initialize(@arity : Int32)
end
def_equals_and_hash arity
end
class BirthRule < Rule
def result(receiver : Cell, message : Message)
end
def signature(*, to signatures)
end
def update(for cell : Cell, newer : BirthRule)
return self if same?(newer)
return self if @lua == newer.@lua
tmp = @lua
@lua = newer.@lua
# may happen if meaningless characters were added
unless tmp.string == @lua.string
express(cell)
end
self
end
def express(receiver : Cell)
# on-birth must be rerun for every copy separately!
receiver.each_relative do |cell|
cell.interpret result(cell)
end
end
def result(receiver : Cell) : ExpressionResult
stack = Lua::Stack.new
res = ExpressionContext.new(receiver)
res.fill(stack)
begin
stack.run(@lua.string, "birth")
OkResult.new
rescue e : Lua::LuaError
ErrResult.new(e, self)
rescue e : ArgumentError
ErrResult.new(e, self)
ensure
stack.close
end
end
end
class KeywordRule < Rule
getter keyword
def initialize(@keyword : Excerpt, @params : Array(Excerpt), lua : Excerpt)
super(lua)
@id = UUID.random
end
def bounds
end
def header_start
@keyword.start
end
def index
@keyword.start
end
def signature(*, to signatures)
signatures << signature
end
def signature
if keyword.string == "*"
WildcardSignature.new(@params.size)
else
KeywordRuleSignature.new(@keyword.string, @params.size)
end
end
def result(receiver : Cell, message : Message, attack = 0.0) : ExpressionResult
stack = Lua::Stack.new
res = MessageExpressionContext.new(receiver, message, attack)
res.fill(stack)
@params.zip(message.args) do |param, arg|
stack.set_global(param.string, arg)
end
begin
stack.run(@lua.string, @keyword.string)
result = OkResult.new
rescue e : Lua::LuaError
result = ErrResult.new(e, self)
rescue e : ArgumentError
result = ErrResult.new(e, self)
ensure
stack.close
end
end
def changed
end
def update(for cell : Cell, newer : KeywordRule)
return newer unless @keyword == newer.@keyword
return self if self == newer
@params = newer.@params
@lua = newer.@lua
@id = UUID.random
changed
self
end
def express(receiver : Cell, vesicle : Vesicle, attack : Float64)
result = result(receiver, vesicle.message, attack)
receiver.interpret(result)
end
def_equals_and_hash @id
end
class HeartbeatRule < KeywordRule
def initialize(keyword : Excerpt, lua : Excerpt, @period : Time::Span?)
super(keyword, [] of Excerpt, lua)
@clock = SF::Clock.new
end
def signature
HeartbeatRuleSignature.new(@period)
end
@corrupt = false
def changed
@corrupt = false
end
# Returns the amount of *pending* lapses for this heartbeat
# rule. Caps to *cap*.
def lapses(period : Time::Span, cap = 4)
delta = @clock.elapsed_time.as_milliseconds - period.total_milliseconds
return unless delta >= 0
# We might have missed some...
lapses = (delta / period.total_milliseconds).trunc + 1
Math.min(cap, lapses.to_i)
end
# Systole is the "body" of a cell's heartbeat: at systole,
# heartbeat rules are triggered.
def systole(for receiver : Cell)
# If heartbeat message was decided corrupt, then it shall
# not be run.
return if @corrupt
if period = @period
return unless count = lapses(period)
else
count = 1
end
result = uninitialized ExpressionResult
# Count is at all times at least = 1, therefore, result
# will be initialized.
count.times do
break if @corrupt
stack = Lua::Stack.new
# TODO: heartbeatresponsecontext, mainly to change period dynamically
res = ExpressionContext.new(receiver)
res.fill(stack)
begin
stack.run(@lua.string, "heartbeat:#{@period}")
result = OkResult.new
rescue e : Lua::LuaError
result = ErrResult.new(e, self)
rescue e : ArgumentError
result = ErrResult.new(e, self)
ensure
stack.close
end
@corrupt = result.is_a?(ErrResult)
end
result
end
# Dyastole resets heartbeat rules.
def dyastole(for receiver : Cell)
return unless period = @period
return unless lapses(period)
@clock.restart
end
end
class Protocol
def initialize
@rules = {} of RuleSignature => KeywordRule
@birth = BirthRule.new(Excerpt.new("", 0))
end
private def each_birth_rule
if birth = @birth
yield birth
end
end
def each_keyword_rule # FIXME: make this private, currently ProtocolEditor needs this
@rules.each_value do |kwrule|
yield kwrule
end
end
private def each_heartbeat_rule
@rules.each_value do |rule|
yield rule if rule.is_a?(HeartbeatRule)
end
end
private def fetch?(signature : RuleSignature)
@rules[signature]?.try { |rule| yield rule }
end
private def fetch?(signature : KeywordRuleSignature)
@rules[signature]?.try { |rule| yield rule }
@rules[WildcardSignature.new(signature.arity)]?.try { |rule| yield rule }
end
def on_memory_changed(cell : Cell)
each_heartbeat_rule &.changed
end
def systole(cell : Cell, tank : Tank)
each_heartbeat_rule do |hb|
result = hb.systole(for: cell)
if result.is_a?(ErrResult)
cell.fail(result, in: tank)
end
end
end
def dyastole(cell : Cell, tank : Tank)
each_heartbeat_rule &.dyastole(for: cell)
end
def update(for cell : Cell, newer : KeywordRule) # FIXME: callers shoudn't be aware of rules!
fetch?(newer.signature) do |prev|
@rules[newer.signature] = prev.update(cell, newer)
return
end
@rules[newer.signature] = newer
end
def update(for cell : Cell, newer : BirthRule) # FIXME: callers shoudn't be aware of rules!
@birth = @birth.update(cell, newer)
end
def rewrite(signatures : Set(RuleSignature)) # FIXME: callers shoudn't be aware of rule signatures!
new_rules = {} of RuleSignature => KeywordRule
# Get rid of those decls that are not in the signatures set.
signatures.each do |signature|
new_rules[signature] = @rules[signature]
end
@rules = new_rules
end
def express(receiver : Cell, vesicle : Vesicle)
fetch?(KeywordRuleSignature.new(vesicle.keyword, vesicle.nargs)) do |rule|
# Attack is a heading pointing hdg the vesicle.
delta = (vesicle.mid - receiver.mid)
attack = Math.atan2(-delta.y, delta.x)
rule.express(receiver, vesicle, attack)
end
end
def born(receiver : Cell)
@birth.express(receiver)
end
end