I took a shot at writing an unwind-protect macro for newLISP and I have hit the wall. Here is the code (which doesn't work BTW):
Code: Select all
(define-macro (unwind-protect EXPR POSTLUDE)
(letex ($expr EXPR $postlude POSTLUDE)
(let (result nil)
(let (no-throw? (catch (let () $expr) 'result))
$postlude
(if no-throw? result (throw result))))))
Am I missing something in my knowledge of newLISP exception handling or is an unwind-protect not possible in newLISP? Curious.
BTW, I'd like to have an unwind-protect for various reasons; however a frequent use would be via a macro which handles file IO housekeeping, like CL's with-open-file, e.g.
Code: Select all
(with-open-file (in "myfile" "read') bla bla bla ...)
Code: Select all
(define-macro (with-open-file FLIST)
(letex ($fhandle-id (FLIST 0)
$path-file (FLIST 1)
$access-mode (FLIST 2)
$option (FLIST 3)
$body (cons 'begin (args)))
(let ($fhandle-id (open $path-file $access-mode $option))
(unwind-protect
$body
(if (peek $fhandle-id) (close $fhandle-id))))))