-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpreamble.lua
More file actions
214 lines (207 loc) · 4.94 KB
/
preamble.lua
File metadata and controls
214 lines (207 loc) · 4.94 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
local DEFAULT_NUMBER = 1
local always_number = function(val)
return tonumber(val) or DEFAULT_NUMBER
end
local not_nan_and_nil = function(val)
return (val ~= val or val == nil) and DEFAULT_NUMBER or val
end
_G.always_number = always_number
local __add = function(v1, v2)
return always_number(v1) + always_number(v2)
end
local __call = function(self)
return self
end
local __concat = function(v1, v2)
return tostring(v1) .. tostring(v2)
end
local __div = function(v1, v2)
return always_number(v1) / always_number(v2)
end
local __index = function(self, key)
if type(self) == 'table' then
return rawget(self, key)
end
return always_number(key)
end
local __le = function(v1, v2)
if type(v1) == 'number' and type(v2) == 'number' then
return v1 <= v2 -- Numeric comparison.
elseif type(v1) == 'string' and type(v2) == 'string' then
return v1 <= v2 -- Lexicographic comparison.
else
return always_number(v1) <= always_number(v2)
end
end
local __len = function(_v)
return DEFAULT_NUMBER
end
local __lt = function(v1, v2)
if type(v1) == 'number' and type(v2) == 'number' then
return v1 < v2 -- Numeric comparison.
elseif type(v1) == 'string' and type(v2) == 'string' then
return v1 < v2 -- Lexicographic comparison.
else
return always_number(v1) < always_number(v2)
end
end
local __mod = function(v1, v2)
return always_number(v1) % always_number(v2)
end
local __mul = function(v1, v2)
return always_number(v1) * always_number(v2)
end
local __newindex = function(self, key, value)
if type(self) == 'table' then
if key ~= key or key == nil then
key = tostring(key)
end
rawset(self, key, value)
end
end
local __pow = function(v1, v2)
return always_number(v1) ^ always_number(v2)
end
local __sub = function(v1, v2)
return always_number(v1) - always_number(v2)
end
local __unm = function(v)
return - always_number(v)
end
local __idiv = load([[
local v1, v2 = ...
return always_number(v1) // always_number(v2)
]])
local __band = load([[
local v1, v2 = ...
return always_number(v1) & always_number(v2)
]])
local __bor = load([[
local v1, v2 = ...
return always_number(v1) | always_number(v2)
]])
local __bxor = load([[
local v1, v2 = ...
return always_number(v1) ~ always_number(v2)
]])
local __bnot = load([[
local v = ...
return ~always_number(v)
]])
local __shl = load([[
local v1, v2 = ...
return always_number(v1) << always_number(v2)
]])
local __shr = load([[
local v1, v2 = ...
return always_number(v1) >> always_number(v2)
]])
debug.setmetatable('string', {
__add = __add,
__call = __call,
__div = __div,
__idiv = __idiv,
__index = __index,
__mod = __mod,
__mul = __mul,
__newindex = __newindex,
__pow = __pow,
__sub = __sub,
__unm = __unm,
__band = __band,
__bor = __bor,
__bxor = __bxor,
__bnot = __bnot,
__shl = __shl,
__shr = __shr,
})
debug.setmetatable(0, {
__add = __add,
__call = __call,
__concat = __concat,
__div = __div,
__idiv = __idiv,
__index = __index,
__len = __len,
__newindex = __newindex,
})
debug.setmetatable(nil, {
__add = __add,
__call = __call,
__concat = __concat,
__div = __div,
__idiv = __idiv,
__index = __index,
__le = __le,
__len = __len,
__lt = __lt,
__mod = __mod,
__mul = __mul,
__newindex = __newindex,
__pow = __pow,
__sub = __sub,
__unm = __unm,
})
debug.setmetatable(function() end, {
__add = __add,
__concat = __concat,
__div = __div,
__idiv = __idiv,
__index = __index,
__le = __le,
__len = __len,
__lt = __lt,
__mod = __mod,
__mul = __mul,
__newindex = __newindex,
__pow = __pow,
__sub = __sub,
__unm = __unm,
})
debug.setmetatable(true, {
__add = __add,
__call = __call,
__concat = __concat,
__div = __div,
__idiv = __idiv,
__index = __index,
__le = __le,
__len = __len,
__lt = __lt,
__mod = __mod,
__mul = __mul,
__newindex = __newindex,
__pow = __pow,
__sub = __sub,
__unm = __unm,
})
local table_mt = {
__add = __add,
__call = __call,
__concat = __concat,
__div = __div,
__idiv = __idiv,
__le = __le,
__len = __len,
__lt = __lt,
__mod = __mod,
__mul = __mul,
__newindex = __newindex,
__pow = __pow,
__sub = __sub,
__unm = __unm,
}
local only_numbers_cmp = function(v1, v2, cmp_op_str)
local op_func = {
['<'] = function(a1, a2) return a1 < a2 end,
['<='] = function(a1, a2) return a1 <= a2 end,
['>'] = function(a1, a2) return a1 > a2 end,
['>='] = function(a1, a2) return a1 >= a2 end,
}
if type(v1) == 'number' and
type(v2) == 'number' then
return op_func[cmp_op_str](v1, v2)
end
return false
end
---------------------- END OF PREAMBLE ----------------------------