-
Notifications
You must be signed in to change notification settings - Fork 3
/
operator.lua
47 lines (40 loc) · 1.23 KB
/
operator.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
-- Priorities for each binary operator.
-- (left priority) * 256 + (right priority)
-- modulus is your friend
local binop = {
['+'] = 6 * 256 + 6, ['-'] = 6 * 256 + 6, ['*'] = 7 * 256 + 7, ['/'] = 7 * 256 + 7, ['%'] = 7 * 256 + 7,
['^'] = 10* 256 + 9, ['..'] = 5 * 256 + 4, -- POW CONCAT (right associative)
['=='] = 3 * 256 + 3, ['~='] = 3 * 256 + 3,
['<'] = 3 * 256 + 3, ['>='] = 3 * 256 + 3, ['>'] = 3 * 256 + 3, ['<='] = 3 * 256 + 3,
['and']= 2 * 256 + 2, ['or'] = 1 * 256 + 1,
['**'] = 10* 256 + 10,
['^^'] = 12* 256 + 11, -- right associative
}
local unaop = {
['#'] = 8,
['-'] = 8,
['not'] = 8,
['`'] = 13, -- highest
}
local function unary_priority(op)
return unaop[op]
end
-- Pseudo priority of a simple identifier. Should be higher than any
-- others operator's priority.
local ident_priority = 16
local function is_binop(op)
return binop[op]
end
local function left_priority(op)
return bit.rshift(binop[op], 8)
end
local function right_priority(op)
return bit.band(binop[op], 0xff)
end
return {
is_binop = is_binop,
left_priority = left_priority,
right_priority = right_priority,
unary_priority = unary_priority,
ident_priority = ident_priority,
}