technoblogy / ulisp-esp

A version of the Lisp programming language for ESP32-based boards.
MIT License
110 stars 37 forks source link

Please provide an (UNWIND-PROTECT x) function #11

Closed phmarek closed 1 year ago

phmarek commented 5 years ago

I'd like to have my ESP32 available via WIFI all the time. I've got a simple telnet REPL working; the one problem left is that some error (any error) breaks the Lisp REPL and returns to the serial console.

I'd imagine that UNWIND-PROTECT would set some variable to the current execution point (and croak if it's already set, to make it non-reentrable); error and error2 would check that variable, and when it's set store the current error in another variable (that can be queried from Lisp) and instead of doing the longjmp(exception, 1) they should restore the execution point to the saved one.

So my REPL could just run something like

(setf *last-error* nil)
(let ((result (unwind-protect (eval input))))
  (cond
    (*last-error*
      (princ "Error: " client)
      (princ *last-error* client))
    (t
      (princ result))))
technoblogy commented 5 years ago

Sorry for the delay in replying. I agree that unwind-protect would be useful, but the action "restore the execution point to the saved one" is difficult because of the need to restore the state of the stack. I'll give it some more thought, but I suspect it will involve a major rewrite of uLisp.

technoblogy commented 5 years ago

Still thinking about this!

dragoncoder047 commented 2 years ago

Whether it ends up in official uLisp or not, I found that @Goheeca and @m-g-r implemented (unwind-protect) and a few other error-related functions. See these two links:

You'll have to apply the patches yourself. I did on my uLisp, and it works great.

EDIT: I will note here that when I tried to use (error) it caused a Guru Meditation Error (i.e. a segfault), and so I had to replace it with a simpler (throwerrorstring) (my own creation; not a special form which I think was the problem).

technoblogy commented 1 year ago

Now included in uLisp Release 4.4.