This article describes the agent Script Engine, including advanced code writing, low-level code writing, and virtual machine SVM.
Introduction to the agent Script Engine
I. Advanced code writing for the j2_snake script engine:
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, it 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 error when adding brackets.
5. Escape Character Input is supported.
6. Supports comments, such as line comments/And block comments /**/
Ii. Low-level code writing of the j2_snake script engine:
1. The syntax is similar to the 8086 assembly, but it is much simpler than it. There are 33 virtual commands available, you can also define a method to demonstrate a complete addition function and the function call Semicolon is a line annotator ):
- MyAdd ()
- {
- Paramy; function parameter y
- Paramx; function parameter x
- Addx, y; Add the value of y to x and assign it to x
- Mov_RetVal, x; assign the value of x to the register with only one register, I .e. _ RetVal)
- Ret; Method return can be omitted)
- }
- _ Main ()
- {
- Varx; defines the local variable x
- Vary; defines the local variable y
- Varz; defines local variables
- Movx, 2; Assign 2 to x
- Movy, 3; assign 3 to y
- Pushx; Apply x to the stack. To call this method, you need to apply the parameter to the stack.
- Pushy; Press y to stack
- CallmyAdd; call Method
- Movz, _ RetVal; assign the register value to z
- }
I will not provide low-level code for judgment and branch. If you want to know, you can first write high-level code, and then compile the code into low-level code by entering the parameter-A in the compiler.) then refer to the low-level code for research.
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.