Turn: Develop your own script engine (1)

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

In fact, I wanted to write something about script engine development a long time ago. Article . Later, I had to write some Program The plan was suspended. In fact, the reason for writing this article is very simple. I couldn't find the appropriate materials on the Internet when I was developing the script engine, but I made them at last, so let's make a contribution.
At the beginning of the article, I first declare that this article is not an academic paper, not an entry-level document for compilation principles. I only write something that I know. The technology discussed in this article can be used as a reference. In the actual development process, optimization should be made based on the actual situation.
I remember that when I first developed arpg, I was in the first high school. At that time, I was basically in the bird state. In order to support the plot, I figured out a simple command control script. Later, I developed a syntax similar to Pascal. Later, C ++ was used to write another script engine. Of course, this syntax imitates C ++. Later, I learned something during the development of this script engine.

This article is the first article in the entire series. Here I will only talk about a rough idea. The specific implementation details will be described in the next article.

What is the structure of a script engine? We can start with the workflow. Some scripts are required at the beginning. Code (You can write the code in a file), and then read the code with one thing, and the result is sent to another thing for running. When this task is running, you can throw the information to you when you encounter something that you cannot handle. After you finish the task, let it continue to work. Obviously, it is a compiler and a virtual machine. The virtual machine also has a mechanism to communicate with the Host Program.
In this step, we will start to examine the actions required by the two brothers, then Develop interfaces, and then what should we do ...... These are the things of people who read software engineering. I don't care about them now. I just want to implement them. As for how people use it, that's what they do.
Thinking about it, you will find, okay? How does one need a compiler? In fact, the compiler serves as a role for organizing code. If you are dealing with complex scripts (syntactically), you can call it a compiler. In the script engine I developed, the compiler is responsible for compiling the input script code into a set of commands designed by myself, which looks like assembly. Then the Virtual Machine constructs the running environment of the entire command based on these commands and some other data, and finally waits for the user to start.
Suppose you are developing a game, and you need a script interpreter. Then you map some functions in the script to some control of your game. This game has been developed, but you are not addicted yet. The new game still requires a script interpreter. If the last script engine was attached to the game, it would be dead! So many complicated things have to be re-launched. So what we need to do now is to abstract the same things that the script engine does in different applications into one thing first. You can develop some plug-ins for future use.
So now it involves the communication between the script engine and the Host Program. What should I do for this communication? It's actually very simple, just two. The first is to let the Host Program execute a developed script, perhaps a function in the script. The second is when the script requires support from the host program, such as a MessageBox. At this time, the script will tell the script engine that I will give you something, create a MessageBox for me. The script engine itself cannot do it, so I went back and told the host program that I gave the script to you, and you helped me create a MessageBox. The Host Program is okay! So a MessageBox came out.

To facilitate the communication between the script engine and the Host Program, we can add some specific things in the script language syntax, for example, when a function is called, The Host Program can be replaced, or a function can be specified to enable the script engine to conveniently call the Host Program. Then I tried to design a set of commands so that the compiler could write better and the virtual machine could run better. This is what we need to do before we start writing the script engine. After all, designing a script engine is not just about the annoying Algorithm In fact, the script syntax should also be designed by yourself. However, sometimes you can refer to some common Programming Language , And then take the transformation, it is almost OK.

Let's take a look.
To develop a script engine, you must first design the syntax of the script language and the data (commands and other things) required by the virtual machine, and then examine the behaviors required by the compiler and virtual machine, the next step is to design the interface, and finally start the operation. In the next article, I will talk to you about the problems encountered in the Design Syntax and some things to be aware. This is a key part of wet. It has a great impact on your compiler and virtual machines.

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

Related Article

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.