Turn: Develop your own script engine (2) script syntax Design

Source: Internet
Author: User
Original article: http://blog.csdn.net/GeniusVczh/archive/2006/10/08/1325310.aspx

To be honest, designing a script syntax is a very painful thing. On the one hand, if you want to make your script cool, you will try to add features to it. On the other hand, you don't want to write it yourself. Code It is annoying to delete features. At the end of the day, the script was messy and the brain could not look like it. So you hate to give your brain a reset, forget your script syntax, and try again.
In fact, this is not just a joke. I have personally experienced it. My own script engine, Program I have been writing the Design Syntax for half a year and a half. In fact, most people do not believe it. But in fact, I overturned my three sets of syntax design. At the end of the day, I came up with a syntax that seems to fit together and can be written by the engine.

Start now. What do you want to add to the script? The basic data type is required. But now some scripts are non-typed, and you want to know what the variable is. This script is slow to execute (type information is a very useful information that helps improve the execution efficiency, because it is used to make limited scripts ), however, the current CPU is fast and there is nothing worth exploring. It's just about your hobbies. I am doing a lot of work, and I am doing everything well.
When the data type is well-developed, you are ready to create some "non-basic" data types. What are enumeration, array, structure, class, pointer, reference, function pointer ...... However, it's easy to choose from. You just need to know what your scripts can do. In my own case, there are arrays, classes, and element types that can only be class templates. Classes use reference methods like Delphi for your reference only.
Then the behavior is described. If you want to design something that looks a little like Java, if you need something, for, while, do-while, and switch, function overload overwrite (virtual functions in the class) also needs to be available. If you don't want a class, it's actually not much worse (but it's a lot worse for your next coding ). In short, branches and loops are necessary. If you want to do anything else, you can use them comfortably.
The last part is the interaction with the host Program. Previous Article We can design a declaration format for the function so that the function can be transferred to the host program when called. In fact, there is a benefit of doing this, that is, during the compilation process, the program can use the declaration of this function to participate in Type Analysis, and the script is also nice to write.

Here, let's take a place to blow water. My own jovescript (and later I found it again with an existing one) simply adds a keyword after the function declaration, for example:
Int getrolehp (INT roleid) linking "_ getrolehp ";
When the script calls this function, it will throw the roleid and "_ getrolehp" together. The Host Program judges the functions required by the script based on this string, and then processes the functions. If necessary, the result is returned.
Besides, because I support function overloading, function names will always become messy after compilation, so in order to allow the Host Program to smoothly call a function, I used a similar method to create an alias for the function:
Int getsomeprivatestuff (INT stuffid) Exporting "_ getsomeprivatestuff ";
When the host program calls this function, it does not use the function name, but the alias behind it.
Blow the water to the end.

But here is another reminder. Currently, many scripts have the garbage collection function. Some are semi-collected (similar to the one in Delphi), and some are fully recycled (similar to the one in Java ). Of course, you have another option: Do not recycle. However, when the person who writes the script is not a programmer, there may be a risk of not recycling it.
This function affects the syntax design. A typical example is that the new keyword does not have the delete keyword. In addition, the existence of garbage collection affects the representation of script data in the memory and the Instruction Design. And garbage collection Algorithm Poor writing will seriously affect the execution efficiency of the script. Therefore, we should consider this issue with caution.

The second step is to design the instruction format. In fact, if your VM is executed by observing the Expression Tree, this step can be avoided. However, in a sense, commands provide recursive functions for you.
Before designing the instruction format, you must determine the execution method of the instruction number. You can choose to simulate on your own or directly simulate the CPU. If you want the CPU to run directly, you need to make a just in time, compile the script into a machine code, and then try to let the operating system accept your message and send it to the CPU, you also need to write a debugger to capture the message sent from the script to complete the communication between the script and the Host Program. If this is your idea, you don't need to design the command, that is, the x86 one. If not, you have to come by yourself.
Commands that depend entirely on stacks (except dynamic data, of course expression execution) are the easiest to claim and run, for example, A + B, it can be divided into the following three steps:
1: Push
2: Push B
3: Add // two numbers are displayed from the stack, and the result is pushed back to the stack.
If a temporary register is required, a place for storing the result is required. Therefore, there are two possible splitting methods:
1:
Move result,
Add result, B
2:
Add result, a, B
This type of command can be executed more quickly (depending on how you complete it), but it is a bit difficult to compile. I chose the type completely dependent on the stack when writing.
After the basic format is designed, consider the parameter conventions of function calls. Of course, this can be done at will, but if you need to compile the class, you can make the compilation result of the class more beautiful, it is also convenient to use the data in the stack to communicate with the Host Program.
The last thing to consider is how to exchange data between stacks and data stacks. A common method is to add a "pointer" in the instruction. In fact, there is a pointer to dynamic data, but you can find a way to make the script syntax do not show pointers, use the compiler to convert things that are not pointers into things that are completed using pointers. For example, an array can save a pointer in the stack, just like C. Of course, in general, dynamic arrays should be included in the scope of garbage collection. Therefore, in addition to data pointers, the stored data also has other things. This format can also be used to release multi-dimensional arrays, that is, the array element is also an array. The representation of data in the memory will be highlighted in the virtual machine section. We will also talk about the compiler.

In fact, designing these syntaxes is not a major event. Apart from the ease of use of scripts, if the syntax is too complex, you may not be able to write the entire script engine. Therefore, we should be careful when designing the script, so that we can enrich the script elements and achieve a balance.

In the next article, we will begin to talk about the compiler. People familiar with compilation principles can skip this step, but I personally think the Book Compilation principles is too ugly (before the compiler is completed ), as a result, I won't make too profound theories in my article, or I will not be able to stand it myself. In addition, I position the readers of the next article as persons with certain procedural experience. You can skip compilation principles. That principle doesn't matter. It won't work as a compiler. The only difference is that it doesn't look good.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/GeniusVczh/archive/2006/10/08/1325310.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.