Tag Archives: Script

Script machine

What is a script machine? why do we use that? which script machine is the best? and there are hundreds of questions about script machines.

Script machines can be a part of computer programs or game engines. It can load and execute external code after the program is compiled. these codes can be a series of commands or C-like codes. It would be beneficial for us because we can change some parts of the application by simply changing the script file even after the program is compiled and published. We can drive the gameplay in a game engine by writing script code without recompilation. Additionally, script commands can be simpler than a programming language and can speed up development.

Most script machines include a lexer, parser, syntax tree, semantic checker, command generator or byte code generator, and a virtual machine. you can use prepared lexer and parser libraries like BISON or COCO or any other here or write your lexer and parser ( just like me 😉 ). After that, you could parse the source code and create two important components in the next step. The first component is the Symbol Table and the second is The Syntax Tree. The symbol table works just like a container that holds any symbol used in your script. All constant and variable strings, numbers, functions, and classes will be stored in this container. By Syntax Tree you can store and represent the script structure. also, this tree will help you to control command order and priority which is very important in math calculations. Don’t worry about these steps because the prepared lexer and parsers library will help you create these components 🙂

It’s usual to generate a list of ByteCodes from the script in the next step. These ByteCodes are closely similar to assembly language and can be saved in a file to be used later by a virtual machine. They will be translated by a virtual machine that simulates the CPU. The advantage of using ByteCodes is that there are more controls over generation time. It would be easier to implement OOP and other new features in common languages. Also, you can find common ways to optimize the generated ByteCodes, etc. Here you can find more things about script engines.

The other way of compiling and running script codes is to generate a list of pointers to function instead of ByteCodes. In this approach, you can create a list of commands in the compiling step and just call command functions on running. Each command contains a pointer to a function and arguments. Some programmers try to build their script machine and would like to implement “pointer to function” in designing. Some of them believe that using “pointer to function” instead of “ByteCode” can obtain the fastest script even as fast as C++ language !!!

Well, I can’t accept that because the most important difference between compiling machine language and script commands is CPU features like CPU Caches, CPU Optimization, etc. The caches in the CPU are responsible for bringing functions and data beside the CPU and increasing access time to them for the CPU significantly. Using any list of commands or virtual machines could lead to the discarding of these features of modern CPUs.