Created
June 9, 2021 06:32
-
-
Save disco0/88ba390d970197be97210ec9f913d445 to your computer and use it in GitHub Desktop.
Class script for Lua, includes inheritance (macro) (DO NOT USE PLEASE THIS SUCKS)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; this, https://gist.github.com/nikneym/b7422a73c91d632e95ad34a813aad105 | |
;; but stuffed in a Fennel macro. | |
(macro class [mother] | |
`(let [o# {} | |
mother# (or ,mother nil)] | |
(tset o# :__index o#) | |
(when mother# | |
(tset o# :super mother#) | |
(each [k# v# (pairs mother#)] | |
(when (not= 1 (k#:find "__")) | |
(tset o# k# v#)))) | |
(setmetatable o# {:__call (fn [self# ...] | |
(let [new# (setmetatable {} self#)] | |
(new#:new ...) | |
new#))}))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import-macros {: class} :class) | |
;; Entity class | |
(local Entity (class)) | |
(lambda Entity.new [self x y w h] | |
(set (self.x self.y self.w self.h) (values x y w h))) | |
(fn Entity.draw [self] | |
(print (.. "X: " self.x)) | |
(print (.. "Y: " self.y)) | |
(print (.. "W: " self.w)) | |
(print (.. "H: " self.h))) | |
;; Player is subclass of Entity class | |
(local Player (class Entity)) | |
(lambda Player.new [self x y w h] | |
;; super call for constructor function of Mother class | |
(self.super.new self x y w h)) | |
(local player (Player 100 100 32 32)) | |
(player:draw) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local Entity = nil | |
do | |
local o_0_ = {} | |
local mother_0_ = (nil or nil) | |
o_0_["__index"] = o_0_ | |
if mother_0_ then | |
o_0_["super"] = mother_0_ | |
for k_0_, v_0_ in pairs(mother_0_) do | |
if (1 ~= k_0_:find("__")) then | |
o_0_[k_0_] = v_0_ | |
end | |
end | |
end | |
local function _1_(self_0_, ...) | |
local new_0_ = setmetatable({}, self_0_) | |
new_0_:new(...) | |
return new_0_ | |
end | |
Entity = setmetatable(o_0_, {__call = _1_}) | |
end | |
Entity.new = function(self, x, y, w, h) | |
assert((nil ~= h), string.format("Missing argument %s on %s:%s", "h", "class-test.fnl", 21)) | |
assert((nil ~= w), string.format("Missing argument %s on %s:%s", "w", "class-test.fnl", 21)) | |
assert((nil ~= y), string.format("Missing argument %s on %s:%s", "y", "class-test.fnl", 21)) | |
assert((nil ~= x), string.format("Missing argument %s on %s:%s", "x", "class-test.fnl", 21)) | |
assert((nil ~= self), string.format("Missing argument %s on %s:%s", "self", "class-test.fnl", 21)) | |
self.x, self.y, self.w, self.h = x, y, w, h | |
return nil | |
end | |
Entity.draw = function(self) | |
print(("X: " .. self.x)) | |
print(("Y: " .. self.y)) | |
print(("W: " .. self.w)) | |
return print(("H: " .. self.h)) | |
end | |
local Player = nil | |
do | |
local o_0_ = {} | |
local mother_0_ = (Entity or nil) | |
o_0_["__index"] = o_0_ | |
if mother_0_ then | |
o_0_["super"] = mother_0_ | |
for k_0_, v_0_ in pairs(mother_0_) do | |
if (1 ~= k_0_:find("__")) then | |
o_0_[k_0_] = v_0_ | |
end | |
end | |
end | |
local function _1_(self_0_, ...) | |
local new_0_ = setmetatable({}, self_0_) | |
new_0_:new(...) | |
return new_0_ | |
end | |
Player = setmetatable(o_0_, {__call = _1_}) | |
end | |
Player.new = function(self, x, y, w, h) | |
assert((nil ~= h), string.format("Missing argument %s on %s:%s", "h", "class-test.fnl", 33)) | |
assert((nil ~= w), string.format("Missing argument %s on %s:%s", "w", "class-test.fnl", 33)) | |
assert((nil ~= y), string.format("Missing argument %s on %s:%s", "y", "class-test.fnl", 33)) | |
assert((nil ~= x), string.format("Missing argument %s on %s:%s", "x", "class-test.fnl", 33)) | |
assert((nil ~= self), string.format("Missing argument %s on %s:%s", "self", "class-test.fnl", 33)) | |
return self.super.new(self, x, y, w, h) | |
end | |
local player = Player(100, 100, 32, 32) | |
return player:draw() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment