-
Notifications
You must be signed in to change notification settings - Fork 5
/
PYLUA.lua
353 lines (313 loc) · 7.12 KB
/
PYLUA.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
-- helpers for scripts generated using pylua Python->Lua translator
local PYLUA = {}
function PYLUA.class(parent)
return function(methods)
if parent then
parent = getmetatable(parent)
setmetatable(methods, {__index=parent.__index})
end
local meta = {
__index=methods,
__tostring=methods.__str__,
}
return setmetatable({}, {
__index=methods,
__call = function(self, ...)
local obj = setmetatable({}, meta)
obj:__init__(...)
return obj
end,
})
end
end
function PYLUA.is_a(typ, class)
local methods = getmetatable(class).__index
while type(typ)=='table' do
local meta = getmetatable(typ)
if not meta then
return false
elseif meta.__index==methods then
return true
end
typ = meta.__index
end
end
function PYLUA.dirname(s)
return (string.gsub(s, '[^\\/]+$', ''))
end
PYLUA.IOError = PYLUA.class() {
__init__ = function(self, msg)
self.msg = debug.traceback(msg, 3)
end,
__str__ = function(self)
return self.msg
end,
}
local wrappedFile = PYLUA.class() {
__init__ = function(self, f)
self.f = f
end,
readline = function(self)
return self.f:read '*L' or ''
end,
read = function(self, n)
if n and n>=0 then
return self.f:read(n) or ''
else
return self.f:read('*a') or ''
end
end,
seek = function(self, offset)
-- TODO: also support 'whence' argument
local prev, err = self.f:seek('set', offset)
if not prev then
error(PYLUA.IOError(err))
end
end,
close = function(self)
self.f:close()
end,
}
function PYLUA.open(filename, mode)
local f, err = io.open(filename, mode)
if not f then
error(PYLUA.IOError(err))
end
return wrappedFile(f)
end
function PYLUA.strip(s)
if not s then error(debug.traceback('nil argument to PYLUA.strip')) end
s = string.gsub(s, '%s*$', '')
s = string.gsub(s, '^%s*', '')
return s
end
function PYLUA.startswith(s, prefix)
return #s>=#prefix and string.sub(s,1,#prefix)==prefix
end
function PYLUA.endswith(s, suffix)
return #s>=#suffix and (#suffix==0 or string.sub(s,-#suffix)==suffix)
end
function PYLUA.split(s, sep, maxsplit)
if maxsplit == -1 then
maxsplit = nil
elseif maxsplit then
maxsplit = maxsplit+1
end
if not sep then
-- special case described in Python manual
local ret, n = {}, 0
string.gsub(s, '()([^%s]+)', function(i, match)
n = n+1
if not maxsplit or n<=maxsplit then
ret[#ret+1] = match
elseif n==maxsplit+1 then
ret[#ret+1] = string.sub(s, i)
end
end, maxsplit)
return ret
else
local ret, prev = {}, 1
while not maxsplit or maxsplit>1 do
local first, last = string.find(s, sep, prev, true)
if not first then
break
end
ret[#ret+1] = string.sub(s, prev, first-1)
prev = last+1
if maxsplit then
maxsplit = maxsplit-1
end
end
ret[#ret+1] = string.sub(s, prev)
return ret
end
end
function PYLUA.print(...)
-- TODO: improve this to better match Python behavior
for _, s in ipairs{...} do
io.write(' '..tostring(s))
end
end
-- below is based on http://stackoverflow.com/a/20778724/98528
local special_chars = '%^$().[]*+-?'
local quote_pattern = '([' .. string.gsub(special_chars, '(.)', '%%%1') .. '])'
function PYLUA.quote(s)
return string.gsub(s, quote_pattern, '%%%1')
end
function PYLUA.replace(s, from, to, n)
to = string.gsub(to, '%%', '%%%%')
return string.gsub(s, PYLUA.quote(from), to, n)
end
function PYLUA.op_in(x, list)
if type(list)=='table' then
for _, v in ipairs(list) do
if x==v then
return true
end
end
return false
elseif type(list)=='string' then
return string.find(list, x, 1, true) ~= nil
else
error(debug.traceback('unexpected type in op_in: '..type(list)))
end
end
function PYLUA.op_not_in(x, list)
return not PYLUA.op_in(x, list)
end
function PYLUA.collect(tab, filter)
local out = {}
for _, v in ipairs(tab) do
local result = filter(v)
if result then
out[#out+1] = result
end
end
return out
end
function PYLUA.filter(func, t)
local res = {}
for _, v in ipairs(t) do
if func(v) then
res[#res+1] = v
end
end
return res
end
function PYLUA.map(func, t)
local res = {}
for _, v in ipairs(t) do
res[#res+1] = func(v)
end
return res
end
function PYLUA.update(target, overwrites)
for k,v in pairs(overwrites) do
target[k] = v
end
return target
end
function PYLUA.copy(t)
local copy = {}
for k,v in pairs(t) do
copy[k] = v
end
return copy
end
function PYLUA.setdefault(target, k, default)
if not target[k] then
target[k] = default
end
return target[k]
end
function PYLUA.lower(s) return string.lower(s) end
function PYLUA.traceback(msg)
if type(msg) == 'string' then
return debug.traceback(msg, 2)
else
return msg
end
end
local function string_iter(s, i)
if i<#s then
local c = string.sub(s, i+1, i+1)
return i+1, c
end
end
function PYLUA.ipairs(t)
if type(t)=='string' then
return string_iter, t, 0
else
return ipairs(t)
end
end
function PYLUA.ipairs_unicode(t)
if type(t)=='string' then
local finder = string.gmatch(t, '[%z\1-\127\194-\244][\128-\191]*')
local iter = function(finder, i)
local c = finder()
if c then
return i+1, c
end
end
return iter, finder, 0
elseif type(t)=='nil' then
error(debug.traceback('nil in ipairs_unicode'))
else
return ipairs(t)
end
end
function PYLUA.ord(s)
if not s then error(debug.traceback('nil s in ord')) end
-- TODO: handle unicode correctly
return string.byte(s)
end
local function unichr_6lsb(n)
-- return 6 least significant bits + marker bit 0x80, and remaining most significant bits
return 0x80 + n % 0x3f, math.floor(n/0x40)
end
function PYLUA.unichr(n)
if n<=0x007f then
return n
elseif n<=0x07ff then
local b0, n = unichr_6lsb(n)
local b1 = 0xc0 + n
return string.char(b1, b0)
elseif n<=0xffff then
local b0, n = unichr_6lsb(n)
local b1, n = unichr_6lsb(n)
local b2 = 0xe0 + n
return string.char(b2, b1, b0)
else
error("unichr not yet implemented for n>0xffff: "..tostring(n))
end
end
PYLUA.keywords = PYLUA.class() {
__init__ = function(self, kw)
PYLUA.update(self, kw)
end,
}
function PYLUA.slice(seq, i, j)
i = i or 0
j = j or #seq
if i < 0 then i = i + #seq end
if j < 0 then j = j + #seq end
-- TODO: what if i or j still < 0 after above adjustment?
i = i + 1
if j > #seq then j = #seq end
if type(seq)=='table' then
local ret = {}
for n = i,j do
ret[#ret+1] = seq[n]
end
return ret
elseif type(seq)=='string' then
return string.sub(seq, i, j)
else
error('unsupported type for PYLUA.slice: '..type(seq))
end
end
function PYLUA.sum(t)
local res = 0
for _, v in ipairs(t) do
res = res + v
end
return res
end
function PYLUA.keys(t)
local res = {}
for k in pairs(t) do
res[#res+1] = k
end
return res
end
function PYLUA.max(t)
local max
for _,v in pairs(t) do
if not max or v>max then
max=v
end
end
return max
end
return PYLUA