-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.lua
389 lines (328 loc) · 10.5 KB
/
init.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
--[[
General Lua Libraries for Lua 5.1, 5.2 & 5.3
Copyright (C) 2002-2018 stdlib authors
]]
--[[--
Enhanced Lua core functions, and others.
After requiring this module, simply referencing symbols in the submodule
hierarchy will load the necessary modules on demand. There are no
changes to any global symbols, or monkey patching of core module tables
and metatables.
@todo Write a style guide(indenting/wrapping, capitalisation,
function and variable names); library functions should call
error, not die; OO vs non-OO(a thorny problem).
@todo pre-compile.
@corefunction std
]]
local _ = require '__zk-lib__/lualib/std/_base'
local argscheck = _.typecheck and _.typecheck.argscheck
local compare = _.list.compare
local maxn = _.table.maxn
local split = _.string.split
_ = nil
local _ENV = require '__zk-lib__/lualib/std/normalize' {
format = 'string.format',
match = 'string.match',
}
--[[ =============== ]]--
--[[ Implementation. ]]--
--[[ =============== ]]--
local M
local function _assert(expect, fmt, arg1, ...)
local msg =(arg1 ~= nil) and format(fmt, arg1, ...) or fmt or ''
return expect or error(msg, 2)
end
local function elems(t)
-- capture pairs iterator initial state
local fn, istate, ctrl = pairs(t)
return function(state, _)
local v
ctrl, v = fn(state, ctrl)
if ctrl then
return v
end
end, istate, true -- wrapped initial state
end
local function eval(s)
return load('return ' .. s)()
end
local function ielems(t)
-- capture pairs iterator initial state
local fn, istate, ctrl = ipairs(t)
return function(state, _)
local v
ctrl, v = fn(state, ctrl)
if ctrl then
return v
end
end, istate, true -- wrapped initial state
end
local function npairs(t)
local m = getmetamethod(t, '__len')
local i, n = 0, m and m(t) or maxn(t)
return function(t)
i = i + 1
if i <= n then
return i, t[i]
end
end,
t, i
end
local function ripairs(t)
local oob = 1
while t[oob] ~= nil do
oob = oob + 1
end
return function(t, n)
n = n - 1
if n > 0 then
return n, t[n]
end
end, t, oob
end
local function rnpairs(t)
local m = getmetamethod(t, '__len')
local oob =(m and m(t) or maxn(t)) + 1
return function(t, n)
n = n - 1
if n > 0 then
return n, t[n]
end
end, t, oob
end
local vconvert = setmetatable({
string = function(x)
return split(x, '%.')
end,
number = function(x)
return {x}
end,
table = function(x)
return x
end,
}, {
__call = function(self, x)
local fn = self[type(x)] or function()
return 0
end
return fn(x)
end,
})
local function vcompare(a, b)
return compare(vconvert(a), vconvert(b))
end
local function _require(module, min, too_big, pattern)
pattern = pattern or '([%.%d]+)%D*$'
local s, m = '', require(module)
if type(m) == 'table' then
s = tostring(m.version or m._VERSION or '')
end
local v = match(s, pattern) or 0
if min then
_assert(vcompare(v, min) >= 0, "require '" .. module ..
"' with at least version " .. min .. ', but found version ' .. v)
end
if too_big then
_assert(vcompare(v, too_big) < 0, "require '" .. module ..
"' with version less than " .. too_big .. ', but found version ' .. v)
end
return m
end
--[[ ================= ]]--
--[[ Public Interface. ]]--
--[[ ================= ]]--
local function X(decl, fn)
return argscheck and argscheck('std.' .. decl, fn) or fn
end
M = {
--- Release version string.
-- @field version
--- Core Functions
-- @section corefuncs
--- Enhance core `assert` to also allow formatted arguments.
-- @function assert
-- @param expect expression, expected to be *truthy*
-- @string[opt=''] f format string
-- @param[opt] ... arguments to format
-- @return value of *expect*, if *truthy*
-- @usage
-- std.assert(expect == nil, '100% unexpected!')
-- std.assert(expect == 'expect', '%s the unexpected!', expect)
assert = X('assert(?any, ?string, [any...])', _assert),
--- Evaluate a string as Lua code.
-- @function eval
-- @string s string of Lua code
-- @return result of evaluating `s`
-- @usage
-- --> 2
-- std.eval 'math.min(2, 10)'
eval = X('eval(string)', eval),
--- Return named metamethod, if any, otherwise `nil`.
-- The value found at the given key in the metatable of *x* must be a
-- function or have its own `__call` metamethod to qualify as a
-- callable. Any other value found at key *n* will cause this function
-- to return `nil`.
-- @function getmetamethod
-- @param x item to act on
-- @string n name of metamethod to lookup
-- @treturn callable|nil callable metamethod, or `nil` if no metamethod
-- @usage
-- clone = std.getmetamethod(std.object.prototype, '__call')
getmetamethod = X('getmetamethod(?any, string)', getmetamethod),
--- Module Functions
-- @section modulefuncs
--- Enhance core `require` to assert version number compatibility.
-- By default match against the last substring of(dot-delimited)
-- digits in the module version string.
-- @function require
-- @string module module to require
-- @string[opt] min lowest acceptable version
-- @string[opt] too_big lowest version that is too big
-- @string[opt] pattern to match version in `module.version` or
-- `module._VERSION`(default: `'([%.%d]+)%D*$'`)
-- @usage
-- -- posix.version == 'posix library for Lua 5.2 / 32'
-- posix = require('posix', '29')
require = X('require(string, ?string, ?string, ?string)', _require),
--- Iterator Functions
-- @section iteratorfuncs
--- An iterator over all values of a table.
-- If *t* has a `__pairs` metamethod, use that to iterate.
-- @function elems
-- @tparam table t a table
-- @treturn function iterator function
-- @treturn table *t*, the table being iterated over
-- @return *key*, the previous iteration key
-- @see ielems
-- @see pairs
-- @usage
-- --> foo
-- --> bar
-- --> baz
-- --> 5
-- std.functional.map(print, std.ielems, {'foo', 'bar', [4]='baz', d=5})
elems = X('elems(table)', elems),
--- An iterator over the integer keyed elements of a table.
--
-- If *t* has a `__len` metamethod, iterate up to the index it
-- returns, otherwise up to the first `nil`.
--
-- This function does **not** support the Lua 5.2 `__ipairs` metamethod.
-- @function ielems
-- @tparam table t a table
-- @treturn function iterator function
-- @treturn table *t*, the table being iterated over
-- @treturn int *index*, the previous iteration index
-- @see elems
-- @see ipairs
-- @usage
-- --> foo
-- --> bar
-- std.functional.map(print, std.ielems, {'foo', 'bar', [4]='baz', d=5})
ielems = X('ielems(table)', ielems),
--- An iterator over integer keyed pairs of a sequence.
--
-- Like Lua 5.1 and 5.3, this iterator returns successive key-value
-- pairs with integer keys starting at 1, up to the first `nil` valued
-- pair.
--
-- If there is a `_len` metamethod, keep iterating up to and including
-- that element, regardless of any intervening `nil` values.
--
-- This function does **not** support the Lua 5.2 `__ipairs` metamethod.
-- @function ipairs
-- @tparam table t a table
-- @treturn function iterator function
-- @treturn table *t*, the table being iterated over
-- @treturn int *index*, the previous iteration index
-- @see ielems
-- @see npairs
-- @see pairs
-- @usage
-- --> 1 foo
-- --> 2 bar
-- std.functional.map(print, std.ipairs, {'foo', 'bar', [4]='baz', d=5})
ipairs = X('ipairs(table)', ipairs),
--- Ordered iterator for integer keyed values.
-- Like ipairs, but does not stop until the __len or maxn of *t*.
-- @function npairs
-- @tparam table t a table
-- @treturn function iterator function
-- @treturn table t
-- @see ipairs
-- @see rnpairs
-- @usage
-- --> 1 foo
-- --> 2 bar
-- --> 3 nil
-- --> 4 baz
-- std.functional.map(print, std.npairs, {'foo', 'bar', [4]='baz', d=5})
npairs = X('npairs(table)', npairs),
--- Enhance core `pairs` to respect `__pairs` even in Lua 5.1.
-- @function pairs
-- @tparam table t a table
-- @treturn function iterator function
-- @treturn table *t*, the table being iterated over
-- @return *key*, the previous iteration key
-- @see elems
-- @see ipairs
-- @usage
-- --> 1 foo
-- --> 2 bar
-- --> 4 baz
-- --> d 5
-- std.functional.map(print, std.pairs, {'foo', 'bar', [4]='baz', d=5})
pairs = X('pairs(table)', pairs),
--- An iterator like ipairs, but in reverse.
-- Apart from the order of the elements returned, this function follows
-- the same rules as @{ipairs} for determining first and last elements.
-- @function ripairs
-- @tparam table t any table
-- @treturn function iterator function
-- @treturn table *t*
-- @treturn number `#t + 1`
-- @see ipairs
-- @see rnpairs
-- @usage
-- --> 2 bar
-- --> 1 foo
-- std.functional.map(print, std.ripairs, {'foo', 'bar', [4]='baz', d=5})
ripairs = X('ripairs(table)', ripairs),
--- An iterator like npairs, but in reverse.
-- Apart from the order of the elements returned, this function follows
-- the same rules as @{npairs} for determining first and last elements.
-- @function rnpairs
-- @tparam table t a table
-- @treturn function iterator function
-- @treturn table t
-- @see npairs
-- @see ripairs
-- @usage
-- --> 4 baz
-- --> 3 nil
-- --> 2 bar
-- --> 1 foo
-- std.functional.map(print, std.rnpairs, {'foo', 'bar', [4]='baz', d=5})
rnpairs = X('rnpairs(table)', rnpairs),
}
--- Metamethods
-- @section Metamethods
return setmetatable(M, {
--- Lazy loading of stdlib modules.
-- Don't load everything on initial startup, wait until first attempt
-- to access a submodule, and then load it on demand.
-- @function __index
-- @string name submodule name
-- @treturn table|nil the submodule that was loaded to satisfy the missing
-- `name`, otherwise `nil` if nothing was found
-- @usage
-- local std = require 'std'
-- local Object = std.object.prototype
__index = function(self, name)
local ok, t = pcall(require, 'std.' .. name)
if ok then
rawset(self, name, t)
return t
end
end,
})