[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Auto-closing with loops
- From: Kenneth Lorber <keni@...>
- Date: Fri, 20 Jul 2018 09:03:50 -0400
I'm going to stick my neck out and propose that we can attack this from another angle. (And I will admit from the start that this is not backed up by either code or a deep understanding of the mechanisms I'm proposing to leverage - someone else can make this work or shoot it down.)
We already have a mechanism that catches all exit paths from a function - xpcall(). And it has a way to run code - the message handler.
So what happens if we build on "do..end" to create "via..do..end" (I don't like "with" and this note might be clearer if I don't use "with") that is (mostly?) syntactic sugar that transforms
via FINALIZER do CHUNK end
into something like
local chunk = compile CHUNK replacing return... with error( {MAGIC=_ENV,RETURN={...}} ) and error(e,lvl) with error({MAGIC=_ENV,ERROR={e,lvl})
local finalizer = function(_ENV) FINALIZER end
local fwrapper = function(e)
finalizer(e.MAGIC)
if e.ERROR then error(e.ERROR.e, e.ERROR.lvl) else return e.RETURN end -- unscrambling e.RETURN to .. left as an exercise for the reader
end
local status, e = xpcall(CHUNK, fwrapper)
if status then error(e) else return e end
Does this approach offer anything?