Welcome, Guest |
You have to register before you can post on our site.
|
Forum Statistics |
» Members: 60
» Latest member: kirbo
» Forum threads: 106
» Forum posts: 416
Full Statistics
|
Online Users |
There are currently 8 online users. » 1 Member(s) | 7 Guest(s) oliverepikface
|
|
|
AD TIME |
Posted by: Admin - 04-01-2025, 03:49 PM - Forum: Announcements
- No Replies
|
 |
We are proud to announce we are adding ads!
(totally not an april fools joke)
|
|
|
infix custom operator HACK! |
Posted by: DaGreatSkunksEqualToMaccas - 03-31-2025, 07:35 PM - Forum: Lua
- Replies (3)
|
 |
a hack for psuedo-custom operators in lua
Code: -- Custom operator to evaluate (class)
local CustomOp = {}
function CustomOp:__div(b) -- eval full operation.
return getmetatable(self.a)['__' .. self.op](self.a, b)
end
setmetatable(CustomOp, {__call =
function(class, a, op) -- construct left-half of operation.
return setmetatable({a = a, op = op}, CustomOp)
end
})
function enable_custom_ops(mt) -- enable custom ops on metatable.
function mt:__div(op)
return CustomOp(self, op)
end
return mt
end
-- Output stream (class)
ostream = {}
ostream.__index = ostream
enable_custom_ops(ostream)
function ostream:write(s)
io.write(s)
end
ostream['__<<'] = function(self, s) -- '<<' operator
self:write(s)
return self
end
setmetatable(ostream, {__call =
function(class, file) -- construct output stream
file = file or io.output()
return setmetatable({file = file}, ostream)
end
})
cout = ostream()
endl = "\n" -- end of line
-- example usage
local _ = cout /'<<'/ "hello" /'<<'/ endl
|
|
|
|