im writing a lisp interpreter from scratch in C (or rather trying to) and i dont know how to represent a general lisp object . i also dont know how to implement ˝lambda˝ and ˝apply˝ , or rather a function which generates a function and returns its address.
any ideas /prog/ ?
is it even possible to write a function that generates a function in C ?
and how the fuck does one make a struct member with undefined lenght/variable ?
i dont know how to represent a general lisp object
The simplest (and maybe efficient when done right) are tagged unions. For example you could reserve a byte for various base types, and read that byte in to find out exactly what object it is and how to represent it.
Something like:
char tag;
void *obj;
However, there is no real need to use a pointer for obj and the data could be continous. There's also no need to use a fixed size tag, some high-performance implementations use variable-length tags, 2 bits for integers, and increase the length the less important is high-speed access.
Of course, an inefficient implementation can also work.
If you'd like to see a Greenspunned lisp(C-based), look into Lisp500 or one of its forks. If you really want to see a real-world based CL, look at CLISP, although it's interpreted. You may also want to look at SBCL and CMUCL, but they're almost entirely written in Lisp, except for loader and gc being written in C. i also dont know how to implement ˝lambda˝ and ˝apply˝
What's so special about lambda? It's just a piece of code. You make a lisp object which is a code object. This code object may have a size and some other properties (maybe relocs if you're compiling to native code and you want to move it around in memory?). What the code is depends on what kind of Lisp you're making(Native compiled(how? actual compiler or lisp->c or related?) or Interpreted(just conses and atoms or compiled to bytecode)). You'll have to make a lot of design decisions which will shape your Lisp before you actually are able to create it.
What's so special about apply? It's just a function which calls a piece of code with some args, while the last arg is a list of the rest of the arguments. To write it, you of course need to know what kind of Lisp you'll write, but in the case of a native or bytecode interpreted one with a stack-based calling convention, you would just push each element in the list to the stack and then push the rest of the arguments, and then proceed to call the actual function. If you're familiar with processor architectures and general compiler technology (as well as with Lisp), this shouldn't be too hard. or rather a function which generates a function and returns its address.
What is a function? It's just a Lisp code object. You would alloc the memory for it and then assemble the rest of the object. The internal representation depends on what kind of Lisp you're designing. For all I know, in an ancient Lisp, such a code object might as well be plain old conses containing the quoted source structure (which is pretty printable of course). is it even possible to write a function that generates a function in C ?
Yes, you can also do it in most other languages, but it depends on what kind of function. If it's native code, you would alloc memory and assemble the code there, and then when you need, you would call it. and how the fuck does one make a struct member with undefined lenght/variable ?
Pointers. Or if you're really unsure about what it is, void*.
Read SICP or PAIP. Read a good book on computer architecture and maybe something on compiler construction.