From 11997f02552468d390c82b812d3800a97e7a8dac Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 2 Feb 2019 19:30:07 -0800 Subject: Added some more metamethods including: (#5 == 5, ((-> (foo)) | (-> (baz))) == (-> ((foo) or (baz))) --- builtin_metatables.lua | 135 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) (limited to 'builtin_metatables.lua') 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 -- cgit v1.2.3