unwind-protect for newLISP
Posted: Fri Apr 13, 2007 2:46 pm
				
				Hello everyone!
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):
I thought that I could use the return value from catch to sense if EXPR threw an exception, which I would then need to throw up the stack (after running POSTLUDE).  But it seems as if I can't control the exception  handling to this degree.
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.
where the file "myfile" gets automatically closed, even when a non-local exit is triggered in the blas.  (The symbol in would hold the file handle for  "myfile".)  And of course, my definition of with-open-file relies on unwind-protect.
Thanks for any help.  --Ricky
			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))))))