Createing a program that can compile and create a new program?

Started by
8 comments, last by MotionCoil 18 years, 10 months ago
Is it possible to make a program that can compile, and create new programs? Basically, is it possible to make a program where you can write more code, and then the program creates another program out of that new code? (like making a new compiling program) If so, what language and compiler would I have to use? Thanks.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Advertisement
Yes, it is possible. Infact, thats exactly what a compiler is.
You just need to write a compiler -- which can be written with (almost) any language on the planet.
The dragon book should tell you everything you need to know.

I've never read it, but back when I looked into compilers a couple years back, that's what everybody seemed to be touting as THE reference for compilers. Probably outdated or something by now. *sigh*

Oh, it's not really called the dragon book. That's just because it has the pretty red drong on the cover.

----------------------------------------------------------No matter how eloquently you state your argument, the fact remains that the toilet seat is a bistable device. Therefore it's natural position is no more down than it is up.[SDL Smooth Tile Scrolling]
When contemplating languages and compilers it's usefule to understand how machines operate. I found coding a 'virtual machine' to be a very enlightening exercise.

So, lets define a simple vm. We'll have two instructions:

add x, y
bne x, y, z

The first instruction: add x, y take the value in location x and add the number y to it. Let's say the number 8 corresponds to 'add'

The second instruction: bne x, y, z compare the value in memory location x the value in y; if they are not equal then program execution is tranfered to the location in z else execution continues at the following instruction. Let's say the number 9 corresponds to 'bne'

We've implied the existance of a counter to keep track of which instruction we're executing, let's call this the pc (program counter). Since this is my vm I decree that for every add instruction 3 will be added to the pc and for every bne instruction 4 will be added to the pc. I also decree that the pc is stored in memory location zero and at the start of any program it's value is 1. The first instruction of any program starts at memory location 1.

So now we can code a simple program:

add 99, 100
add 100, 1
bne 100, 99, 1

So to store our program in an integer arry we'd have:

memory    address    value:0             11             8, 99, 1004             8, 100, 17             9, 100, 99, 1...99            0100           0


Execution begins at the value in memory address 0 (this is the program counter; pc). We read an 8, we know this is an add so we read the next to memory addresses and increment the pc by 3. We add the value 100 to the value in memory address 99. We have:

memory    address    value:0             41             8, 99, 1004             8, 100, 17             9, 100, 99, 1...99            100100           0


We repeat starting at the pc ie memory address 4 and add 1 to memory address 100;

memory    address    value:0             71             8, 99, 1004             8, 100, 17             9, 100, 99, 1...99            100100           1


Now we fetch the next instruction, its a 9, we know this is bne, so we read the next 3 areas. The value in memory address 100 isn't equal to 100 so we store 1 in the pc. We fetch the next instruction from memory address 1. Repeat. You get the idea I hope because I'm tired of typing ;P

The point of this exercise was to show the idea of what happens behind the scenes. Compilers are programs that output machine code, assembler, or other program code. You could use the little 'assemby' language I wrote above to implement a very basic version of the BASIC language. I found this link to be very helpful concerning compiler construction:

click

Good luck!
------------------<a href="http://jsgc.sourceforge.net>jsgc
if you want to create a compiler the easy way, you should find a simple
compiler that compiles the same language that you are trying to compile.

For example, a C compiler function might look like this (written in C)
int compile(const char* path){    system("gcc %s", path);}

Thanks, I'll have to look more into it.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Do note that building a compiler of any size or complexity is a much bigger task than it may initially seem... I've done it a couple times, and it's very very hard to do it well.

Don't let that discourage you though. :)
-----http://alopex.liLet's Program: http://youtube.com/user/icefox192
Quote:Original post by wasted_druid
That's just because it has the pretty red drong on the cover.


What the crap is a drong? lol

----------------------------------------------------------No matter how eloquently you state your argument, the fact remains that the toilet seat is a bistable device. Therefore it's natural position is no more down than it is up.[SDL Smooth Tile Scrolling]
Andrew W. Appel: Modern Compiler Implementation in C.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I've just started on the compiler for my engine's scripting language. AngelPMX compile environment is in day 3 of its creation and its a slow job i tell you.
What are you hoping to write? a compiler that will compile to your own format or a compiler that will compile to a format that already exists like an exe file or something.
Either way, if its for an engine you will need to write a virtual machine (as someone has already mentioned).

However, as has been said before, writing a compiler is quite a task to get into.

This topic is closed to new replies.

Advertisement