-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathast-boolean-const-eval.lua
More file actions
45 lines (38 loc) · 1.03 KB
/
ast-boolean-const-eval.lua
File metadata and controls
45 lines (38 loc) · 1.03 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
local BoolConstRule = { }
-- A function that return a numeric constant if an AST node evaluate to an
-- arithmetic constant or "nil" otherwise.
-- The implementation of the function is given below.
local const_eval
local function dirop_compute(o, a, b)
if o == 'and' then return a and b
elseif o == 'or' then return a or b
end
end
function BoolConstRule.Literal(node)
local v = node.value
if type(v) == 'boolean' then return v end
end
function BoolConstRule.BinaryExpression(node)
local o = node.operator
local a = const_eval(node.left)
if a ~= nil then
local b = const_eval(node.right)
if b ~= nil then
return dirop_compute(o, a, b)
end
end
end
function BoolConstRule.UnaryExpression(node)
local o = node.operator
if o == 'not' then
local v = const_eval(node.argument)
if v ~= nil then return not v end
end
end
function const_eval(node)
local rule = BoolConstRule[node.kind]
if rule then
return rule(node)
end
end
return const_eval