r/lisp • u/Imaginary-Deer4185 • 10d ago
Code vs data
Hi, I have been reading up on Lisp for the last couple of days. I come from Java, but have also written a "toy" Forth that compiles to byte code on Arduino.
It seems to me that the memory model of Lisp, if based on CONS cells alone, means a lot of serial traversing. I tried to search for malloc-style memory management in Lisp, and came up mostly empty, with various commentators condemning this approach.
I realize the "code as data" is great for macros, which remind me of immediate words in Forth, but with the added advantage of potentially having the code following it available as a CONS data structure, upon which it can work, as part of the compile process.
Now, compiling Forth is very simple, because Forth is mostly "flat", whereas Lisp is nested, but in the combined interest of speed and readability (where Forth falls short), I wonder if there has been made Lisp implementations with full macro support (on CONS source tree data) but with malloc style data handling, for lookup by offset and memory efficiency?
My Forth targets the atmega328p with 2Kb of SRAM. To a certain extent the stack nature of Forth becomes "bearable" when writing code, but Lisp syntax is much more explicit. I am interested in mapping a Lisp parser/compiler onto the bytecode machine I have defined for my Forth-like language.
View all comments
Show parent comment
2
u/Imaginary-Deer4185 10d ago
My rudimentary idea is creating a compiler (to byte code) for Lisp syntax, without representing code as some CONS tree, but still maintaining Lisp macro functionality to extend the compiler, similarly to "immediate" words in Forth.
With no CONS tree available for macros, a macro would instead configure some kind of pattern matching rules or just a function pointer, to modify s-expressions on the fly when compiling. My current Forth compiler uses the Forth return stack to preserve jump-back addresses and patch-locations for forward jumps, when implementing conditionals and loops in Forth.