forked from ceu-lang/ceu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawaits.lua
More file actions
227 lines (202 loc) · 5.13 KB
/
Copy pathawaits.lua
File metadata and controls
227 lines (202 loc) · 5.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
function SAME (me, sub)
local sub = sub or me[#me]
me.aw.n = sub.aw.n
me.aw.t = sub.aw.t
me.aw.forever_ = sub.aw.forever_
end
function U (s1, s2)
for i, v in ipairs(s2) do
s1[#s1+1] = v
end
for k,v in pairs(s2) do
if type(k) ~= 'number' then
s1[k] = v
end
end
end
function MIN (a, b)
return (a < b) and a or b
end
_AWAITS = {
n = 0, -- number of global awaits
}
F = {
Node_pre = function (me)
me.aw = {
n = 0,
t = {},
forever_ = false,
}
end,
Node = function (me)
if not F[me.tag] then
if _AST.isNode(me[#me]) then
SAME(me)
end
end
end,
Root = function (me)
for _, cls in ipairs(me) do
_AWAITS.n = _AWAITS.n + #cls.aw.t
end
end,
-- TODO: remove all!
--[[
Node_pos = function (me)
if me.aw.n == 1 then
if me.depth >= 1 then
U(_AST.iter()().aw.t, me.aw.t)
end
end
end,
Root = function (me)
for _, cls in ipairs(me) do
if cls.aw.forever_ and cls.glbs then
for awt in pairs(cls.aw.t) do
local id = awt[1].evt
_AWAITS.t[id] = {} -- new glb candidate
ALL[awt] = nil -- remove them from non-glb
end
end
end
for awt in pairs(ALL) do
local id = awt[1].evt
if _AWAITS.t[id] then
_AWAITS.t[id] = nil -- remove non-glb from glb
end
end
local t = {}
for id in pairs(_AWAITS.t) do
_AWAITS.n = _AWAITS.n + 1
t[#t+1] = id.id
end
DBG('Global awaits: ', table.concat(t,', '))
-- disable global awaits
--for id in pairs(_AWAITS.t) do
--_AWAITS.t[id] = nil
--end
end,
]]
Stmts = function (me)
local last = me[#me]
if not last then
return
end
-- if the last runs forever, all previous must be tight
if last.aw.forever_ then
for i=1, #me-1 do
local sub = me[i]
if sub.aw.n > 0 then
me.aw.n = 2
me.aw.t = {}
return
end
end
SAME(me)
-- otherwise, add all
else
F.ParAnd(me)
end
end,
ParAnd = function (me)
me.aw.n = 0
for _, sub in ipairs(me) do
me.aw.n = me.aw.n + sub.aw.n
U(me.aw.t, sub.aw.t)
end
if me.aw.n > 1 then
me.aw.t = {} -- no candidates
end
end,
ParEver = function (me)
for _, sub in ipairs(me) do
if sub.aw.forever_ then
U(me.aw.t, sub.aw.t)
end
end
me.aw.n = 2
me.aw.forever_ = true
end,
If = function (me)
local c, t, f = unpack(me)
local t_aw_n = t and t.aw.n or 0
local f_aw_n = f and f.aw.n or 0
if t_aw_n == f_aw_n then
me.aw.n = t_aw_n
if me.aw.n == 1 then
U(me.aw.t, t.aw.t)
U(me.aw.t, f.aw.t)
end
else
me.aw.n = 2 -- avoid inner and following awaits
me.aw.t = {}
end
me.aw.forever_ = t and f and t.aw.forever_ and f.aw.forever_
end,
ParOr = function (me)
me.aw.n = me[1].aw.n
for _, sub in ipairs(me) do
me.aw.n = MIN(me.aw.n, sub.aw.n)
if sub.aw.forever_ or sub.aw.n==1 then
U(me.aw.t, sub.aw.t)
end
end
if me.aw.n == 0 then
me.aw.n = 1 -- assumes it always awaits (avoid tight loops)
end
end,
-- TODO: breaks e rets dentro de ParEver, Loop
Escape = function (me)
_AST.iter'SetBlock'().aw.forever_ = false
end,
SetBlock_pre = 'Loop_pre',
SetBlock = 'Loop',
Break = function (me)
_AST.iter'Loop'().aw.forever_ = false
end,
Loop_pre = function (me)
me.aw.forever_ = true
end,
Loop = function (me)
local sub = (me.tag=='Loop' and me[1]) or me[2]
me.aw.n = sub.aw.n
me.aw.t = sub.aw.t
-- but not forever_
if not me.aw.forever_ then
me.aw.n = 2
me.aw.t = {}
end
if me.aw.n == 0 then
me.aw.n = 1 -- assumes it always awaits (avoid tight loops)
end
end,
SetAwait = function (me)
SAME(me, me[2])
end,
AwaitT = 'AwaitExt',
AwaitInt = 'AwaitExt',
AwaitExt = function (me)
me.aw.n = 1
me.aw.t[ #me.aw.t+1 ] = me
end,
--[=[
-- TODO: remove
AwaitInt = function (me)
if (not me[1].org) or (me[1].org.tag=='This') then -- only local awaits
-- TODO: why?
F.AwaitExt(me)
end
end,
]=]
Async = function (me)
me.aw.n = 1
end,
AwaitT = function (me)
me.aw.n = 1
end,
AwaitN = function (me)
me.aw.n = 1
me.aw.forever_ = true
end,
}
_AST.visit(F)