code / nomsu

Lines6.6K Lua5.1K PEG1.3K make117
2 others 83
Markdown60 Bourne Again Shell23
(616 lines)
1 local List, Dict, Undict, _undict_mt, _dict_mt
2 local insert, remove, concat
3 do
4 local _obj_0 = table
5 insert, remove, concat = _obj_0.insert, _obj_0.remove, _obj_0.concat
6 end
7 local as_nomsu
8 as_nomsu = function(self)
9 if type(self) == 'number' then
10 return tostring(self)
11 end
12 do
13 local mt = getmetatable(self)
14 if mt then
15 do
16 local _as_nomsu = mt.as_nomsu
17 if _as_nomsu then
18 return _as_nomsu(self)
19 end
20 end
21 end
22 end
23 return tostring(self)
24 end
25 local as_lua
26 as_lua = function(self)
27 if type(self) == 'number' then
28 return tostring(self)
29 end
30 do
31 local mt = getmetatable(self)
32 if mt then
33 do
34 local _as_lua = mt.as_lua
35 if _as_lua then
36 return _as_lua(self)
37 end
38 end
39 end
40 end
41 return tostring(self)
42 end
43 local nth_to_last
44 nth_to_last = function(self, n)
45 return self[#self - n + 1]
46 end
47 local _list_mt = {
48 __type = "a List",
49 __eq = function(self, other)
50 if not (type(other) == 'table' and getmetatable(other) == getmetatable(self) and #other == #self) then
51 return false
52 end
53 for i, x in ipairs(self) do
54 if not (x == other[i]) then
55 return false
56 end
57 end
58 return true
59 end,
60 __tostring = function(self)
61 return "[" .. concat((function()
62 local _accum_0 = { }
63 local _len_0 = 1
64 for _index_0 = 1, #self do
65 local b = self[_index_0]
66 _accum_0[_len_0] = as_nomsu(b)
67 _len_0 = _len_0 + 1
68 end
69 return _accum_0
70 end)(), ", ") .. "]"
71 end,
72 as_nomsu = function(self)
73 return "[" .. concat((function()
74 local _accum_0 = { }
75 local _len_0 = 1
76 for _index_0 = 1, #self do
77 local b = self[_index_0]
78 _accum_0[_len_0] = as_nomsu(b)
79 _len_0 = _len_0 + 1
80 end
81 return _accum_0
82 end)(), ", ") .. "]"
83 end,
84 as_lua = function(self)
85 return "a_List{" .. concat((function()
86 local _accum_0 = { }
87 local _len_0 = 1
88 for _index_0 = 1, #self do
89 local b = self[_index_0]
90 _accum_0[_len_0] = as_lua(b)
91 _len_0 = _len_0 + 1
92 end
93 return _accum_0
94 end)(), ", ") .. "}"
95 end,
96 __lt = function(self, other)
97 assert(type(self) == 'table' and type(other) == 'table', "Incompatible types for comparison")
98 for i = 1, math.max(#self, #other) do
99 if not self[i] and other[i] then
100 return true
101 elseif self[i] and not other[i] then
102 return false
103 elseif self[i] < other[i] then
104 return true
105 elseif self[i] > other[i] then
106 return false
107 end
108 end
109 return false
110 end,
111 __le = function(self, other)
112 assert(type(self) == 'table' and type(other) == 'table', "Incompatible types for comparison")
113 for i = 1, math.max(#self, #other) do
114 if not self[i] and other[i] then
115 return true
116 elseif self[i] and not other[i] then
117 return false
118 elseif self[i] < other[i] then
119 return true
120 elseif self[i] > other[i] then
121 return false
122 end
123 end
124 return true
125 end,
126 __add = function(self, other)
127 local ret = List((function()
128 local _accum_0 = { }
129 local _len_0 = 1
130 for _index_0 = 1, #self do
131 local x = self[_index_0]
132 _accum_0[_len_0] = x
133 _len_0 = _len_0 + 1
134 end
135 return _accum_0
136 end)())
137 for _index_0 = 1, #other do
138 local x = other[_index_0]
139 insert(ret, x)
140 end
141 return ret
142 end,
143 __index = {
144 add = insert,
145 append = insert,
146 add_1_at_index = function(t, x, i)
147 return insert(t, i, x)
148 end,
149 at_index_1_add = insert,
150 pop = remove,
151 remove_last = remove,
152 remove_index = remove,
153 last = (function(self)
154 return self[#self]
155 end),
156 first = (function(self)
157 return self[1]
158 end),
159 _1_st_to_last = nth_to_last,
160 _1_nd_to_last = nth_to_last,
161 _1_rd_to_last = nth_to_last,
162 _1_th_to_last = nth_to_last,
163 joined = function(self)
164 return table.concat((function()
165 local _accum_0 = { }
166 local _len_0 = 1
167 for _index_0 = 1, #self do
168 local x = self[_index_0]
169 _accum_0[_len_0] = tostring(x)
170 _len_0 = _len_0 + 1
171 end
172 return _accum_0
173 end)())
174 end,
175 joined_with = function(self, glue)
176 return table.concat((function()
177 local _accum_0 = { }
178 local _len_0 = 1
179 for _index_0 = 1, #self do
180 local x = self[_index_0]
181 _accum_0[_len_0] = tostring(x)
182 _len_0 = _len_0 + 1
183 end
184 return _accum_0
185 end)(), glue)
186 end,
187 has = function(self, item)
188 for _index_0 = 1, #self do
189 local x = self[_index_0]
190 if x == item then
191 return true
192 end
193 end
194 return false
195 end,
196 remove = function(self, item)
197 for i, x in ipairs(self) do
198 if x == item then
199 remove(self, i)
200 end
201 end
202 end,
203 index_of = function(self, item)
204 for i, x in ipairs(self) do
205 if x == item then
206 return i
207 end
208 end
209 return nil
210 end,
211 from_1_to = function(self, start, stop)
212 local n = #self
213 if start < 0 then
214 start = (n + 1 - start)
215 end
216 if stop < 0 then
217 stop = (n + 1 - stop)
218 end
219 return List((function()
220 local _accum_0 = { }
221 local _len_0 = 1
222 for i = start, stop do
223 _accum_0[_len_0] = self[i]
224 _len_0 = _len_0 + 1
225 end
226 return _accum_0
227 end)())
228 end,
229 from = function(self, start)
230 return self:from_1_to(start, -1)
231 end,
232 up_to = function(self, stop)
233 return self:from_1_to(1, stop)
234 end,
235 copy = function(self)
236 return List((function()
237 local _accum_0 = { }
238 local _len_0 = 1
239 for i = 1, #self do
240 _accum_0[_len_0] = self[i]
241 _len_0 = _len_0 + 1
242 end
243 return _accum_0
244 end)())
245 end,
246 reverse = function(self)
247 local n = #self
248 for i = 1, math.floor(n / 2) do
249 self[i], self[n - i + 1] = self[n - i + 1], self[i]
250 end
251 end,
252 reversed = function(self)
253 return List((function()
254 local _accum_0 = { }
255 local _len_0 = 1
256 for i = #self, 1, -1 do
257 _accum_0[_len_0] = self[i]
258 _len_0 = _len_0 + 1
259 end
260 return _accum_0
261 end)())
262 end,
263 sort = function(self)
264 return table.sort(self)
265 end,
266 sort_by = function(self, fn)
267 local keys = setmetatable({ }, {
268 __index = function(self, k)
269 local key = fn(k)
270 self[k] = key
271 return key
272 end
274 return table.sort(self, function(a, b)
275 return keys[a] <= keys[b]
276 end)
277 end,
278 sorted = function(self)
279 local c = self:copy()
280 c:sort()
281 return c
282 end,
283 sorted_by = function(self, fn)
284 local c = self:copy()
285 c:sort_by(fn)
286 return c
287 end,
288 filter_by = function(self, keep)
289 local deleted = 0
290 for i = 1, #self do
291 if not (keep(self[i])) then
292 deleted = deleted + 1
293 elseif deleted > 0 then
294 self[i - deleted] = self[i]
295 end
296 end
297 for i = #self - deleted + 1, #self do
298 self[i] = nil
299 end
300 end,
301 filtered_by = function(self, keep)
302 local c = self:copy()
303 c:filter_by(keep)
304 return c
305 end
307 __newindex = function(self, k, v)
308 assert(type(k) == 'number', "List indices must be numbers")
309 return rawset(self, k, v)
310 end
312 _list_mt.__index.as_lua = _list_mt.as_lua
313 _list_mt.__index.as_nomsu = _list_mt.as_nomsu
314 List = function(t, ...)
315 local l
316 if type(t) == 'table' then
317 l = setmetatable(t, _list_mt)
318 elseif type(t) == 'function' then
319 l = setmetatable({ }, _list_mt)
320 local add
321 add = function(...)
322 for i = 1, select('#', ...) do
323 l[#l + 1] = select(i, ...)
324 end
325 end
326 t(add)
327 elseif type(t) == 'thread' then
328 l = setmetatable({ }, _list_mt)
329 for val in coroutine.wrap(t) do
330 l[#l + 1] = val
331 end
332 else
333 error("Unsupported List type: " .. type(t))
334 end
335 if select(1, ...) then
336 local _list_0 = List(...)
337 for _index_0 = 1, #_list_0 do
338 local x = _list_0[_index_0]
339 l[#l + 1] = x
340 end
341 end
342 return l
343 end
344 local compliments = setmetatable({ }, {
345 __mode = 'k'
347 _undict_mt = {
348 __type = "an Inverse Dict",
349 __index = function(self, k)
350 return not compliments[self][k] and true or nil
351 end,
352 __newindex = function(self, k, v)
353 if k then
354 compliments[self][k] = nil
355 else
356 compliments[self][k] = true
357 end
358 end,
359 __eq = function(self, other)
360 if not (type(other) == 'table' and getmetatable(other) == getmetatable(self)) then
361 return false
362 end
363 return compliments[self] == compliments[other]
364 end,
365 __len = function(self)
366 return math.huge
367 end,
368 __tostring = function(self)
369 return "~" .. _dict_mt.__tostring(compliments[self])
370 end,
371 as_nomsu = function(self)
372 return "~" .. _dict_mt.as_nomsu(compliments[self])
373 end,
374 as_lua = function(self)
375 return "~" .. __dict_mt.as_lua(compliments[self])
376 end,
377 __band = function(self, other)
378 if getmetatable(other) == _undict_mt then
379 return Undict(_dict_mt.__bor(compliments[self], compliments[other]))
380 else
381 return _dict_mt.__band(other, self)
382 end
383 end,
384 __bor = function(self, other)
385 if getmetatable(other) == _undict_mt then
386 return Undict(_dict_mt.__band(compliments[self], compliments[other]))
387 else
388 return Undict((function()
389 local _tbl_0 = { }
390 for k, v in pairs(compliments[self]) do
391 if not other[k] then
392 _tbl_0[k] = v
393 end
394 end
395 return _tbl_0
396 end)())
397 end
398 end,
399 __bxor = function(self, other)
400 if getmetatable(other) == _undict_mt then
401 return _dict_mt.__bxor(compliments[self], compliments[other])
402 else
403 return Undict(_dict_mt.__band(other, self))
404 end
405 end,
406 __bnot = function(self)
407 return Dict((function()
408 local _tbl_0 = { }
409 for k, v in pairs(compliments[self]) do
410 _tbl_0[k] = v
411 end
412 return _tbl_0
413 end)())
414 end
416 Undict = function(d)
417 local u = setmetatable({ }, _undict_mt)
418 compliments[u] = Dict((function()
419 local _tbl_0 = { }
420 for k, v in pairs(d) do
421 if v then
422 _tbl_0[k] = true
423 end
424 end
425 return _tbl_0
426 end)())
427 return u
428 end
429 _dict_mt = {
430 __type = "a Dict",
431 __eq = function(self, other)
432 if not (type(other) == 'table' and getmetatable(other) == getmetatable(self)) then
433 return false
434 end
435 for k, v in pairs(self) do
436 if not (v == other[k]) then
437 return false
438 end
439 end
440 for k, v in pairs(other) do
441 if not (v == self[k]) then
442 return false
443 end
444 end
445 return true
446 end,
447 __len = function(self)
448 local n = 0
449 for _ in pairs(self) do
450 n = n + 1
451 end
452 return n
453 end,
454 __tostring = function(self)
455 return "{" .. concat((function()
456 local _accum_0 = { }
457 local _len_0 = 1
458 for k, v in pairs(self) do
459 _accum_0[_len_0] = v == true and "." .. as_nomsu(k) or "." .. tostring(k) .. " = " .. tostring(v)
460 _len_0 = _len_0 + 1
461 end
462 return _accum_0
463 end)(), ", ") .. "}"
464 end,
465 as_nomsu = function(self)
466 return "{" .. concat((function()
467 local _accum_0 = { }
468 local _len_0 = 1
469 for k, v in pairs(self) do
470 _accum_0[_len_0] = v == true and "." .. as_nomsu(k) or "." .. tostring(as_nomsu(k)) .. " = " .. tostring(as_nomsu(v))
471 _len_0 = _len_0 + 1
472 end
473 return _accum_0
474 end)(), ", ") .. "}"
475 end,
476 as_lua = function(self)
477 return "a_Dict{" .. concat((function()
478 local _accum_0 = { }
479 local _len_0 = 1
480 for k, v in pairs(self) do
481 _accum_0[_len_0] = "[ " .. tostring(as_lua(k)) .. "]= " .. tostring(as_lua(v))
482 _len_0 = _len_0 + 1
483 end
484 return _accum_0
485 end)(), ", ") .. "}"
486 end,
487 __inext = function(self, key)
488 local nextkey, value = next(self, key)
489 if nextkey ~= nil then
490 return nextkey, Dict({
491 key = nextkey,
492 value = value
494 end
495 end,
496 __band = function(self, other)
497 return Dict((function()
498 local _tbl_0 = { }
499 for k, v in pairs(self) do
500 if other[k] then
501 _tbl_0[k] = v
502 end
503 end
504 return _tbl_0
505 end)())
506 end,
507 __bor = function(self, other)
508 if getmetatable(other) == _undict_mt then
509 return _undict_mt.__bor(other, self)
510 end
511 local ret = Dict((function()
512 local _tbl_0 = { }
513 for k, v in pairs(self) do
514 _tbl_0[k] = v
515 end
516 return _tbl_0
517 end)())
518 for k, v in pairs(other) do
519 if ret[k] == nil then
520 ret[k] = v
521 end
522 end
523 return ret
524 end,
525 __bxor = function(self, other)
526 if getmetatable(other) == _undict_mt then
527 return _undict_mt.__bxor(other, self)
528 end
529 local ret = Dict((function()
530 local _tbl_0 = { }
531 for k, v in pairs(self) do
532 _tbl_0[k] = v
533 end
534 return _tbl_0
535 end)())
536 for k, v in pairs(other) do
537 if ret[k] == nil then
538 ret[k] = v
539 else
540 ret[k] = nil
541 end
542 end
543 return ret
544 end,
545 __bnot = Undict,
546 __add = function(self, other)
547 local ret = Dict((function()
548 local _tbl_0 = { }
549 for k, v in pairs(self) do
550 _tbl_0[k] = v
551 end
552 return _tbl_0
553 end)())
554 for k, v in pairs(other) do
555 if ret[k] == nil then
556 ret[k] = v
557 else
558 ret[k] = ret[k] + v
559 end
560 end
561 return ret
562 end,
563 __sub = function(self, other)
564 local ret = Dict((function()
565 local _tbl_0 = { }
566 for k, v in pairs(self) do
567 _tbl_0[k] = v
568 end
569 return _tbl_0
570 end)())
571 for k, v in pairs(other) do
572 if ret[k] == nil then
573 ret[k] = -v
574 else
575 ret[k] = ret[k] - v
576 end
577 end
578 return ret
579 end
581 Dict = function(t, more, ...)
582 local d
583 if type(t) == 'table' then
584 d = setmetatable(t, _dict_mt)
585 elseif type(t) == 'function' then
586 d = setmetatable({ }, _dict_mt)
587 local add
588 add = function(...)
589 for i = 1, select('#', ...) do
590 d[select(i, ...)] = true
591 end
592 end
593 local add_1_eq_2
594 add_1_eq_2 = function(k, v)
595 d[k] = v
596 end
597 t(add, add_1_eq_2)
598 elseif type(t) == 'thread' then
599 d = setmetatable({ }, _dict_mt)
600 for k, v in coroutine.wrap(t) do
601 d[k] = v
602 end
603 else
604 error("Unsupported Dict type: " .. type(t))
605 end
606 if more then
607 for k, v in pairs(Dict(more, ...)) do
608 d[k] = v
609 end
610 end
611 return d
612 end
613 return {
614 List = List,
615 Dict = Dict