Killoso32
Killoso32

Reputation: 71

How do I use the live-code feature in sbcl?

I am trying to get live-coding to work in lisp. i have the file t.cl which contains only this line: (loop(write(- 2 1))). Now, when i run the file in bash with sbcl --load t.cl --eval '(quit)', it runs the line, but when I try to edit the file in another terminal and save it while it runs, nothing changes ..

Upvotes: 3

Views: 417

Answers (1)

Thomas Houllier
Thomas Houllier

Reputation: 346

Why your example fails

When running sbcl --load t.cl --eval '(quit)' in a shell, what this does is spin-up a SBCL Lisp image in a process, compile the file and run it. You then modify the file and save it to your disk. This last action is of no concern to the already running SBCL process, which has already compiled the previous file. SBCL read the file once when you asked it to, once it has the compiled instructions to run, it has no reason to look at the file again unless you explicitly ask it to.

A 'live' example with Emacs+SLIME

In order to perform 'live' changes to your running program, you must interact with the already running Lisp image. This is easily doable with Emacs+Slime. You can, for example, have a loop like so:

(defun foo (x) (+ x 3))

(dotimes (it 20)
  (format t "~A~%" (foo it))
  (sleep 1))

and then recompile foo during execution within the REPL with a new definition:

(defun foo (x) (+ x 100))

Another thread will be used to recompile the function. The new function will be used for future calls as soon as its compilation is finished. The output in the REPL will look like:

3
4
5
CL-USER> (defun foo (x) (+ x 100))
WARNING: redefining COMMON-LISP-USER::FOO in DEFUN
FOO
103
104
105
...

This would also work with the new definition of foo being compiled from another file as opposed to being entered directly in the REPL.

Working from the system shell

While you can already use the example above for development purposes, you might want to interact with a running SBCL Lisp image from the shell. I am not aware of how to do that. For your exact example, you want to get SBCL to reload eventual files that you have modified. A brief look at the SBCL manual doesn't seem to provide ways to pipe lisp code to an already running SBCL process.

Upvotes: 4

Related Questions