Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.76 KB | None | 0 0
  1.     local function class_generator(name, b, t)
  2.         local parents = {}
  3.         for _, v in ipairs(b) do
  4.             parents[v] = true
  5.             for _, v in ipairs(v.__parents__) do
  6.                 parents[v] = true
  7.             end
  8.         end
  9.  
  10.         local temp = { __parents__ = {} }
  11.         for i, v in pairs(parents) do
  12.             table.insert(temp.__parents__, i)
  13.         end
  14.  
  15.         local class = setmetatable(temp, {
  16.             __index = function(self, key)
  17.                 if key == "__class__" then return temp end
  18.                 if key == "__name__" then return name end
  19.                 if t[key] ~= nil then return t[key] end
  20.                 for i, v in ipairs(b) do
  21.                     if v[key] ~= nil then return v[key] end
  22.                 end
  23.                 if tostring(key):match("^__.+__$") then return end
  24.                 if self.__getattr__ then
  25.                     return self:__getattr__(key)
  26.                 end
  27.             end,
  28.  
  29.             __newindex = function(self, key, value)
  30.                 t[key] = value
  31.             end,
  32.  
  33.             allocate = function(instance)
  34.                 local smt = getmetatable(temp)
  35.                 local mt = {__index = smt.__index}
  36.                 function mt:__newindex(key, value)
  37.                     if self.__setattr__ then
  38.                         return self:__setattr__(key, value)
  39.                     else
  40.                         return rawset(self, key, value)
  41.                     end
  42.                 end
  43.  
  44.                 if temp.__cmp__ then
  45.                     if not smt.eq or not smt.lt then
  46.                         function smt.eq(a, b)
  47.                             return a.__cmp__(a, b) == 0
  48.                         end
  49.                         function smt.lt(a, b)
  50.                             return a.__cmp__(a, b) < 0
  51.                         end
  52.                     end
  53.                     mt.__eq = smt.eq
  54.                     mt.__lt = smt.lt
  55.                 end
  56.  
  57.                 for i, v in pairs{__call__ = "__call", __len__ = "__len", __add__ = "__add", __sub__ = "__sub", __mul__ = "__mul", __div__ = "__div", __mod__ = "__mod", __pow__ = "__pow", __neg__ = "__unm", __concat__ = "__concat", __str__ = "__tostring"} do
  58.                     if temp[i] then
  59.                         mt[v] = temp[i]
  60.                     end
  61.                 end
  62.  
  63.                 return setmetatable(instance or {}, mt)
  64.             end,
  65.  
  66.             __call = function(self, ...)
  67.                 local instance = getmetatable(self).allocate()
  68.                 if instance.__init__ then instance:__init__(...) end
  69.                 return instance
  70.             end
  71.             })
  72.  
  73.         for i, v in ipairs(t.__attributes__ or {}) do
  74.             class = v(class) or class
  75.         end
  76.  
  77.         return class
  78.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement