Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function class_generator(name, b, t)
- local parents = {}
- for _, v in ipairs(b) do
- parents[v] = true
- for _, v in ipairs(v.__parents__) do
- parents[v] = true
- end
- end
- local temp = { __parents__ = {} }
- for i, v in pairs(parents) do
- table.insert(temp.__parents__, i)
- end
- local class = setmetatable(temp, {
- __index = function(self, key)
- if key == "__class__" then return temp end
- if key == "__name__" then return name end
- if t[key] ~= nil then return t[key] end
- for i, v in ipairs(b) do
- if v[key] ~= nil then return v[key] end
- end
- if tostring(key):match("^__.+__$") then return end
- if self.__getattr__ then
- return self:__getattr__(key)
- end
- end,
- __newindex = function(self, key, value)
- t[key] = value
- end,
- allocate = function(instance)
- local smt = getmetatable(temp)
- local mt = {__index = smt.__index}
- function mt:__newindex(key, value)
- if self.__setattr__ then
- return self:__setattr__(key, value)
- else
- return rawset(self, key, value)
- end
- end
- if temp.__cmp__ then
- if not smt.eq or not smt.lt then
- function smt.eq(a, b)
- return a.__cmp__(a, b) == 0
- end
- function smt.lt(a, b)
- return a.__cmp__(a, b) < 0
- end
- end
- mt.__eq = smt.eq
- mt.__lt = smt.lt
- end
- 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
- if temp[i] then
- mt[v] = temp[i]
- end
- end
- return setmetatable(instance or {}, mt)
- end,
- __call = function(self, ...)
- local instance = getmetatable(self).allocate()
- if instance.__init__ then instance:__init__(...) end
- return instance
- end
- })
- for i, v in ipairs(t.__attributes__ or {}) do
- class = v(class) or class
- end
- return class
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement