Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

godfuckingdamnit!/prog/

Name: Anonymous 2010-02-06 15:49

hey /prog/

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 ?

Name: Anonymous 2010-02-06 15:57

Secret alien technology

Name: Anonymous 2010-02-06 16:01

>>1
any ideas /prog/ ?
Read the source of any of the 106 Scheme implementations in existence.
is it even possible to write a function that generates a function in C ?
In ANSI C? No.
and how the fuck does one make a struct member with undefined lenght/variable ?
Like this: http://c-faq.com/struct/structhack.html

Name: Anonymous 2010-02-06 16:06

Why would you be writing a Lisp interpreter in C when you don't know anything about Lisp, C or how to program?
If you're writing an interpreter, you don't `generate a function in C', you generate a Lisp function object.
And you store the length of the member in the struct, and either store a pointer to the alloc'ed data, or use a flexible array member.

Name: Anonymous 2010-02-06 16:08

Read SICP.

Name: Anonymous 2010-02-06 16:14

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.

Name: Anonymous 2010-02-06 16:16

>>3

In ANSI C? No.
So C is only for producing program which has a clear logic without being able to change the program itself ? i assume this has to be because of the format of most executable formats , they cant modify themselfs can they and even if you could one would have to make hacks in the paged memmory , right ?

Like this: http://c-faq.com/struct/structhack.html
But is it possible using ONLY C? like without any functions or librarys ? neither can this work because of the memmory model can it ?

im making it REALLY from scratch .
i also try to understand what lisp really is and how it works .

Name: Anonymous 2010-02-06 16:22

>>1,7
Try to understand programming first.

Name: Anonymous 2010-02-06 16:22

>>7
What the fuck are you on?

i also try to understand what lisp really is and how it works .
This is not how you do it. Read SICP. If you still feel like you want to implement a Lisp in C, read K&R and do it.

Name: Anonymous 2010-02-06 16:27

>>3
In ANSI C? No.
Let's say you malloc some memory, copy an assembled (platform-specific), possibly base-independent(or perform relocations if it's not) function, cast that pointer to a function pointer, and call it. Which part makes it non-ANSI C? The casting of the pointer to a function type or the execution of the function code(which contains platform dependent code, and doing such thing is likely undefined by the ANSI C standard) or both?. I have yet to see a real ANSI C implementation where this doesn't work, however it's pretty clear that executing the code at that pointer is undefined by the standard, however I'm not so sure about that cast (it's probably undefined too?).

Name: Anonymous 2010-02-06 16:27

>>6
>>4
i was hopeing to avoid pointers , my idea was to make a struct containing a internal identifier number of specific type for the data and right after it the data itself without it being seperated across the mem .

>>5
>>6

my idea of apply was to store functions as pointers (as it is in C already) and just call it with the args on the stack.

in SICP it is calling either a primitive procedure or evaling a all the elements in the function , again i wanted to avoid this and generate a function just like the primitive ones using lambda but this seems very difficult .

Name: Anonymous 2010-02-06 16:31

Listen, motherfucker. This is way beyond your level. Learn some C before trying to proceed. If you don't, you're going to learn it anyway, but it will take you much longer.

Name: Anonymous 2010-02-06 16:32

>>10
it's probably undefined too?
No, it's just illegal.

Name: Anonymous 2010-02-06 16:39

>>7
So C is only for producing program which has a clear logic without being able to change the program itself ? i assume this has to be because of the format of most executable formats , they cant modify themselfs can they and even if you could one would have to make hacks in the paged memmory , right ?
ANSI C is one thing. The implementation and platform are other things. You can do whatever you want in practice, as long as you can deal with the consequences. Yes, programs can be self-modifying. Assembly code can be self-modifying on some architectures, including x86. Lisp code can be self-modifying to a certain extent (recompiling and replacing functions and data at runtime). C code can be self modifying using platform specific ways (not directly ANSI C). Let's not try to confuse the platform with the language.

i also try to understand what lisp really is and how it works .
You don't really need to write your own Lisp in C, but you're free too. Metacircular implementations or some simple compilers would make it more clear, if you already know Lisp. Doing it in C just means you'll just define the base data types from scratch, this isn't really anything that complex, just read some implementation source code to see how it works. There's also a good book about implementing Scheme interpreters and compilers that might be of interest to you: Lisp in Small Pieces.

>>11
i was hopeing to avoid pointers
Why? Even my most trivial C programs make use of pointers if I want to be able to support non-constrained external data. Lisp objects are so dynamic, that I'd be very surprised if you managed to avoid using pointers. I can only imagine the kind of abuse you'd have to do to avoid pointers. I think it's really not worth going to such lengths to avoid them.
my idea was to make a struct containing a internal identifier number of specific type for the data and right after it the data itself without it being seperated across the mem .
You would still need pointers to operate on that data, even if you avoid using pointers in the actual structure representation.
in SICP it is calling either a primitive procedure or evaling a all the elements in the function , again i wanted to avoid this and generate a function just like the primitive ones using lambda but this seems very difficult .
You want to break a vicious metacircular cicle? Sure, just implement the special operators directly. I'm not so sure I understand the remaining part of your statement.

Name: Anonymous 2010-02-06 16:46

>>14
C code can be self modifying using platform specific ways (not directly ANSI C). Let's not try to confuse the platform with the language.

i was hoping to make it easy portable to other architectures (but i didnt really count with it).

Name: Anonymous 2010-02-06 16:47

>>15
You give the impression that your knowledge of C is not very solid. How about you just make a simple interpreter and avoid making a real native-code compiler.

Name: >>16 2010-02-06 16:51

If you want to make a compiler, take a look at http://www.iro.umontreal.ca/~boucherd/mslug/meetings/20041020/minutes-en.html as an example.

Name: Anonymous 2010-02-06 16:53

>>16
its quite solid i would say, thats why im asking on /prog/ .
i wouldnt ask here if it would not seem impossible and i werent almost a loony because of this.

Name: Anonymous 2010-02-06 16:57

>>18
its quite solid i would say
Your questions indicate otherwise.

Don't sage your own on-topic posts, by the way.

Name: Anonymous 2010-02-06 16:59

>>18
If you insist on making a native code compiler, I would suggest you write it in Lisp. A good majority of Lisp implementations are written in Lisp, especially the compilers, because it's easier to write it in Lisp of course. Even the first LISP compiler was written in LISP. First he wrote a simple interpreter in assembler and then wrote the compiler in LISP while interpreting the compiler in the original interpreter, after that the compiler compiled itself and all was well with the world.

Name: Anonymous 2010-02-06 17:08

>>19
Your questions indicate otherwise.

i know how stupid they are . its just that i have certain goals which make it very difficult . these are :
a) use a minimum of C and a maximum of lisp
b) avoid ASM and arch specific stuff for portabilty and simplicty
c) modularity and simplicity incase i screw up and have to rewrite this shit

and i wont be able to hold any single one of these. thats the reason why i ask stupid shit like this like that i want to generate a function out of lisp code at runtime and copy it into the code section(lol) .

Don't sage your own on-topic posts, by the way

they dont seem on-topic for me .

Name: Anonymous 2010-02-06 17:12

>>21
No. You don't actually know what you are doing. Do you even know whether you're making a compiler or an interpreter?

Name: Anonymous 2010-02-06 17:14

>>21
i forgot to say that: i want to avoid using librarys and external functions .

Name: Anonymous 2010-02-06 17:15

>>21
a) use a minimum of C and a maximum of lisp
write a simple interpreter in C, rest of it in lisp.
You need to implement a simple parser and a special forms/operators (depending if you want to implement Scheme or CL). Look at Lisp500 or one of the many (real or toy) Scheme implementations.
b) avoid ASM and arch specific stuff for portabilty and simplicty
Don't need it in a simple interpreter. Might not be needed even in the case of a bytecode interpreter. If you want to implement GC or OS-specific stuff, or certain optimizations, you might. It all depends on the design choices you make, however if you're writing a pure native compiler (no lisp->c or lisp->bytecode), you will have to write assemblers (and possibly disassemblers too) for the archs you're implementing. There's no way around that as you need to generate native code SOMEHOW.

Name: Anonymous 2010-02-06 17:52

OP here:

thanks guys i really appreciate your input .
as i thought my ideas are mostly crap , im gonna try to implement a simple interpreter and see where it takes me . maybe after that i can use some of my original ideas .

its nice having some of my shit reflected .

good night /prog/

Name: Anonymous 2010-02-06 18:51

write it in lisp you fucking idiot.

Name: Anonymous 2010-02-06 19:04

this thread is very entertaining.
lol, Lisp.
lol, Lispers.
you idiots so crazy.

Name: Anonymous 2010-02-06 19:08

>>27
1/10
Obviously never read his SICP. At least OP seemed to want to further his knowledge since it was incomplete.

Name: Anonymous 2010-02-06 19:29

>>28
-1/0
1. i have read SICP, it was underwhelming and basic.
2. you _are_ OP.
3. Lisp is a joke.

Name: Anonymous 2010-02-06 21:04

>>29
1. No you haven't
2. You are tapeworm
3. You're obviously a joke

Name: Anonymous 2010-02-06 21:14

>>29
I can't be OP as I've already coded some Scheme and CL(subset) interpreters and 2 simple toy compilers.

Name: Anonymous 2010-02-06 23:56

>>20

An endless succession of code

Name: Anonymous 2010-02-07 0:24

>>32
endless CANDY

JFGI

Name: Anonymous 2010-02-07 7:39

im writing a lisp interpreter from scratch in C (or rather trying to)
i was hopeing to avoid pointers
Highly entertaining. Would read again.

Name: Anonymous 2010-02-07 11:03

OP, google "Scheme from Scratch". Peter Michaux was writing up his attempt at making a scheme interpreter in C and finished it a little while ago.

Name: Anonymous 2010-02-07 15:56

>>1
7.5/10

Don't change these.
Name: Email:
Entire Thread Thread List