ForeverDev

Untitled

Nov 15th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.85 KB | None | 0 0
  1. local Class = {}
  2. Class.__index = Class
  3.  
  4. function copy(array)
  5.     local new = {}
  6.     for i, v in pairs(array) do
  7.         new[i] = v
  8.     end
  9.     return new
  10. end
  11.  
  12. function Class.__newindex(self, key, value)
  13.     if key == "extends" then
  14.         local meta = getmetatable(self)
  15.         meta.__index = function(this, key)
  16.             return rawget(self, key) or rawget(value, key) or rawget(module, key)
  17.         end
  18.         return setmetatable(self, meta)
  19.     else
  20.         rawset(self, key, value)
  21.     end
  22. end
  23.  
  24. function Class.__call(self, ...)
  25.     local new = setmetatable(copy(self), getmetatable(self))
  26.     if new.init then
  27.         new:init(...)
  28.     end
  29.     return new
  30. end
  31.  
  32. setmetatable(Class, {__call = function() return setmetatable({}, Class) end})
  33.  
  34. function getByClassName(name)
  35.     for i, v in pairs(environment) do
  36.         if v._COMPILE_CODE:find(name:gsub("%s+", "")) then
  37.             return i
  38.         end
  39.     end
  40.     return false
  41. end
  42.  
  43. local keywords = {
  44.     ["func(.-)%((.-)%)%s*{%s*(.-)};"] = function(name, args, block)
  45.         environment[getByClassName(name)][name] = loadstring("return function(self ".. (args:find("%S") and "," or "") .. args ..") "..block.." end")()
  46.     end,
  47.     ["override func(.-)%((.-)%)%s*{%s*(.-)};"] = function(name, args, block)
  48.         if not environment[getByClassName(name)][name] then
  49.             error("Cannot override a funciton that does not exist.")
  50.         end
  51.         environment[getByClassName(name)][name] = loadstring("return function(self ".. (args:find("%S") and "," or "") .. args ..") "..block.." end")()
  52.     end,
  53.     ["var(.-)=(.-);"] = function(name, value)
  54.         environment[getByClassName(name)][name] = value
  55.     end
  56. }
  57.  
  58. function run(code)
  59.     environment = {}
  60.     local extenders = {}
  61.     code = code:gsub("class (.-)%->(.-)<%-", function(name, block)
  62.         if name:find("extends") then
  63.             local save = name:match("extends (%a+)")
  64.             name = name:gsub("extends (%a+)", "")
  65.             extenders[name:gsub("%s+", "")] = save
  66.         end
  67.         return ("environment._NAME = {_COMPILE_CODE = " .. "[[" .. "%s" .. "]]}"):gsub("_NAME", tostring(name)):format(block)
  68.     end)
  69.     loadstring(code)()
  70.     for i, v in pairs(keywords) do
  71.         code:gsub(i, v)
  72.     end
  73.     for className, class in pairs(environment) do
  74.         getfenv()[className] = Class()
  75.         for key, value in pairs(class) do
  76.             getfenv()[className][key:gsub("%s+", "")] = value
  77.         end
  78.     end
  79.     for className, class in pairs(getfenv()) do
  80.         if extenders[className] then
  81.             class.extends = getfenv()[extenders[className]]
  82.         end
  83.     end
  84. end
  85.  
  86. run([[
  87.    
  88.     class UIPhysics ->
  89.         func isOn(other) {
  90.             return
  91.                 self.x + self.sx > other.x and
  92.                 self.y + self.sy > other.y and
  93.                 other.x + other.sx > self.x and
  94.                 other.y + other.sy > self.y
  95.         };
  96.         func area() {
  97.             return self.sx * self.sy
  98.         };
  99.     <-
  100.    
  101.     class Body extends UIPhysics ->
  102.         func init(pos, size) {
  103.             self.x = pos.X
  104.             self.sx = size.X
  105.             self.y = pos.Y
  106.             self.sy = size.Y
  107.         };
  108.     <-
  109.    
  110. ]])
  111.  
  112. local square = Body(Vector2.new(0, 0), Vector2.new(50, 50))
  113. print(square:area())
Advertisement
Add Comment
Please, Sign In to add comment