code / nomsu

Lines6.6K Lua5.1K PEG1.3K make117
2 others 83
Markdown60 Bourne Again Shell23
(228 lines)
1 require("text")
2 local number_mt = {
3 __type = "a Number",
4 as_lua = tostring,
5 as_nomsu = tostring,
6 as_text = tostring,
7 as_a_number = function(self)
8 return self
9 end,
10 rounded = function(self)
11 return math.floor(self + .5)
12 end,
13 rounded_down = math.floor,
14 rounded_up = math.ceil,
15 to_the_nearest = function(self, rounder)
16 return rounder * math.floor(self / rounder + 0.5)
17 end,
18 base16 = function(self)
19 return ("%X"):format(self)
20 end
22 number_mt.__index = number_mt
23 debug.setmetatable(0, number_mt)
24 local bool_mt = {
25 __type = "a Boolean",
26 __len = function(self)
27 return self and 1 or 0
28 end,
29 as_lua = tostring,
30 as_nomsu = function(self)
31 return self and "yes" or "no"
32 end,
33 as_text = function(self)
34 return self and "yes" or "no"
35 end,
36 _and = function(self, cond)
37 return self and cond
38 end,
39 _or = function(self, cond)
40 return self or cond
41 end,
42 xor = function(self, cond)
43 return self == (not cond)
44 end
46 bool_mt.__index = bool_mt
47 debug.setmetatable(true, bool_mt)
48 local fn_mt = {
49 __type = "an Action",
50 as_text = function(self)
51 return (tostring(self):gsub("function", "Action"))
52 end,
53 __add = function(self, other)
54 if type(self) == 'function' and type(other) == 'function' then
55 return function(...)
56 return (self(...) + other(...))
57 end
58 elseif type(self) == 'function' then
59 return function(...)
60 return (self(...) + other)
61 end
62 else
63 return function(...)
64 return (self + other(...))
65 end
66 end
67 end,
68 __sub = function(self, other)
69 if type(self) == 'function' and type(other) == 'function' then
70 return function(...)
71 return (self(...) - other(...))
72 end
73 elseif type(self) == 'function' then
74 return function(...)
75 return (self(...) - other)
76 end
77 else
78 return function(...)
79 return (self - other(...))
80 end
81 end
82 end,
83 __mul = function(self, other)
84 if type(self) == 'function' and type(other) == 'function' then
85 return function(...)
86 return (self(...) * other(...))
87 end
88 elseif type(self) == 'function' then
89 return function(...)
90 return (self(...) * other)
91 end
92 else
93 return function(...)
94 return (self * other(...))
95 end
96 end
97 end,
98 __div = function(self, other)
99 if type(self) == 'function' and type(other) == 'function' then
100 return function(...)
101 return (self(...)(other(...)))
102 end
103 elseif type(self) == 'function' then
104 return function(...)
105 return (self(...)(other))
106 end
107 else
108 return function(...)
109 return (self(other(...)))
110 end
111 end
112 end,
113 __mod = function(self, other)
114 if type(self) == 'function' and type(other) == 'function' then
115 return function(...)
116 return (self(...) % other(...))
117 end
118 elseif type(self) == 'function' then
119 return function(...)
120 return (self(...) % other)
121 end
122 else
123 return function(...)
124 return (self % other(...))
125 end
126 end
127 end,
128 __band = function(self, other)
129 if type(self) == 'function' and type(other) == 'function' then
130 return function(...)
131 return (self(...) and other(...))
132 end
133 elseif type(self) == 'function' then
134 return function(...)
135 return (self(...) and other)
136 end
137 else
138 return function(...)
139 return (self and other(...))
140 end
141 end
142 end,
143 __bor = function(self, other)
144 if type(self) == 'function' and type(other) == 'function' then
145 return function(...)
146 return (self(...) or other(...))
147 end
148 elseif type(self) == 'function' then
149 return function(...)
150 return (self(...) or other)
151 end
152 else
153 return function(...)
154 return (self or other(...))
155 end
156 end
157 end,
158 __bxor = function(self, other)
159 if type(self) == 'function' and type(other) == 'function' then
160 return function(...)
161 return (self(...) ~= other(...))
162 end
163 elseif type(self) == 'function' then
164 return function(...)
165 return (self(...) ~= other)
166 end
167 else
168 return function(...)
169 return (self ~= other(...))
170 end
171 end
172 end
174 fn_mt.__index = fn_mt
175 debug.setmetatable((function() end), fn_mt)
176 local _last_co_i = setmetatable({ }, {
177 __mode = 'k'
179 local co_mt
180 co_mt = {
181 __type = "a Coroutine",
182 as_text = function(self)
183 return (tostring(self):gsub("thread", "Coroutine")) .. " (" .. coroutine.status(self) .. ")"
184 end,
185 __len = function(self)
186 return math.huge
187 end,
188 __call = coroutine.resume,
189 __inext = function(self, k)
190 local ok, val = coroutine.resume(self)
191 if coroutine.status(self) == 'dead' then
192 return
193 end
194 if ok then
195 return (k or 0) + 1, val
196 end
197 end,
198 __index = function(self, k)
199 if k == (_last_co_i[self] or 0) + 1 then
200 local ret = {
201 coroutine.resume(self, k)
203 _last_co_i[self] = k
204 if ret[1] then
205 return table.unpack(ret, 2)
206 else
207 return nil
208 end
209 end
210 return co_mt[k]
211 end
213 co_mt.__next = co_mt.__inext
214 debug.setmetatable(coroutine.create(function() end), co_mt)
215 local nil_mt = {
216 __type = "Nil",
217 as_lua = function(self)
218 return "nil"
219 end,
220 as_nomsu = function(self)
221 return "nil"
222 end,
223 as_text = function(self)
224 return "nil"
225 end
227 nil_mt.__index = nil_mt
228 return debug.setmetatable(nil, nil_mt)