To teach you how to write script engine (ii)--command script

Source: Internet
Author: User
Tags exit goto integer requires

This time, the simplest form of the script will be implemented. The script has only commands, labels, and jumps, and it looks like a compilation, but it's good to read. Although the syntax of this scripting language is very simple, the most basic elements are still available.

As a scripting engine, the script itself is best not to involve specific areas in order to be used in a variety of appropriate hosting programs. Of course, it doesn't matter if the script is created for the purpose of just one area. Therefore, a scripting engine requires a mechanism for checking and running code, maintenance of the run-time environment, and a plug-in system with sufficient functionality. A complete scripting engine requires at least the following parts:

1, code data structure. The data structure of the code is used to store the parsed script code. In fact, the interpreted scripting engine, which is the scripting engine that analyzes code strings while performing edges, is more difficult and less efficient. The scripting code is analyzed in advance to check for errors that can be checked before it is run. And after we have processed the script code back into a data structure, the execution becomes more manageable.

2, the runtime environment. The Run-time environment is used to store data that the script produces during the run, such as stacks, variables, and state information. For a known code, different runtime environments represent different script execution processes. In order for the script to execute at the same time (but not necessarily concurrently), it is necessary to separate the runtime environment.

3, the parser. The parser is used to convert code into an equivalent code data structure and to output appropriate error messages when a code error is found.

4, Plug-ins. Plug-ins are one way in which scripts interact with the external environment. With the plug-in system, we can add additional functionality unrelated to the scripting engine, such as file manipulation, screen input and output. If necessary, the plug-in system can isolate the scripting engine from the domain information, and the system becomes easier to use.

5, Virtual machine. The virtual machine is used to execute code and return the corresponding result. We interact directly with the virtual machine when we use the scripting engine, and the virtual machine coordinates the collaboration of these 4 components.

Once we know that, we can start to develop a command-based scripting engine. A simple command based language is constructed here to describe the development process and principles in more detail and in a clear way. A language must at least have branches and loops. But for simplicity, we break the branching and looping into judgments and jumps. The language can be added freely, and the label will appear as the target of the jump. This language uses the following syntax:

<: value can be an integer, decimal, String, or name.

< name: Name can be variable name or label, using letters and underscores, followed by indefinite number of letters, underscores and numbers.

<:: The name followed by a colon represents a label. This label represents the position of an instruction to specify a jump target.

Goto < name >:goto is used to jump directly to a location to continue execution.

Set < name > < value >:set is used to assign a value to a variable of a specified name. If this variable does not exist, it is created.

OpCode < name > < value > < value >:opcode can be add, minus, Mul, Div, idiv, or mod. The 6 commands Add, subtract, multiply, divide, divide, and balance the two values and assign the result to a variable of the specified name. If this variable does not exist, it is created.

If < value >[opcode <] Goto < name >:if is used to judge a condition and jump to a specified place when the condition is satisfied. The condition can be a value, the value must be an integer, and the condition is satisfied when the value is not 0. The condition can also be a comparison, at which point the opcode can be is, Is_not, Less_than, Greater_than, less_equal, or greater_equal, respectively, in the first value equal, not equal, less than, greater than, less than, or equal to, Satisfies a condition when it is greater than or equal to the second value.

Exit: End Execution

< name > < value >*: If the command name is not one of the 5 above, then this command will be passed to the plug-in for execution. At this point, the command can have arbitrary parameters.

In this syntax, we can assume that the host program gives us the write, Writeln, and read commands for input and output, and gets a program that determines whether the number entered is prime:

Write "Please enter a number:"

read Number
 if Number less_then 2 goto FAIL
  if Number is 2 goto SUCCESS
 set Divisor 2
LOOP_BEGIN:
 if Number is Divisor goto SUCCESS
 mod Remainder Number Divisor
 if Remainder is 0 goto FAIL
 add Divisor Divisor 1
 goto LOOP_BEGIN
SUCCESS:
 writeln Number "是质数。"
 exit
FAIL:
 writeln Number "不是质数。"

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.