Skip to main content
gamedev.net
🔒 Locked

Scripting Languages and Byte-Codes

Started by adam17 Jul 15, 2008 at 8:37 PM 5 replies 1.8k views
Original Post
adam17
adam17
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
hh10k
hh10k
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.
Hodgman
Hodgman
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 );
adam17
adam17
cool thanks alot!
johnstanp
johnstanp
Some articles on this site:

http://www.gamedev.net/reference/list.asp?categoryid=76
losethos
losethos
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!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.