4 ms·
In TXR Lisp, I did the grunt work and wrote separate constructor functions for different function signatures: http://www.kylheku.com/cgit/txr/tree/lib.c#n5525
by kazinator 9y ago
In TXR Lisp, I did the grunt work and wrote separate constructor functions for different function signatures:
http://www.kylheku.com/cgit/txr/tree/lib.c#n5525 http://www.kylheku.com/cgit/txr/tree/lib.c#n5525
These functions are only used from within C code; they are not the basis for any intrinsic functions. The call to each of these functions knows exactly the type of function it wants to make.
There is a notation in the naming.
Firstly, the leading 'f' denotes functions with an environment value, whereas 'n' denotes non-environment functions. Then there is a number indicating the number of arguments. Then an optional letter which is 'o' if the function has optional arguments, or 'v' if it is variadic. Both can be present in which case we have 'ov.
Thus for example func_n2ov(some_c_function, 1) will create a variadic function with 2 fixed arguments, of which 1 is required (so one optional). some_c_function has to have the type signature val (* )(val, val, varg). No casting is required because func_n2ov just assigns this to the correct union member. If we pass a function with an incorrect signature, we get a C compile error.
The func_n2ov constructor is used when registering the unique function in eval.c:
reg_fun(intern(lit("unique"), user_package), func_n2ov(unique, 1));
We intern the string "unique" in the user package ("usr"), and use that symbol to register a function createdy by hoisting the C function unique into the Lisp domain with func_n2ov.
I wrote TXR in a way that is easy to understand and maintain. It is only implemented in C, though. If you want to study how to make a Lisp interpreter in C. Yet, it is production code for real work.