Quote:Original post by sathenzar
thank you, any other suggustions are welcome, and yes, Saurman I already know how to compile/generate an EXE thorugh VC++ or another compiler. I'm talking about thorugh actual code you can write in C++. I always made me admire people that made compilers b/c I can only imagine what goes into them.
Any other help is welcome. Thanks again.
C is actually quite simple, to the point where you could write yourself a really simple compiler for a subset of C pretty quick. (for a vm, of cource. You don't EVER want to compile to machine code.... its bloody hard compared to the compiling part).
First, remove whitespace.
Second turn it into a massive array.
Third turn it into a tree.
Forth, you start compiling, turning simple things like I++ into inc i, as well as doing expression parsing (which is hard)
Fifth, you compile the variables, adding the variable loads/stores, depending on vm.
sixth, you do things like loops, if's, ect.
seventh, function calls
eighth, you compile to vm machine code from the asm you made.
ninth, you can do some keyhole optmiisations. (like div eax, 2 == lshift x, 1)
Thats about it.
For eg.
int main() {if(x == 3) { y = 7}}
First remove whitespace.
int main() {if(x == 3) {y = 7}}
Turn into a tree
Main cmp x, 3 jneq 2 Set y, 7 exit
Change variables to registers
Main cmp eax, 3 jneq 2 Set ebx, 7 exit
Function calls replaced by adresses. (cause you know exactly how big each function is, you can give them apsolute adresses)
Now, you go from tree to asm.
cmp eax, 3jneq 2Set ebx, 7exit
And from there you compile to asm...
From,
Nice coder