code / nomsu

Lines6.6K Lua5.1K PEG1.3K make117
2 others 83
Markdown60 Bourne Again Shell23
(105 lines)
1 -- This file defines some methods on Lua numbers, bools, actions, nil, and coroutines
2 require "text"
4 number_mt =
5 __type: "a Number"
6 as_lua: tostring
7 as_nomsu: tostring
8 as_text: tostring
9 as_a_number: => @
10 rounded: => math.floor(@ + .5)
11 rounded_down: math.floor
12 rounded_up: math.ceil
13 to_the_nearest: (rounder)=> rounder * math.floor(@/rounder + 0.5)
14 base16: => ("%X")\format(@)
15 number_mt.__index = number_mt
16 debug.setmetatable 0, number_mt
18 bool_mt =
19 __type: "a Boolean"
20 __len: => @ and 1 or 0
21 as_lua: tostring
22 as_nomsu: => @ and "yes" or "no"
23 as_text: => @ and "yes" or "no"
24 _and: (cond)=> @ and cond
25 _or: (cond)=> @ or cond
26 xor: (cond)=> @ == (not cond)
27 bool_mt.__index = bool_mt
28 debug.setmetatable true, bool_mt
30 fn_mt =
31 __type: "an Action"
32 as_text: => (tostring(@)\gsub("function", "Action"))
33 __add: (other)=>
34 if type(@) == 'function' and type(other) == 'function'
35 (...)-> (@(...) + other(...))
36 elseif type(@) == 'function' then (...)-> (@(...) + other)
37 else (...)-> (@ + other(...))
38 __sub: (other)=>
39 if type(@) == 'function' and type(other) == 'function'
40 (...)-> (@(...) - other(...))
41 elseif type(@) == 'function' then (...)-> (@(...) - other)
42 else (...)-> (@ - other(...))
43 __mul: (other)=>
44 if type(@) == 'function' and type(other) == 'function'
45 (...)-> (@(...) * other(...))
46 elseif type(@) == 'function' then (...)-> (@(...) * other)
47 else (...)-> (@ * other(...))
48 __div: (other)=>
49 if type(@) == 'function' and type(other) == 'function'
50 (...)-> (@(...) other(...))
51 elseif type(@) == 'function' then (...)-> (@(...) other)
52 else (...)-> (@ other(...))
53 __mod: (other)=>
54 if type(@) == 'function' and type(other) == 'function'
55 (...)-> (@(...) % other(...))
56 elseif type(@) == 'function' then (...)-> (@(...) % other)
57 else (...)-> (@ % other(...))
58 __band: (other)=>
59 if type(@) == 'function' and type(other) == 'function'
60 (...)-> (@(...) and other(...))
61 elseif type(@) == 'function' then (...)-> (@(...) and other)
62 else (...)-> (@ and other(...))
63 __bor: (other)=>
64 if type(@) == 'function' and type(other) == 'function'
65 (...)-> (@(...) or other(...))
66 elseif type(@) == 'function' then (...)-> (@(...) or other)
67 else (...)-> (@ or other(...))
68 __bxor: (other)=>
69 if type(@) == 'function' and type(other) == 'function'
70 (...)-> (@(...) != other(...))
71 elseif type(@) == 'function' then (...)-> (@(...) != other)
72 else (...)-> (@ != other(...))
73 fn_mt.__index = fn_mt
74 debug.setmetatable (->), fn_mt
76 _last_co_i = setmetatable({}, {__mode:'k'})
77 local co_mt
78 co_mt =
79 __type: "a Coroutine"
80 as_text: => (tostring(@)\gsub("thread", "Coroutine")).." ("..coroutine.status(@)..")"
81 __len: => math.huge
82 __call: coroutine.resume
83 __inext: (k)=>
84 ok, val = coroutine.resume(@)
85 return if coroutine.status(@) == 'dead'
86 if ok then return (k or 0) + 1, val
87 __index: (k)=>
88 if k == (_last_co_i[@] or 0) + 1
89 ret = {coroutine.resume(@, k)}
90 _last_co_i[@] = k
91 if ret[1]
92 return table.unpack(ret, 2)
93 else
94 return nil
95 return co_mt[k]
96 co_mt.__next = co_mt.__inext
97 debug.setmetatable(coroutine.create(->), co_mt)
99 nil_mt =
100 __type: "Nil"
101 as_lua: => "nil"
102 as_nomsu: => "nil"
103 as_text: => "nil"
104 nil_mt.__index = nil_mt
105 debug.setmetatable nil, nil_mt