-
Notifications
You must be signed in to change notification settings - Fork 0
/
boolexpr.lua
408 lines (372 loc) · 13.9 KB
/
boolexpr.lua
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
---@class BoolExpr
BoolExpr = setmetatable({}, {})
do
local boolexpr = getmetatable(BoolExpr) ---@type BoolExpr
boolexpr.__index = boolexpr
local DEF = 1 ---@type integer
local NOT = 2 ---@type integer
local AND = 3 ---@type integer
local OR = 4 ---@type integer
local XNOR = 5 ---@type integer
local XOR = 6 ---@type integer
local NAND = 7 ---@type integer
local NOR = 8 ---@type integer
local ALL = 9 ---@type integer
local ANY = 10 ---@type integer
BoolExpr.DEF = DEF
BoolExpr.NOT = NOT
BoolExpr.AND = AND
BoolExpr.OR = OR
BoolExpr.XNOR = XNOR
BoolExpr.XOR = XOR
BoolExpr.NAND = NAND
BoolExpr.NOR = NOR
BoolExpr.ALL = ALL
BoolExpr.ANY = ANY
local op_neg = {
[DEF] = NOT , [NOT] = DEF,
[AND] = NAND , [OR] = NOR,
[XNOR] = XOR , [XOR] = XNOR,
[NAND] = AND , [NOR] = OR
}
local op_str = {
[DEF] = 'DEF' , [NOT] = 'NOT',
[AND] = 'AND' , [OR] = 'OR',
[XNOR] = 'XNOR', [XOR] = 'XOR',
[NAND] = 'NAND', [NOR] = 'NOR',
[ALL] = 'ALL' , [ANY] = 'ANY'
}
local op_up = {
[AND] = ALL , [OR] = ANY,
[ALL] = AND , [ANY] = OR,
}
local function assert_expression(...)
local t = table.pack(...)
local n = t.n
for i = 1, n do
assert(t[i] ~= nil, "[ERROR]: Passed expression is nil!")
end
end
---@param expr function|table|BoolExpr
---@return BoolExpr
function boolexpr.New(expr)
assert_expression(expr)
return setmetatable({DEF, expr}, boolexpr)
end
---@param expr function|table|BoolExpr
---@return BoolExpr
function boolexpr.Not(expr)
assert_expression(expr)
return setmetatable({NOT, expr}, boolexpr)
end
---@param left_expr function|table|BoolExpr
---@param right_expr function|table|BoolExpr
---@return BoolExpr
function boolexpr.And(left_expr, right_expr)
assert_expression(left_expr, right_expr)
return setmetatable({AND, left_expr, right_expr}, boolexpr)
end
---@param left_expr function|table|BoolExpr
---@param right_expr function|table|BoolExpr
---@return BoolExpr
function boolexpr.Or(left_expr, right_expr)
assert_expression(left_expr, right_expr)
return setmetatable({OR, left_expr, right_expr}, boolexpr)
end
---@param left_expr function|table|BoolExpr
---@param right_expr function|table|BoolExpr
---@return BoolExpr
function boolexpr.Xnor(left_expr, right_expr)
assert_expression(left_expr, right_expr)
return setmetatable({XNOR, left_expr, right_expr}, boolexpr)
end
---@param left_expr function|table|BoolExpr
---@param right_expr function|table|BoolExpr
---@return BoolExpr
function boolexpr.Xor(left_expr, right_expr)
assert_expression(left_expr, right_expr)
return setmetatable({XOR, left_expr, right_expr}, boolexpr)
end
---@param left_expr function|table|BoolExpr
---@param right_expr function|table|BoolExpr
---@return BoolExpr
function boolexpr.Nand(left_expr, right_expr)
assert_expression(left_expr, right_expr)
return setmetatable({NAND, left_expr, right_expr}, boolexpr)
end
---@param left_expr function|table|BoolExpr
---@param right_expr function|table|BoolExpr
---@return BoolExpr
function boolexpr.Nor(left_expr, right_expr)
assert_expression(left_expr, right_expr)
return setmetatable({NOR, left_expr, right_expr}, boolexpr)
end
---@return BoolExpr
function boolexpr.All(...)
assert_expression(...)
return setmetatable({ALL, ...}, boolexpr)
end
---@return BoolExpr
function boolexpr.Any(...)
assert_expression(...)
return setmetatable({ANY, ...}, boolexpr)
end
function boolexpr:__gc()
for i = 1, #self do self[i] = nil end
end
local first, last, step
local operator = {
-- Operator: DEF (Default)
function (expr_t, reverse, ...)
return boolexpr.__call(expr_t[2], reverse, ...)
end,
-- Operator: NOT (Not)
function (expr_t, reverse, ...)
return not boolexpr.__call(expr_t[2], reverse, ...)
end,
-- Operator: AND (And)
function (expr_t, reverse, ...)
return
boolexpr.__call(expr_t[reverse and 3 or 2], reverse, ...) and
boolexpr.__call(expr_t[reverse and 2 or 3], reverse, ...)
end,
-- Operator: OR (Or)
function (expr_t, reverse, ...)
return
boolexpr.__call(expr_t[reverse and 3 or 2], reverse, ...) or
boolexpr.__call(expr_t[reverse and 2 or 3], reverse, ...)
end,
-- Operator: XNOR (Exclusive-Nor)
function (expr_t, reverse, ...)
return
boolexpr.__call(expr_t[reverse and 3 or 2], reverse, ...) ==
boolexpr.__call(expr_t[reverse and 2 or 3], reverse, ...)
end,
-- Operator: XOR (Exclusive-Or)
function (expr_t, reverse, ...)
return
boolexpr.__call(expr_t[reverse and 3 or 2], reverse, ...) ~=
boolexpr.__call(expr_t[reverse and 2 or 3], reverse, ...)
end,
-- Operator: NAND (Not-And)
function (expr_t, reverse, ...)
return not (
boolexpr.__call(expr_t[reverse and 3 or 2], reverse, ...) and
boolexpr.__call(expr_t[reverse and 2 or 3], reverse, ...)
)
end,
-- Operator: NOR (Not-Or)
function (expr_t, reverse, ...)
return not (
boolexpr.__call(expr_t[reverse and 3 or 2], reverse, ...) or
boolexpr.__call(expr_t[reverse and 2 or 3], reverse, ...)
)
end,
-- Operator: ALL (All)
function (expr_t, reverse, ...)
if reverse then
first, last, step = #expr_t, 2, -1
else
first, last, step = 2, #expr_t, 1
end
for i = first, last, step do
if not boolexpr.__call(expr_t[i], reverse, ...) then return false end
end
return true
end,
-- Operator: ANY (Any)
function (expr_t, reverse, ...)
if reverse then
first, last, step = #expr_t, 2, -1
else
first, last, step = 2, #expr_t, 1
end
for i = first, last, step do
if boolexpr.__call(expr_t[i], reverse, ...) then return true end
end
return false
end
}
---@param self BoolExpr
---@param reverse boolean
---@return boolean
function boolexpr.__call(self, reverse, ...)
local t = type(self)
if t == 'table' then
local mt = getmetatable(self)
if mt and mt.__call and mt.__call ~= boolexpr.__call then
return self(self, reverse, ...)
end
return operator[self[1]](self, reverse, ...)
elseif t == 'function' then
return self(self, reverse, ...)
end
return self and true or false
end
boolexpr.evaluate = boolexpr.__call
local function callable_status(o)
if type(o) == 'table' then
local mt = getmetatable(o)
if mt and mt.__call then
if mt.__call ~= boolexpr.__call then
return 2 -- table is inherently callable
end
return 3 -- a BoolExpr or extends BoolExpr
end
return 0 -- non-callable table
elseif type(o) == 'function' then
return 1 -- a function
end
return nil -- non-table and non-callable
end
local function is_table_expr(o)
local op = o[1]
return
op == DEF or op == NOT or
op == AND or op == OR or
op == XNOR or op == XOR or
op == NAND or op == NOR or
op == ALL or op == ANY
end
local function is_expr_table(o)
local call_stat = callable_status(o)
return call_stat == 3 or (call_stat == 0 and is_table_expr(o))
end
local function copy_table(o)
if type(o) == 'table' then
local t = {}
for i = 1, #o do t[i] = copy_table(o[i]) end
return t
end
return o
end
local ex_count
---@return string
function boolexpr:__tostring(tab, start)
tab = tab or ""
ex_count = start or 0
if type(self) == 'table' then
local s = tab .. "{\n"
local op = self[1]
s = s .. tab .. "\t" .. op_str[op] .. "\n"
for i = 2, #self do
s = s .. boolexpr.__tostring(self[i], tab .. "\t", ex_count)
end
return s .. tab .. "}\n"
end
ex_count = ex_count + 1
return tab .. tostring(ex_count) .. "\n"
end
function boolexpr:print()
print(tostring(self))
end
---@return BoolExpr
function boolexpr:compile()
if #self > 0 then
local copy = copy_table(self) -- deep copy array table
local compile = boolexpr.compile
local op = copy[1]
if op == DEF then
local expr = copy[2]
if is_expr_table(expr) then
expr = compile(expr)
-- if an expression table, collapse one level
for i = 1, #expr do copy[i] = expr[i] end
end
elseif op == NOT then
-- Simplify expression by one level if possible
local expr = copy[2]
if is_expr_table(expr) then
-- is an expression table
expr = compile(expr)
local eop = expr[1]
if eop == DEF then
copy[2] = expr[2]
elseif eop == NOT then
copy[1] = DEF
copy[2] = expr[2]
elseif eop ~= ALL and eop ~= ANY then
-- invert operator and collapse one level
copy[1] = op_neg[eop]
copy[2] = expr[2]
copy[3] = expr[3]
end
end
elseif op == AND or op == OR then
-- Convert AND to ALL and OR to ANY when possible
local le, re = copy[2], copy[3]
local let, ret = is_expr_table(le), is_expr_table(re)
local uop = op_up[op]
if let or ret then
local lop, rop, upl, upr
if let then
le = compile(le)
lop = le[1]
upl = lop == op or lop == uop
end
if ret then
re = compile(re)
rop = re[1]
upr = rop == op or rop == uop
end
if upl or upr then
copy[1] = uop
local len
if upl then
for i = 2, #le do copy[i] = le[i] end
len = #le
else
copy[2] = le
len = 2
end
if upr then
for i = 2, #re do copy[len + i - 1] = re[i] end
else
copy[len + 1] = re
end
elseif lop == DEF or rop == DEF then
-- collapse one level
if lop == DEF then copy[2] = le[2] end
if rop == DEF then copy[3] = re[2] end
else
-- reload compiled children
copy[2] = le
copy[3] = re
end
end
elseif op == ALL or op == ANY then
-- Combine ALL and AND to ALL or ANY and OR to ANY
local ref = {}
local uop = op_up[op]
for i = 1, #copy do ref[i] = copy[i] end
local j = 2
for i = 2, #ref do
local expr = ref[i]
if is_expr_table(expr) then
-- is expression table
expr = compile(expr)
local eop = expr[1]
if eop == op or eop == uop or eop == DEF then
-- collapsable/combinable
for k = 2, #expr do
copy[j] = expr[k]
j = j + 1
end
else
-- not collapsable, add expression as-is
copy[j] = expr
j = j + 1
end
else
-- not an expression table, add expression as-is
copy[j] = expr
j = j + 1
end
end
end
-- return compiled expression as a new BoolExpr
return setmetatable(copy, boolexpr)
end
return nil
end
end