3 ms·
C is an interesting contrast. There is no "C runtime". There is just the target machine. A C interpreter in C has no advantage over one in any other language, b
by ncmncm 8y ago
C is an interesting contrast. There is no "C runtime". There is just the target machine. A C interpreter in C has no advantage over one in any other language, because every detail of the source language of the interpreter is washed away in compilation, along with the compiler.
In Lisp, everything is still there. "Resolves a symbol" relies on the runtime. "Calls apply" relies on the runtime. You don't need to write those, because you inherited them.
- kazinator 8y agoIn fact, in a hosted conforming implementation of ISO C, there is a considerable run-time, but it doesn't have anything useful in it for building a low-effort C interpreter. apply is just a function in Lisp. The host Lisp itself doesn't (necessarily) use apply for calling functions. When an expression like (cons a b) in the host Lisp is processed, it's quite likely compiled into machine code or byte code that doesn't use apply. The hosted interpreter uses apply because it operates at the host Lisp's run-time; with respect to that host, it's handling function calls by dynamically constructing lists of evaluated arguments and then having to solve the problem of applying a list of arguments to a function. A C run-time could provide apply. Libraries exist for this like GNU ffcall, and libffi. They are low-level, hard to use and error-prone compared to apply. A C run-time can provide a way to get the addresses of functions also; we can do this in modern POSIX environments with dlsym, in Win32 with GetProcAddress and such. If we put together libffi and dlsym to call a function, and make the slightest mistake, we have corruption or a crash on our hands. Whereas apply is robust; we get diagnostics about too many or too few arguments and so on. If ISO C provided these features, then we could have ISO C "C-in-C" meta-circular interpreters. They would contain a significant lines-of-code expenditure compared to Lisp-in-Lisp.