User Manual for the agent Script Engine

Source: Internet
Author: User
Tags svm

Here, I will share with you the usage of the agent Script Engine, including advanced code writing, low-level code writing, and Script virtual machine. I believe this article will surely help you gain some benefits.

About Snake Script Engine

I. Advanced code writing:

1. The syntax is similar to C.

2. Supports function definitions, function calls, arrays, expressions, loops, and branches.

3. The weak lua type is also used. The built-in types include string, int, float, and boolean.
For example, if you define the variable varx, you can assign x = "Hello Snake" to x; x = 32; x = 32.23; x = true;

4. 35 built-in operators, suitable for all expressions, such as bitwise operations> logical operations & Value assignment operations | =. The operator priority is reduced to four levels, from high to low is a single-object operation> arithmetic operation> relational operation> logical operation. If you write code that is not sure about the priority of the operator, there will be no errors when you add brackets.

5. Escape Character Input is supported.

6. Supports comments, such as line comments/And block comments /**/

Ii. Low-level code writing:

1. The syntax is similar to Assembly 8086, but it is much simpler than it. There are 33 virtual commands available for selection and methods can be defined.

Demonstrate a complete addition function and function call Semicolon is a line annotator ):

 
 
  1. MyAdd ()
  2. {
  3. Paramy; function parameter y
  4. Paramx; function parameter x
  5. Addx, y; Add the value of y to x and assign it to x
  6. Mov_RetVal, x; assign the value of x to the register with only one register, I .e. _ RetVal)
  7. Ret; Method return can be omitted)
  8. }
  9. _ Main ()
  10. {
  11. Varx; defines the local variable x
  12. Vary; defines the local variable y
  13. Varz; defines local variables
  14. Movx, 2; Assign 2 to x
  15. Movy, 3; assign 3 to y
  16. Pushx; Apply x to the stack. To call this method, you need to apply the parameter to the stack.
  17. Pushy; Press y to stack
  18. CallmyAdd; call Method
  19. Movz, _ RetVal; assign the register value to z
  20. }

I will not provide low-level code for judgment and branch. If you want to know, you can write high-level code first, and then compile the code into low-level code by entering the parameter-A in the compiler)
Further research with reference to low-level code

Then again, why do we need to design low-level code? It is much more convenient to directly write Advanced Code. The reason for this design is that the compiler compilation jumps to the branch, which is very streamlined and runs very fast, however, compiling expressions is complicated. Although I didn't know the principle of compilation three weeks ago, I wrote the lexical analysis and syntax analysis of scripts, compilation and other programs I discovered that it is more difficult and complex than I imagined several times. Currently, the most popular compilers such as C ++ compilers use state machines for syntax analysis, I have optimized the compilation of expressions, but I am a beginner. To speed up R & D, I used the semi-state machine semi-recursive descent method for compilation, therefore, there is not much Optimization in efficiency. If you want to write code that evaluates complex expressions and needs to run quickly, you are advised to write it in low-level code and merge it with advanced code, it is compiled into binary by assembler.

Iii. Script vm svm)

1. The execution of a virtual machine is based on bytecode and only one register _ RetVal is used to store the return value. The runtime stack is used to call a function. The default value is 512 KB, you can modify the stack size by yourself, for example, adding the keyword SetStackSize1024 to the header of the low-level code.

2. Communication with the main game engine is based on method calls. You need to write the keystore library class by yourself. This class must inherit Lib. class, which is implemented in the worker library. Considering the efficiency, objects are not used without communication, and it is not based on the RTTI mechanism. Therefore, pay attention to the following points when using the class, these two points are written in the test package. You can check them.

3. error handling is also because the efficiency is not based on java exceptions, but only prints the wrong print and the virtual opportunity runs normally. However, once an error is printed, you need to stop the program and check whether the script is running, or is there any BUG in the core Execution Unit of the virtual machine? If this is not done, the subsequent script data will become dirty data, which may cause potential risks to the game logic.

4. multithreading: the script engine can load multiple scripts for concurrent execution, and each thread has a priority. scripts with a higher priority can be assigned more time slices. SVM can manage the running of these threads, in addition, the main game engine can conveniently operate Script virtual machines to load, pause, stop, and resume these script threads. Version 0.8 provides multi-thread functions)

5. Running Mode: In the while loop, the main game engine calls the runScriptinttime method of the Virtual Machine each time. It passes in the time slice to determine how long the virtual machine runs in this cycle, so you can write this loop whiletrue in the script) that is, the script has its own logic endless loop, but it does not interfere with the logic endless loop of the main engine, because when the input time slice is running, the script engine automatically jumps out to allow the main engine to continue running the next loop, which is transparent to programmers.


 

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.