Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- IDEA: Put all string error messages in a separate file. Get table on load for
- -- better readability
- local Class = {}
- local ClassObject = {}
- local ClassProxy = {}
- local Object = {}
- local ObjectProxy = {}
- local ClassFunction = {}
- local ObjectFunction = {}
- local ClassValue = {}
- local ObjectValue = {}
- local ValueReference = {}
- local AccessController = {}
- local ClassParser = {}
- local currentClass = {}
- local Utils = {}
- local ClassTree = {}
- setmetatable(ClassParser,{
- __index = function(_,ClassName)
- return function(ClassBody)
- currentClass:setSuperByName(ClassName) -- set the superclass of the current class
- currentClass:buildClass(ClassBody) -- like old classics obj:construct()
- end
- end;
- __call = function(_,ClassBody)
- currentClass:buildClass(ClassBody)
- end;
- })
- function Utils.checkClassName(ClassName) -- check wether the classname is already in use or not
- if ClassTree[ClassName] then
- error("Class name is already in use, choose another name or delete old class")
- end
- end
- function Utils.getClassByName(ClassName)
- if not ClassTree[ClassName] then
- error("Can't find class "..ClassName..". Has it been created ?")
- end
- return ClassTree[ClassName]
- end
- function Utils.makeGlobal(ClassObj) -- make the new class globally usable
- currentClass = ClassObj
- _G[ClassObj]--[[:getName()]] = ClassObj
- end
- function Utils.isFieldAnnotation(FieldTable)
- -- TODO
- end
- function Utils.getFieldAnnotation(FieldTable)
- -- TODO
- end
- function Class.new(ClassName) -- create a new Class
- Utils.checkClassName(ClassName)
- local classObj = ClassObject(ClassName)
- ClassTree[ClassName] = classObj;
- Utils.makeGlobal(classObj)
- return ClassParser
- end
- function ClassObject.new(ClassName)
- local object = {
- type = "class";
- className = ClassName;
- inherits = false;
- super = nil;
- raw = {};
- properties = {};
- interfaces = {};
- classType = {[1] = ClassName};
- }
- setmetatable(object,{
- __index = ClassObject;
- __newindex = object.raw;
- __call = function(_,...) return object:instance() end -- TODO / WIP
- })
- return object
- end
- function ClassObject:setSuperByName(ClassName)
- local super = Utils.getClassByName(ClassName)
- self:setProperty("super",super)
- self:setProperty("inherits",true)
- end
- function ClassObject:buildClass(ClassBody)
- for fieldName,value in pairs(ClassBody) do
- if type(value)~= "function" then
- self:addValue(fieldName,value)
- else
- self:addFunction(fieldName,value)
- end
- end
- end
- function ClassObject:addValue(FieldName,Value)
- --TODO
- end
- function ClassObject:addFunction(FieldName,Value)
- -- TODO
- end
- function ClassObject:setProperty(PropName,Value)
- rawset(self,PropName,Value)
- end
- setmetatable(Class,{__call=function(_,ClassName) return Class.new(ClassName) end})
- setmetatable(ClassObject,{__call=function(_,ClassName) return ClassObject.new(ClassName) end})
- setmetatable(ClassProxy,{__call=function() return ClassProxy.new() end})
- setmetatable(Object,{__call=function() return Object.new() end})
- setmetatable(ObjectProxy,{__call=function() return ObjectProxy.new() end})
- setmetatable(ClassFunction,{__call=function() return ClassFunction.new() end})
- setmetatable(ObjectFunction,{__call=function() return ObjectFunction.new() end})
- setmetatable(ClassValue,{__call=function() return ClassValue.new() end})
- setmetatable(ObjectValue,{__call=function() return ObjectValue.new() end})
- setmetatable(ValueReference,{__call=function() return ValueReference.new() end})
- setmetatable(AccessController,{__call=function() return AccessController.new() end})
- _G.class = Class
- class "SuperTest" {
- }
- class "Test" : SuperTest {
- }
Advertisement
Add Comment
Please, Sign In to add comment