Kouksi44

Classic Reloaded

Jan 16th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.66 KB | None | 0 0
  1.  
  2. -- IDEA: Put all string error messages in a separate file. Get table on load for
  3. --       better readability
  4.  
  5. local Class = {}
  6. local ClassObject = {}
  7. local ClassProxy = {}
  8. local Object = {}
  9. local ObjectProxy = {}
  10. local ClassFunction = {}
  11. local ObjectFunction = {}
  12. local ClassValue = {}
  13. local ObjectValue = {}
  14. local ValueReference = {}
  15. local AccessController = {}
  16. local ClassParser = {}
  17.  
  18. local currentClass = {}
  19. local Utils = {}
  20.  
  21. local ClassTree = {}
  22.  
  23.  
  24. setmetatable(ClassParser,{
  25.     __index = function(_,ClassName)
  26.         return function(ClassBody)
  27.             currentClass:setSuperByName(ClassName) -- set the superclass of the current class
  28.             currentClass:buildClass(ClassBody) -- like old classics obj:construct()
  29.         end
  30.     end;
  31.     __call = function(_,ClassBody)
  32.         currentClass:buildClass(ClassBody)
  33.     end;
  34. })
  35.  
  36. function Utils.checkClassName(ClassName) -- check wether the classname is already in use or not
  37.     if ClassTree[ClassName] then
  38.         error("Class name is already in use, choose another name or delete old class")
  39.     end
  40. end
  41.  
  42. function Utils.getClassByName(ClassName)
  43.     if not ClassTree[ClassName] then
  44.         error("Can't find class "..ClassName..". Has it been created ?")
  45.     end
  46.    
  47.     return ClassTree[ClassName]
  48. end
  49.  
  50. function Utils.makeGlobal(ClassObj) -- make the new class globally usable
  51.     currentClass = ClassObj
  52.     _G[ClassObj]--[[:getName()]] = ClassObj
  53. end
  54.  
  55. function Utils.isFieldAnnotation(FieldTable)
  56.     -- TODO
  57. end
  58.  
  59. function Utils.getFieldAnnotation(FieldTable)
  60.     -- TODO
  61. end
  62.  
  63. function Class.new(ClassName) -- create a new Class
  64.     Utils.checkClassName(ClassName)
  65.    
  66.     local classObj = ClassObject(ClassName)
  67.     ClassTree[ClassName] = classObj;
  68.    
  69.     Utils.makeGlobal(classObj)
  70.     return ClassParser
  71. end
  72.  
  73. function ClassObject.new(ClassName)
  74.     local object = {
  75.         type = "class";
  76.         className = ClassName;
  77.         inherits = false;
  78.         super = nil;
  79.         raw = {};
  80.         properties = {};
  81.         interfaces = {};
  82.         classType = {[1] = ClassName};
  83.     }
  84.     setmetatable(object,{
  85.         __index = ClassObject;
  86.         __newindex = object.raw;
  87.         __call = function(_,...) return object:instance() end -- TODO / WIP
  88.     })
  89.    
  90.     return object
  91. end
  92.  
  93. function ClassObject:setSuperByName(ClassName)
  94.     local super = Utils.getClassByName(ClassName)
  95.    
  96.     self:setProperty("super",super)
  97.     self:setProperty("inherits",true)
  98. end
  99.  
  100. function ClassObject:buildClass(ClassBody)
  101.     for fieldName,value in pairs(ClassBody) do
  102.         if type(value)~= "function" then
  103.             self:addValue(fieldName,value)
  104.         else
  105.             self:addFunction(fieldName,value)
  106.         end
  107.     end
  108. end
  109.  
  110. function ClassObject:addValue(FieldName,Value)
  111.     --TODO
  112. end
  113.  
  114. function ClassObject:addFunction(FieldName,Value)
  115.     -- TODO
  116. end
  117.  
  118. function ClassObject:setProperty(PropName,Value)
  119.     rawset(self,PropName,Value)
  120. end
  121.  
  122.  
  123.  
  124. setmetatable(Class,{__call=function(_,ClassName) return Class.new(ClassName) end})
  125. setmetatable(ClassObject,{__call=function(_,ClassName) return ClassObject.new(ClassName) end})
  126. setmetatable(ClassProxy,{__call=function() return ClassProxy.new() end})
  127. setmetatable(Object,{__call=function() return Object.new() end})
  128. setmetatable(ObjectProxy,{__call=function() return ObjectProxy.new() end})
  129. setmetatable(ClassFunction,{__call=function() return ClassFunction.new() end})
  130. setmetatable(ObjectFunction,{__call=function() return ObjectFunction.new() end})
  131. setmetatable(ClassValue,{__call=function() return ClassValue.new() end})
  132. setmetatable(ObjectValue,{__call=function() return ObjectValue.new() end})
  133. setmetatable(ValueReference,{__call=function() return ValueReference.new() end})
  134. setmetatable(AccessController,{__call=function() return AccessController.new() end})
  135.  
  136. _G.class = Class
  137.  
  138.  
  139. class "SuperTest" {
  140.    
  141. }
  142.  
  143. class "Test" : SuperTest {
  144.    
  145. }
Advertisement
Add Comment
Please, Sign In to add comment