forked from ceu-lang/ceu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathana.lua
More file actions
308 lines (274 loc) · 7.86 KB
/
Copy pathana.lua
File metadata and controls
308 lines (274 loc) · 7.86 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
-- TODO: rename to flow
_ANA = {
ana = {
isForever = nil,
reachs = 0, -- unexpected reaches
unreachs = 0, -- unexpected unreaches
},
}
-- avoids counting twice (due to loops)
-- TODO: remove
local __inc = {}
function INC (me, c)
if __inc[me] then
return true
else
_ANA.ana[c] = _ANA.ana[c] + 1
__inc[me] = true
return false
end
end
-- [false] => never terminates
-- [true] => terminates w/o event
function OR (me, sub, short)
-- TODO: short
-- short: for ParOr/Loop/SetBlock if any sub.pos is equal to me.pre,
-- then we have a "short circuit"
for k in pairs(sub.ana.pos) do
if k ~= false then
me.ana.pos[false] = nil -- remove NEVER
me.ana.pos[k] = true
end
end
end
function COPY (n)
local ret = {}
for k in pairs(n) do
ret[k] = true
end
return ret
end
function _ANA.CMP (n1, n2)
return _ANA.HAS(n1, n2) and _ANA.HAS(n2, n1)
end
function _ANA.HAS (n1, n2)
for k2 in pairs(n2) do
if not n1[k2] then
return false
end
end
return true
end
local LST = {
Do=true, Stmts=true, Block=true, Root=true, Dcl_cls=true,
Pause=true,
}
F = {
Root_pos = function (me)
_ANA.ana.isForever = not (not me.ana.pos[false])
end,
Node_pre = function (me)
if me.ana then
return
end
local top = _AST.iter()()
me.ana = {
pre = (top and top.ana.pre) or { [true]=true },
}
end,
Node = function (me)
if me.ana.pos then
return
end
if LST[me.tag] and me[#me] then
me.ana.pos = COPY(me[#me].ana.pos) -- copy lst child pos
else
me.ana.pos = COPY(me.ana.pre) -- or copy own pre
end
end,
Dcl_cls_pre = function (me)
if me ~= _MAIN then
me.ana.pre = { [me.id]=true }
end
end,
Orgs = function (me)
me.ana.pos = { [false]=true } -- orgs run forever
end,
Stmts_bef = function (me, sub, i)
if i == 1 then
-- first sub copies parent
sub.ana = {
pre = COPY(me.ana.pre)
}
else
-- broken sequences
if me[i-1].ana.pos[false] and (not me[i-1].ana.pre[false]) then
--_ANA.ana.unreachs = _ANA.ana.unreachs + 1
me.__unreach = true
WRN( INC(me, 'unreachs'),
sub, 'statement is not reachable')
end
-- other subs follow previous
sub.ana = {
pre = COPY(me[i-1].ana.pos)
}
end
end,
ParOr_pos = function (me)
me.ana.pos = { [false]=true }
for _, sub in ipairs(me) do
OR(me, sub, true)
end
if me.ana.pos[false] then
--_ANA.ana.unreachs = _ANA.ana.unreachs + 1
WRN( INC(me, 'unreachs'),
me, 'at least one trail should terminate')
end
end,
ParAnd_pos = function (me)
-- if any of the sides run forever, then me does too
-- otherwise, behave like ParOr
for _, sub in ipairs(me) do
if sub.ana.pos[false] then
me.ana.pos = { [false]=true }
--_ANA.ana.unreachs = _ANA.ana.unreachs + 1
WRN( INC(me, 'unreachs'),
sub, 'trail should terminate')
return
end
end
-- like ParOr, but remove [true]
local onlyTrue = true
me.ana.pos = { [false]=true }
for _, sub in ipairs(me) do
OR(me, sub)
if not sub.ana.pos[true] then
onlyTrue = false
end
end
if not onlyTrue then
me.ana.pos[true] = nil
end
end,
ParEver_pos = function (me)
me.ana.pos = { [false]=true }
local ok = false
for _, sub in ipairs(me) do
if sub.ana.pos[false] then
ok = true
break
end
end
if not ok then
--_ANA.ana.reachs = _ANA.ana.reachs + 1
WRN( INC(me, 'reachs'),
me, 'all trails terminate')
end
end,
If = function (me)
me.ana.pos = { [false]=true }
for _, sub in ipairs{me[2],me[3]} do
OR(me, sub)
end
end,
SetBlock_pre = function (me)
me.ana.pos = { [false]=true } -- `return/break´ may change this
end,
Escape = function (me)
local top = _AST.iter((me.tag=='Escape' and 'SetBlock') or 'Loop')()
me.ana.pos = COPY(me.ana.pre)
OR(top, me, true)
me.ana.pos = { [false]='esc' } -- diff from [false]=true
end,
SetBlock = function (me)
local blk = me[1]
if not blk.ana.pos[false] then
--_ANA.ana.reachs = _ANA.ana.reachs + 1
WRN( INC(me, 'reachs'),
blk, 'missing `escape´ statement for the block')
end
end,
Loop_pre = 'SetBlock_pre',
Break = 'Escape',
Loop = function (me)
if me.isBounded then
me.ana.pos = COPY(me[1].ana.pos)
return -- guaranteed to terminate
end
if me[1].ana.pos[false] then
--_ANA.ana.unreachs = _ANA.ana.unreachs + 1
WRN( INC(me, 'unreachs'),
me, '`loop´ iteration is not reachable')
end
end,
Thread = 'Async',
Async = function (me)
if me.ana.pre[false] then
me.ana.pos = COPY(me.ana.pre)
else
me.ana.pos = { ['ASYNC_'..me.n]=true } -- assume it terminates
end
end,
--[[
-- TODO: remove
-- not needed after SetAwait => AwaitX;SetExp
SetAwait = function (me)
local _, awt, set = unpack(me)
set.ana.pre = COPY(awt.ana.pos)
set.ana.pos = COPY(awt.ana.pos)
me.ana.pre = COPY(awt.ana.pre)
me.ana.pos = COPY(set.ana.pos)
end,
]]
AwaitS = function (me)
DBG'TODO - ana.lua - AwaitS'
end,
AwaitExt_aft = function (me, sub, i)
if i > 1 then
return
end
-- between Await and Until
local awt, cnd = unpack(me)
local t
if me.ana.pre[false] then
t = { [false]=true }
else
-- enclose with a table to differentiate each instance
if me.tag == 'AwaitExt' then
t = { [{awt.evt}]=true }
elseif me.tag == 'AwaitInt' then
-- use "var" as identifier (why "evt" doesn't work?)
t = { [{awt.var}]=true }
else -- 'AwaitT'
t = { [{awt.evt or 'WCLOCK'}]=true }
end
end
me.ana.pos = COPY(t)
if cnd then
cnd.ana = {
pre = COPY(t),
}
end
end,
AwaitInt_aft = 'AwaitExt_aft',
AwaitT_aft = 'AwaitExt_aft',
AwaitN = function (me)
me.ana.pos = { [false]=true }
end,
}
local _union = function (a, b, keep)
if not keep then
local old = a
a = {}
for k in pairs(old) do
a[k] = true
end
end
for k in pairs(b) do
a[k] = true
end
return a
end
-- TODO: remove
-- if nested node is reachable from "pre", join with loop POS
function _ANA.union (root, pre, POS)
local t = {
Node = function (me)
if me.ana.pre[pre] then -- if matches loop begin
_union(me.ana.pre, POS, true)
end
end,
}
_AST.visit(t, root)
end
_AST.visit(F)