fft1976 said:
Reinventing LISP in C is not how C is meant to be used, is it?
but, it is very useful for some tasks, and can be used along with good old
traditional C practices...
Before some LISP code monkey quotes Greenspun, tell me, does Linux
reinvent LISP, or is Linux insufficiently complex? What about Git?
Linux does not, but Linux does not need to for what it does (as an OS...).
By the way, closures are not runtime-generated code!
runtime code generation, however, is one way to get closures in C...
this is actually because of a subtle issue in how C is usually implemented:
a function pointer is just a raw address to some code, so to call it, more
or less the call-frame is set up, and control is transferred to this
address...
often, there is no other context for this function available.
so, one can generate a new piece of code, which itself knows where the
captured state is, and so can pass it on to the closed-over function (in the
implementation, for example, as a hidden argument...).
the actual underlying mechanisms, however, are far more general and powerful
than something just "getting LISP implemented in C...".
FWIW, I will note that my compiler represents its AST's in XML, and not
S-Exps... (the reason is that XML is actually more flexible and adaptable...
even if heavier weight and more effort to work with...).
this may well be part of why XML eats S-Exps' cake, as for all their merit
S-Exps would seem to have a notable flaw for general-purpose data
processing: in non-trivial cases they are about as flexible as if the
structure were made of glass... (in XML you can almost always add a new
attribute or namespace to fudge around whatever comes up, but S-Exps tend to
provide no real way to "bend" the structure with new data, only the
combination of so many 'forms'...).
so, one can't usually bend the forms, and instead adaptations may tend to
require fundamental alterations to the structure, ...
although, sadly, XML does have its performance costs... and, yes, I still do
use S-Exps in more than a few places...