Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------
- --CLASS--
- ---------
- local class_list = {}
- local class_membercopy = function(v)
- return type(v)=="table" and table.copy(v) or v
- end
- local class_extends = setmetatable({},{
- __index = function(t,k) return{1, class_list[k] or error("base class not found: '"..tostring(k).."'")} end
- })
- local class_operator = function(op)
- return function(f) return {2,{op,f}} end
- end
- local class_mt = {
- __newindex = function(t,name,cls)
- local constructor = cls[name] or function() end
- local funcs,members = {},{}
- local extends,meta = {},{__index = funcs}
- local function fillextends(t)
- if table.find(extends,t) then error("class '"..name.."' can only inherit '"..t.name.."' once") end
- extends[#extends+1] = t
- for i=1,#t[4] do
- fillextends(t[4][i])
- end
- end
- if type(name)~="string" then error("class name must be a string") end
- if type(constructor)~="function" then error("member with same name as class must be constructor function") end
- for k,v in pairs(cls) do
- if type(k) == "string" then
- local x = type(v) == "function" and funcs or members
- x[k] = class_membercopy(v)
- elseif type(k) == "number" then
- assert(type(v)=="table","anonymous members are not allowed")
- if v[1] == 1 then
- fillextends(v[2])
- elseif v[1] == 2 then
- local op = v[2][1]
- local f = v[2][2]
- local t ={
- ["+"] = "__add", ["*"] = "__mult", ["-"] = "__sub", ["/"] = "__div", ["%"] = "__mod",
- ["^"] = "__pow", ["--"] = "__unm", [".."] = "__concat", ["#"] = "__len",
- ["()"] = "__call", ["~"] = "__gc"
- }
- meta[t[op] or op] = f
- end
- end
- end
- for i=1,#extends do
- for k,v in pairs(extends[i][3]) do members[k] = members[k] or v end
- end
- local constr = function(t, ...)
- for i=1,#extends do extends[i][1](t) end
- constructor(t,...)
- end
- class_list[name] = {constr,funcs,members,extends,meta,name=name}
- funcs["type"] = function(t) return class_list[name] end
- setmetatable(funcs,{
- __index = function(t,k)
- for i=1,#extends do
- if extends[i][2][k] then return extends[i][2][k] end
- end
- end
- })
- end,
- __index = function(t,name)
- if name == "extends" then return class_extends
- elseif name == "operator" then return class_operator end
- local cls = class_list[name]
- if not cls then
- return function(x) t[name] = x end
- end
- local r = {}
- for k,v in pairs(cls[3]) do
- r[k] = class_membercopy(v)
- end
- return function(...)
- setmetatable(r,cls[5])
- cls[1](r,...)
- return r
- end
- end
- }
- class = setmetatable({},class_mt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement