-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathoperators.pluto
More file actions
71 lines (65 loc) · 1.46 KB
/
operators.pluto
File metadata and controls
71 lines (65 loc) · 1.46 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
-- lparser.cpp's builtinoperators
local Pluto_operator_new <const> = function(mt, ...)
if not mt then
error("attempt to construct a nil value")
end
if mt.new then
return mt.new(...)
end
local t = {}
setmetatable(t, mt)
if not rawget(mt, "__index") then
mt.__index = mt
end
if mt.__construct then
mt.__construct(t, ...)
end
return t
end
local Pluto_operator_extends <const> = function(c, p)
if (p_type := type(p)) != "table" then
error("expected a class or class-like table to extend, got " .. p_type)
end
for {
-- luaT_eventname
"__index", "__mindex", "__newindex",
"__gc", "__mode", "__len", "__eq",
"__add", "__sub", "__mul", "__mod", "__pow",
"__div", "__idiv",
"__band", "__bor", "__bxor", "__shl", "__shr",
"__unm", "__bnot", "__lt", "__le",
"__concat", "__call", "__close",
-- misc
"__tostring", "__pairs"
} as mm do
if p[mm] and not c[mm] then
c[mm] = p[mm]
end
end
setmetatable(c, { __index = p })
c.__parent = p
end
local Pluto_operator_instanceof <const> = function(t, mt)
t = getmetatable(t)
while t do
if t == mt then
return true
end
t = t.__parent
end
return false
end
local Pluto_operator_spaceship <const> = function(a, b)
return a ~= b ? a < b ? -1 : +1 : 0
end
local Pluto_operator_ipow <const> = function(x, p)
local res = 1
while p > 0 do
if p & 1 ~= 0 then
res *= x
end
p >>= 1
x *= x
end
return res
end