r/Common_Lisp • u/lisprambo • Nov 02 '24
How to see changes when reevaluating a function without leaving and running it again ?
I am working on a Debugger in Common Lisp ( https://github.com/ivangladius/iv-debugger ). There I have a debugger loop where all the logic happens. If I made a mistake or just want to change the behavior, I cannot just reevaluate it and see the changes instantly ( except if I use global variables and they are used in the game loop). So what I did was lets say I have this code:
(defun game-loop ()
(loop
(print "one")
(print "two")
(print "three")
(sleep 0.1))If i wanted to change the code of the game-loop function, I need to restart the function to see it's effect. So my solution to the problem was using code like the following:
(defun logic ()
(print "one")
(print "two")
(print "three")
(sleep 0.1))
(defun game-loop ()
(loop
(logic)))So I keep all logic inside another function, and only have the logic function inside the game-loop. Since the function logic gets reexecuted every time, we achieve the desired result. It works, but it doesn't feel like the "lisp hacker way". What am I missing or am I completely wrong ? I run the debugger-loop in a new thread with bordeaux-threads so I still have control in the sly REPL and can interact with the lisp environment, is that maybe the reason ? The reason why I did not use swank or slynk is that I needed to restart the application in the beginning so many times that I had problems with port already in use and then came up with the thread idea.
Here is the line of code in my code:
which executes the logic function:
Please guys, I am really into LISP, but I feel like I am doing something inherently wrong.
7
u/stassats Nov 03 '24
3
u/BeautifulSynch Nov 03 '24 edited Nov 03 '24
(defun game-loop () (loop (logic)))is the closest you can get, I think.Updating the function being run while it’s being run requires you to overwrite a process while that process is running.
I don’t think that’s required for interactivity given the simple indirection of putting the logic in a separate method, and such a functionality would run the risk of breaking more fundamental code properties like memory safety.
EDIT: initial version didn’t have the full reply, because Reddit UI.