Analysis on the usage of the agent Script Engine (1)

Source: Internet
Author: User

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 ):

 
 
  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 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.


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.