Guest User

Arderas Link Library 2

a guest
Oct 19th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.52 KB | None | 0 0
  1. local links = {}
  2. local slinks = {}
  3. local cslinks = setmetatable({}, {__index = function(t, k) return slinks[k]() end})
  4. local adresstolink = {}
  5. local adresstoslink = {}
  6.  
  7. local __eq_l = function(a, b)
  8.     return adresstolink[a] == adresstolink[b]
  9. end
  10.  
  11. local __eq_sl = function(a, b)
  12.     return adresstoslink[a] == adresstoslink[b]
  13. end
  14.  
  15. function link(val, id)
  16.     local wrong_type = "Link Value must be a byval type. (number, string, boolean, nil)"
  17.     if not id then
  18.         id = math.random()
  19.         while links[id] do
  20.             id = math.random()
  21.         end
  22.     end
  23.    
  24.     if type(val) == "number" or type(f) == "string" or type(f) == "boolean" then
  25.         links[id] = val
  26.     elseif type(val) ~= "nil" then
  27.         error(wrong_type, 2)
  28.     end
  29.    
  30.     local l = setmetatable({}, {
  31.         __newindex = function(t, k, v)
  32.             if k == "s" and (type(v) == "number" or type(v) == "string" or type(v) == "boolean" or v == nil) then
  33.                 -- only byval types allowed
  34.                 links[id] = v
  35.             else
  36.                 error(wrong_type, 2)
  37.             end
  38.         end,
  39.         __index = function(t, k)
  40.             if k == "g" then
  41.                 return links[id]
  42.             end
  43.         end,
  44.         __call = function()
  45.             return link(nil, id)
  46.         end,
  47.         __metatable = "link",
  48.         __tostring = function()
  49.             return tostring(links[id])
  50.         end,
  51.         __unm = function()
  52.             return -links[id]
  53.         end,
  54.         __add = function(a, b)
  55.             return links[id] + b
  56.         end,
  57.         __sub = function(a, b)
  58.             return links[id] - b
  59.         end,
  60.         __mul = function(a, b)
  61.             return links[id] * b
  62.         end,
  63.         __div = function(a, b)
  64.             return links[id] / b
  65.         end,
  66.         __mod = function(a, b)
  67.             return links[id] % b
  68.         end,
  69.         __pow = function(a, b)
  70.             return links[id] ^ b
  71.         end,
  72.         __concat = function(a, b)
  73.             return tostring(links[id]) .. tostring(b)
  74.         end,
  75.         __eq = __eq_l
  76.     })
  77.     adresstolink[l] = id
  78.    
  79.     return l
  80. end
  81.  
  82. fslink = 0
  83.  
  84. function slink(f, id)
  85.     local wrong_type = "Stronglink value must be a byval type (number, string, boolean, nil) or a function."
  86.     if not id then
  87.         id = math.random()
  88.         while slinks[id] do
  89.             id = math.random()
  90.         end
  91.     end
  92.    
  93.     if type(f) == "function" then
  94.         slinks[id] = f
  95.     elseif type(f) == "number" or type(f) == "string" or type(f) == "boolean" then
  96.         slinks[id] = function() return f end
  97.     elseif type(f) ~= "nil" then
  98.         error(wrong_type, 2)
  99.     end
  100.    
  101.     local l = setmetatable({}, {
  102.         __newindex = function(t, k, v)
  103.             if k == "s" and (type(v) == "number" or type(v) == "string" or type(f) == "boolean" or v == nil) then
  104.                 -- only byval types allowed
  105.                 slinks[id] = function() return v end
  106.             else
  107.                 error(wrong_type, 2)
  108.             end
  109.         end,
  110.         __index = function(t, k)
  111.             if k == "g" then
  112.                 return cslinks[id]
  113.             end
  114.         end,
  115.         __call = function()
  116.             return slink(nil, id)
  117.         end,
  118.         __metatable = "stronglink",
  119.         __tostring = function()
  120.             return tostring(cslinks[id])
  121.         end,
  122.         __unm = function()
  123.             return slink(function() return -cslinks[id] end)
  124.         end,
  125.         __add = function(a, b)
  126.             return slink(function() return cslinks[id] + b end)
  127.         end,
  128.         __sub = function(a, b)
  129.             return slink(function() return cslinks[id] - b end)
  130.         end,
  131.         __mul = function(a, b)
  132.             return slink(function() return cslinks[id] * b end)
  133.         end,
  134.         __div = function(a, b)
  135.             return slink(function() return cslinks[id] / b end)
  136.         end,
  137.         __mod = function(a, b)
  138.             return slink(function() return cslinks[id] % b end)
  139.         end,
  140.         __pow = function(a, b)
  141.             return slink(function() return cslinks[id] ^ b end)
  142.         end,
  143.         __concat = function(a, b)
  144.             return slink(function() return tostring(cslinks[id]) .. tostring(b) end)
  145.         end,
  146.         __eq = __eq_sl
  147.     })
  148.    
  149.     adresstoslink[l] = id
  150.    
  151.     return l
  152. end
Advertisement
Add Comment
Please, Sign In to add comment