Jump to content
  • Advertisement
Sign in to follow this  
Cornstalks

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

This topic is 4891 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 1
1 8, 99, 100
4 8, 100, 1
7 9, 100, 99, 1
...
99 0
100 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 4
1 8, 99, 100
4 8, 100, 1
7 9, 100, 99, 1
...
99 100
100 0




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


memory
address value:
0 7
1 8, 99, 100
4 8, 100, 1
7 9, 100, 99, 1
...
99 100
100 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!

Share this post


Link to post
Share on other sites
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);
}


Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!