Kouksi44

annotation

Mar 11th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1.  
  2. local annotation = function() end
  3.  
  4. local isClass = function(t)
  5.   if type(t) ~= "table" then return false end
  6.   if not getmetatable(t) then return false end
  7.   if getmetatable(t).__type == "class" then return true end
  8.   return false
  9. end
  10.  
  11.  
  12.  
  13.  
  14. local parseannotations = function(left,right)
  15.   if type(left) == "table" and rawget(left,"__type") == "annotation" and type(right) == "table" and rawget(right,"__type") == "annotation" then
  16.     local ann = annotation()
  17.     right.copyto(ann)
  18.     left.copyto(ann)
  19.     return ann
  20.   elseif (isClass(left)) or (isClass(right)) then
  21.     if isClass(left) then
  22.       if type(right) == "table" and rawget(right,"__type") == "annotation" then
  23.         local a = annotation("object")
  24.         right.copyto(a)
  25.         a.objType = getmetatable(left).__class.name
  26.         return a
  27.       else
  28.         local a = annotation("object")
  29.         a.objType = getmetatable(left).__class.name
  30.         a.setValue(right)
  31.         return a
  32.       end
  33.     elseif isClass(right) then
  34.       local a = annotation("object")
  35.       left.copyto(a)
  36.       a.objType = getmetatable(right).__class.name
  37.       return a
  38.     end
  39.   elseif rawget(left,"__type") == "annotation" and (not (type(right) == "table" and (rawget(right,"__type") == "annotation" or rawget(right,"__type") == "annotation"))) then
  40.     local ann = annotation()
  41.     ann.setValue(right)
  42.     left.copyto(ann)
  43.     ann.objType = left.objType
  44.     return ann
  45.   end
  46. end
  47.  
  48.  
  49.  
  50. annotation = function(desc)
  51.   local ann = {}
  52.       ann.__type = "annotation"
  53.       ann.value = nil
  54.       ann.type = _type
  55.       ann.descriptors = {[1] = desc}
  56.       ann.objType = nil
  57.       ann.get = function() return ann.descriptors end
  58.       ann.copyto = function(next)
  59.         for i=1,#ann.descriptors do
  60.           table.insert(next.descriptors,ann.descriptors[i])
  61.         end
  62.         next.objType = ann.objType
  63.       end
  64.     ann.setValue = function(val)
  65.       ann.value = val
  66.       ann.type = (type(val) == "function" and "method") or "attribute"
  67.     end
  68.  
  69.   return setmetatable(ann,{
  70.     __sub = parseannotations
  71.   })
  72. end
  73.  
  74. return annotation, parseannotations
Advertisement
Add Comment
Please, Sign In to add comment