Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Class = {}
- Class.__index = Class
- function copy(array)
- local new = {}
- for i, v in pairs(array) do
- new[i] = v
- end
- return new
- end
- function Class.__newindex(self, key, value)
- if key == "extends" then
- local meta = getmetatable(self)
- meta.__index = function(this, key)
- return rawget(self, key) or rawget(value, key) or rawget(module, key)
- end
- return setmetatable(self, meta)
- else
- rawset(self, key, value)
- end
- end
- function Class.__call(self, ...)
- local new = setmetatable(copy(self), getmetatable(self))
- if new.init then
- new:init(...)
- end
- return new
- end
- setmetatable(Class, {__call = function() return setmetatable({}, Class) end})
- function getByClassName(name)
- for i, v in pairs(environment) do
- if v._COMPILE_CODE:find(name:gsub("%s+", "")) then
- return i
- end
- end
- return false
- end
- local keywords = {
- ["func(.-)%((.-)%)%s*{%s*(.-)};"] = function(name, args, block)
- environment[getByClassName(name)][name] = loadstring("return function(self ".. (args:find("%S") and "," or "") .. args ..") "..block.." end")()
- end,
- ["override func(.-)%((.-)%)%s*{%s*(.-)};"] = function(name, args, block)
- if not environment[getByClassName(name)][name] then
- error("Cannot override a funciton that does not exist.")
- end
- environment[getByClassName(name)][name] = loadstring("return function(self ".. (args:find("%S") and "," or "") .. args ..") "..block.." end")()
- end,
- ["var(.-)=(.-);"] = function(name, value)
- environment[getByClassName(name)][name] = value
- end
- }
- function run(code)
- environment = {}
- local extenders = {}
- code = code:gsub("class (.-)%->(.-)<%-", function(name, block)
- if name:find("extends") then
- local save = name:match("extends (%a+)")
- name = name:gsub("extends (%a+)", "")
- extenders[name:gsub("%s+", "")] = save
- end
- return ("environment._NAME = {_COMPILE_CODE = " .. "[[" .. "%s" .. "]]}"):gsub("_NAME", tostring(name)):format(block)
- end)
- loadstring(code)()
- for i, v in pairs(keywords) do
- code:gsub(i, v)
- end
- for className, class in pairs(environment) do
- getfenv()[className] = Class()
- for key, value in pairs(class) do
- getfenv()[className][key:gsub("%s+", "")] = value
- end
- end
- for className, class in pairs(getfenv()) do
- if extenders[className] then
- class.extends = getfenv()[extenders[className]]
- end
- end
- end
- run([[
- class UIPhysics ->
- func isOn(other) {
- return
- self.x + self.sx > other.x and
- self.y + self.sy > other.y and
- other.x + other.sx > self.x and
- other.y + other.sy > self.y
- };
- func area() {
- return self.sx * self.sy
- };
- <-
- class Body extends UIPhysics ->
- func init(pos, size) {
- self.x = pos.X
- self.sx = size.X
- self.y = pos.Y
- self.sy = size.Y
- };
- <-
- ]])
- local square = Body(Vector2.new(0, 0), Vector2.new(50, 50))
- print(square:area())
Advertisement
Add Comment
Please, Sign In to add comment