Jump to content
  • Advertisement
Sign in to follow this  
adam17

Scripting Languages and Byte-Codes

This topic is 3745 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

After playing with different game engines and their scripting, I have been wondering how these scripts are implemented. I have looked around online and have noticed that they all have the same thing in common. The script code is compiled into a byte-code and then executed by the game. My question is how are these byte-codes used by the game? Do they write an interpreter inside the game? is it ran by a complex, jumbled mess of if-then statements? Could anyone point me in a direction to research this? thanks

Share this post


Link to post
Share on other sites
Advertisement
From what I've seen, most (all?) scripting languages execute these byte codes using a large switch statement in a loop. Any decent compiler can optimise this switch statement to an O(1) jump table, so it's very efficient. I would recommend taking a look at the source code for something like Lua for more details. It's use of macros makes some things a little cryptic, but you should be able to get the gist of how things work. Lua also has a wiki and PDFs that explain a lot of how and why they designed the language like they did.

Share this post


Link to post
Share on other sites
It depends on the implementation. You could download the source for a language like Lua and check out it's internals.

A simple method would be to create a loop that fetches the next instruction to be executed, and then performs a switch statement on it's op-code.

e.g.

int instructionPointer = 0;
struct instruction
{
char op_code, param1, param2, param3;
};

instruction compiledProgram[42];

int registers[16];

bool quit = false;
do {
const instruction& current = compiledProgram[instructionPointer++];
switch( current.op_code )
{
case JUMP:
instructionPointer = current.param1;
break;
case ABORT:
quit = true;
break;
case ADD:
registers[current.param1] = registers[current.param2] + registers[current.param3];
break;
case SET:
registers[current.param1] = current.param2;
break;
//TODO - add more instructions...
}
} while( !quit );

Share this post


Link to post
Share on other sites
I get annoyed when people think LoseThos, my operating system, uses byte-codes. It's compiled! The operating system won't work without the compiler. It works just-in-time and gets used all over the place to do tricky stuff. Once you have a compiler, you never need byte-code. All operating systems should have a just-in-time compiler available. I can't believe how much redundancy there is, with batch files, conventional programs and other things all being compiled(parsed at least) by different compilers. People think my start-up scripts are byte code. No! It's just the one and only compiler! Why would I do a different compiler when I already have one!

Years ago, I started with byte codes but it's not that much more work to concatenate the template code from byte code templates together to make it compiled. Optimizing, however, will keep you busy!

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!