Skip to content

Commit db02773

Browse files
committed
Add some table functions
- Add table.pack - Add table.insert - Add table.remove - Add table.sort - Change ipairs to make it return 3 values, iter, i and v. - Add Lua and pico8 tests for table.pack, table.insert, table.remove, table.sort and return values for ipairs. - Add comments to let the user know about getmetatable and rawget dependencies. - generate_pico8.py now exports missing.lua in 1-space indentation. - generate_package.py now exports the "table" table.
1 parent d9425a8 commit db02773

File tree

5 files changed

+296
-9
lines changed

5 files changed

+296
-9
lines changed

generate_package.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
matches = []
88
with open('missing.lua', 'r') as r:
99
for line in r:
10-
match = re.match(r'function (\w+) \(', line)
11-
if match is not None:
10+
function_match = re.match(r'function (\w+) \(', line)
11+
table_match = re.match(r'(table) = \{\}', line)
12+
if function_match is not None:
1213
line = re.sub(r'(function \w+ \()', r'local \1', line)
13-
matches.append(match.group(1))
14+
matches.append(function_match.group(1))
15+
elif table_match is not None:
16+
line = re.sub(r'(table = \{\})', r'local \1', line)
17+
matches.append(table_match.group(1))
1418
w.write(line)
1519
r.close()
1620
w.write("\n")

generate_pico8.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,21 @@
77
lua_content = None
88
missing_content = None
99
with open('missing.lua', 'r') as r:
10-
lua_content = r.read()
10+
lua_content = ""
11+
for line in r:
12+
if re.match(r'^ {12}[\w.]+', line) is not None:
13+
line = re.sub(r'^ {12}([\w.]+)', r' \1', line)
14+
elif re.match(r'^ {10}[\w.]+', line) is not None:
15+
line = re.sub(r'^ {10}([\w.]+)', r' \1', line)
16+
elif re.match(r'^ {8}[\w.]+', line) is not None:
17+
line = re.sub(r'^ {8}([\w.]+)', r' \1', line)
18+
elif re.match(r'^ {6}[\w.]+', line) is not None:
19+
line = re.sub(r'^ {6}([\w.]+)', r' \1', line)
20+
elif re.match(r'^ {4}[\w.]+', line) is not None:
21+
line = re.sub(r'^ {4}([\w.]+)', r' \1', line)
22+
elif re.match(r'^ {2}[\w.]+', line) is not None:
23+
line = re.sub(r'^ {2}([\w.]+)', r' \1', line)
24+
lua_content += line
1125
r.close()
1226
with open('template_missing.p8', 'r') as r:
1327
missing_content = r.read()

missing.lua

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
-- pico8-missing-builtins v0.1.3
1+
-- pico8-missing-builtins v0.2.0
22
-- https://github.com/adamscott/pico8-missing-builtins
33
__setmetatable = setmetatable
44
__metatables = {}
55
function setmetatable (object, mt)
66
__metatables[object] = mt
77
return __setmetatable(object, mt)
88
end
9+
-- getmetatable depends on this setmetatable implementation
910
function getmetatable (object)
1011
return __metatables[object]
1112
end
1213

14+
-- rawget depends on getmetatable
1315
function rawget (tbl, index)
1416
assert(type(tbl) == 'table', "bad argument #1 to 'rawget' "
1517
.."(table expected, got "..type(tbl)..")")
@@ -44,11 +46,78 @@ function unpack (arr, i, j)
4446
return create_arg(l)
4547
end
4648

47-
function ipairs (arr)
48-
local i, n = 0, #arr
49-
return function ()
49+
function ipairs (a)
50+
local function iter(a, i)
5051
i = i + 1
51-
if i <= n then return i, arr[i] end
52+
local v = a[i]
53+
if v then
54+
return i, v
55+
end
56+
end
57+
return iter, a, 0
58+
end
59+
60+
table = {}
61+
table.pack = function (...) return {...} end
62+
table.unpack = unpack
63+
64+
function table.insert (list, pos, value)
65+
assert(type(list) == 'table', "bad argument #1 to 'insert' "
66+
.."(table expected, got "..type(list)..")")
67+
if pos and not value then
68+
value = pos
69+
pos = #list + 1
70+
else
71+
assert(type(pos) == 'number', "bad argument #2 to 'insert' "
72+
.."(number expected, got "..type(pos)..")")
73+
end
74+
if pos <= #list then
75+
for i = #list, pos, -1 do
76+
list[i + 1] = list[i]
77+
end
78+
end
79+
list[pos] = value
80+
end
81+
82+
function table.remove(list, pos)
83+
assert(type(list) == 'table', "bad argument #1 to 'remove' "
84+
.."(table expected, got "..type(list)..")")
85+
if not pos then
86+
pos = #list
87+
else
88+
assert(type(pos) == 'number', "bad argument #2 to 'remove' "
89+
.."(number expected, got "..type(tbl)..")")
90+
end
91+
for i = pos, #list do
92+
list[i] = list[i + 1]
93+
end
94+
end
95+
96+
function table.sort (arr, comp)
97+
if not comp then
98+
comp = function (a, b)
99+
return a < b
100+
end
101+
end
102+
local function partition (a, lo, hi)
103+
pivot = a[hi]
104+
i = lo - 1
105+
for j = lo, hi - 1 do
106+
if comp(a[j], pivot) then
107+
i = i + 1
108+
a[i], a[j] = a[j], a[i]
109+
end
110+
end
111+
a[i + 1], a[hi] = a[hi], a[i + 1]
112+
return i + 1
113+
end
114+
local function quicksort (a, lo, hi)
115+
if lo < hi then
116+
p = partition(a, lo, hi)
117+
quicksort(a, lo, p - 1)
118+
return quicksort(a, p + 1, hi)
119+
end
52120
end
121+
return quicksort(arr, 1, #arr)
53122
end
54123
-- END pico8-missing-builtins v0.1.3

template_missing.p8

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ function _init()
1515
test_rawget()
1616
test_unpack()
1717
test_ipairs()
18+
test_table_pack()
19+
test_table_insert()
20+
test_table_remove()
21+
test_table_sort()
1822
print("every test passed.")
1923
end
2024

@@ -211,6 +215,99 @@ function test_ipairs()
211215
for i,v in ipairs(test_table) do
212216
assert(2^i == v, "fail")
213217
end
218+
printh("----> return an iter function")
219+
test_table = {2,4,8,16,32,64,128,256,512,1024}
220+
iter, i, v = ipairs(test_table)
221+
assert(type(iter) == "function", "fail")
222+
printh("pass")
223+
end
224+
225+
function test_table_pack()
226+
printh("==> 'table.pack'")
227+
printh("----> pack values")
228+
local values = table.pack(1,2,3,4,5)
229+
assert(values[1] == 1, "fail")
230+
assert(values[2] == 2, "fail")
231+
assert(values[3] == 3, "fail")
232+
assert(values[4] == 4, "fail")
233+
assert(values[5] == 5, "fail")
234+
printh("pass")
235+
end
236+
237+
function test_table_insert()
238+
printh("==> 'table.insert'")
239+
printh("----> insert value")
240+
local test_table = {22,-20,67,1009,-500}
241+
table.insert(test_table, 10)
242+
assert(test_table[1] == 22, "fail")
243+
assert(test_table[2] == -20, "fail")
244+
assert(test_table[3] == 67, "fail")
245+
assert(test_table[4] == 1009, "fail")
246+
assert(test_table[5] == -500, "fail")
247+
assert(test_table[6] == 10, "fail")
248+
printh("----> insert value at specified pos")
249+
test_table = {-3,-2,-1,0,2,3}
250+
table.insert(test_table, 5, 1)
251+
assert(test_table[1] == -3, "fail")
252+
assert(test_table[2] == -2, "fail")
253+
assert(test_table[3] == -1, "fail")
254+
assert(test_table[4] == 0, "fail")
255+
assert(test_table[5] == 1, "fail")
256+
assert(test_table[6] == 2, "fail")
257+
assert(test_table[7] == 3, "fail")
258+
printh("pass")
259+
end
260+
261+
function test_table_remove()
262+
printh("==> 'table.remove'")
263+
printh("----> remove value")
264+
local test_table = {-3,-2,-1,0,1,2,3}
265+
table.remove(test_table)
266+
assert(test_table[1] == -3, "fail")
267+
assert(test_table[2] == -2, "fail")
268+
assert(test_table[3] == -1, "fail")
269+
assert(test_table[4] == 0, "fail")
270+
assert(test_table[5] == 1, "fail")
271+
assert(test_table[6] == 2, "fail")
272+
assert(test_table[7] == nil, "fail")
273+
printh("----> remove value at specified pos")
274+
test_table = {-3,-2,-1,0,1,2,3}
275+
table.remove(test_table, 1)
276+
assert(test_table[1] == -2, "fail")
277+
assert(test_table[2] == -1, "fail")
278+
assert(test_table[3] == 0, "fail")
279+
assert(test_table[4] == 1, "fail")
280+
assert(test_table[5] == 2, "fail")
281+
assert(test_table[6] == 3, "fail")
282+
assert(test_table[7] == nil, "fail")
283+
printh("pass")
284+
end
285+
286+
function test_table_sort()
287+
printh("==> 'table.sort'")
288+
printh("----> sort simple table")
289+
local test_table = {33,3,333,87,13,252,-29}
290+
table.sort(test_table)
291+
assert(test_table[1] == -29, "fail")
292+
assert(test_table[2] == 3, "fail")
293+
assert(test_table[3] == 13, "fail")
294+
assert(test_table[4] == 33, "fail")
295+
assert(test_table[5] == 87, "fail")
296+
assert(test_table[6] == 252, "fail")
297+
assert(test_table[7] == 333, "fail")
298+
printh("----> sort simple table with given comp function")
299+
local function desc(a,b)
300+
return a > b
301+
end
302+
test_table = {33,3,333,87,13,252,-29}
303+
table.sort(test_table, desc)
304+
assert(test_table[1] == 333, "fail")
305+
assert(test_table[2] == 252, "fail")
306+
assert(test_table[3] == 87, "fail")
307+
assert(test_table[4] == 33, "fail")
308+
assert(test_table[5] == 13, "fail")
309+
assert(test_table[6] == 3, "fail")
310+
assert(test_table[7] == -29, "fail")
214311
printh("pass")
215312
end
216313

test.lua

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,107 @@ describe("missing -ipairs-", function()
221221
assert.are.equals(2^i, v)
222222
end
223223
end)
224+
225+
it("should return a iter function", function()
226+
local test_table = {2,4,8,16,32,64,128,256,512,1024}
227+
iter, i, v = missing.ipairs(test_table)
228+
assert.are.equals(type(iter), 'function')
229+
end)
230+
end)
231+
232+
describe("missing -table.pack-", function()
233+
it("should pack", function()
234+
local test_table_native = table.pack(-1,0,1,2,3,4,5)
235+
local test_table_missing = missing.table.pack(-1,0,1,2,3,4,5)
236+
for i,v in ipairs(test_table_native) do
237+
assert.are.equals(test_table_native[i], test_table_missing[i])
238+
end
239+
end)
240+
end)
241+
242+
describe("missing -table.insert-", function()
243+
it("should insert a value in a simple table", function()
244+
local function generate_table()
245+
return {22,-20,67,1009,-500}
246+
end
247+
local test_table_native = generate_table()
248+
local test_table_missing = generate_table()
249+
table.insert(test_table_native, 10)
250+
missing.table.insert(test_table_missing, 10)
251+
for i,v in ipairs(test_table_native) do
252+
assert.are.equals(test_table_native[i], test_table_missing[i])
253+
end
254+
end)
255+
256+
it("should insert a value at specified pos in a table", function()
257+
local function generate_table()
258+
return {-3,-2,-1,0,2,3}
259+
end
260+
local test_table_native = generate_table()
261+
local test_table_missing = generate_table()
262+
table.insert(test_table_native, 5, 1)
263+
missing.table.insert(test_table_missing, 5, 1)
264+
for i,v in ipairs(test_table_native) do
265+
assert.are.equals(test_table_native[i], test_table_missing[i])
266+
end
267+
end)
268+
end)
269+
270+
describe("missing -table.remove-", function()
271+
it("should remove an element from a given table", function()
272+
local function generate_table()
273+
return {-3,-2,-1,0,1,2,3}
274+
end
275+
local test_table_native = generate_table()
276+
local test_table_missing = generate_table()
277+
table.remove(test_table_native)
278+
missing.table.remove(test_table_missing)
279+
for i,v in ipairs(test_table_native) do
280+
assert.are.equals(test_table_native[i], test_table_missing[i])
281+
end
282+
end)
283+
284+
it("should remove an specified element from a given table", function()
285+
local function generate_table()
286+
return {-3,-2,-1,0,1,2,3}
287+
end
288+
local test_table_native = generate_table()
289+
local test_table_missing = generate_table()
290+
table.remove(test_table_native, 1)
291+
missing.table.remove(test_table_missing, 1)
292+
for i,v in ipairs(test_table_native) do
293+
assert.are.equals(test_table_native[i], test_table_missing[i])
294+
end
295+
end)
296+
end)
297+
298+
describe("missing -table.sort-", function()
299+
it("should sort a simple table", function()
300+
local function generate_table()
301+
return {33,3,333,87,13,252,-29}
302+
end
303+
local test_table_native = generate_table()
304+
local test_table_missing = generate_table()
305+
table.sort(test_table_native)
306+
missing.table.sort(test_table_missing)
307+
for i,v in ipairs(test_table_native) do
308+
assert.are.equals(test_table_native[i], test_table_missing[i])
309+
end
310+
end)
311+
312+
it("should sort a simple table with a given comp function", function()
313+
local function generate_table()
314+
return {33,3,333,87,13,252,-29}
315+
end
316+
local function desc(a, b)
317+
return a > b
318+
end
319+
local test_table_native = generate_table()
320+
local test_table_missing = generate_table()
321+
table.sort(test_table_native, desc)
322+
missing.table.sort(test_table_missing, desc)
323+
for i,v in ipairs(test_table_native) do
324+
assert.are.equals(test_table_native[i], test_table_missing[i])
325+
end
326+
end)
224327
end)

0 commit comments

Comments
 (0)