diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2019-02-02 19:30:07 -0800 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2019-02-02 19:31:15 -0800 |
| commit | 11997f02552468d390c82b812d3800a97e7a8dac (patch) | |
| tree | bef658592e0bf2ddf0ed9bb8435230c50e6e8401 /builtin_metatables.lua | |
| parent | e8e00adeaa29cfc0e7b581550bc1bca15f4ddc69 (diff) | |
Added some more metamethods including: (#5 == 5, ((-> (foo)) | (->
(baz))) == (-> ((foo) or (baz)))
Diffstat (limited to 'builtin_metatables.lua')
| -rw-r--r-- | builtin_metatables.lua | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/builtin_metatables.lua b/builtin_metatables.lua index e579e6c..f659ad3 100644 --- a/builtin_metatables.lua +++ b/builtin_metatables.lua @@ -1,6 +1,9 @@ require("text") local number_mt = { __type = "a Number", + __len = function(self) + return self + end, as_lua = tostring, as_nomsu = tostring, as_text = tostring, @@ -23,12 +26,24 @@ number_mt.__index = number_mt debug.setmetatable(0, number_mt) local bool_mt = { __type = "a Boolean", + __len = function(self) + return self and 1 or 0 + end, as_lua = tostring, as_nomsu = function(self) return self and "yes" or "no" end, as_text = function(self) return self and "yes" or "no" + end, + _and = function(self, cond) + return self and cond + end, + _or = function(self, cond) + return self or cond + end, + xor = function(self, cond) + return self == (not cond) end } bool_mt.__index = bool_mt @@ -37,6 +52,126 @@ local fn_mt = { __type = "an Action", as_text = function(self) return (tostring(self):gsub("function", "Action")) + end, + __add = function(self, other) + if type(self) == 'function' and type(other) == 'function' then + return function(...) + return (self(...) + other(...)) + end + elseif type(self) == 'function' then + return function(...) + return (self(...) + other) + end + else + return function(...) + return (self + other(...)) + end + end + end, + __sub = function(self, other) + if type(self) == 'function' and type(other) == 'function' then + return function(...) + return (self(...) - other(...)) + end + elseif type(self) == 'function' then + return function(...) + return (self(...) - other) + end + else + return function(...) + return (self - other(...)) + end + end + end, + __mul = function(self, other) + if type(self) == 'function' and type(other) == 'function' then + return function(...) + return (self(...) * other(...)) + end + elseif type(self) == 'function' then + return function(...) + return (self(...) * other) + end + else + return function(...) + return (self * other(...)) + end + end + end, + __div = function(self, other) + if type(self) == 'function' and type(other) == 'function' then + return function(...) + return (self(...)(other(...))) + end + elseif type(self) == 'function' then + return function(...) + return (self(...)(other)) + end + else + return function(...) + return (self(other(...))) + end + end + end, + __mod = function(self, other) + if type(self) == 'function' and type(other) == 'function' then + return function(...) + return (self(...) % other(...)) + end + elseif type(self) == 'function' then + return function(...) + return (self(...) % other) + end + else + return function(...) + return (self % other(...)) + end + end + end, + __band = function(self, other) + if type(self) == 'function' and type(other) == 'function' then + return function(...) + return (self(...) and other(...)) + end + elseif type(self) == 'function' then + return function(...) + return (self(...) and other) + end + else + return function(...) + return (self and other(...)) + end + end + end, + __bor = function(self, other) + if type(self) == 'function' and type(other) == 'function' then + return function(...) + return (self(...) or other(...)) + end + elseif type(self) == 'function' then + return function(...) + return (self(...) or other) + end + else + return function(...) + return (self or other(...)) + end + end + end, + __bxor = function(self, other) + if type(self) == 'function' and type(other) == 'function' then + return function(...) + return (self(...) ~= other(...)) + end + elseif type(self) == 'function' then + return function(...) + return (self(...) ~= other) + end + else + return function(...) + return (self ~= other(...)) + end + end end } fn_mt.__index = fn_mt |
