WML script is part of the unlimited application protocol WAP application layer. It can be used to add client processing logic to WML card groups and cards. The latest version is version 1.1. WML script1.1 is specified based on the ecmascript script language developed by the European computer manufacturer protocol and modified and optimized. It can better support instant Bandwidth communication devices such as mobile phones. Using WML script in WML programming can effectively enhance the flexibility of client applications, we can also use WML script as a tool to develop powerful WAP network applications and unlimited web pages. In this chapter, we will explain in detail the basic pre-Laws of WML script1.1 programming, such as basic rules, variable pre-data types, and operation-authorized expressions. To simplify the description, we will refer to "WML script1.1" or "WMLScript" in the future ".
The WML script function is called in the 4.1 WML program.
After the first two chapters, readers who are familiar with the C language may realize that WML functions and logical operation functions are very limited. WMLScript provides rich function functions. We can use WMLScript to enhance WML programming in WAP application development. Therefore, WMLScript becomes the main development tool to expand WML programming capabilities.
4.2 main advantages of WMLScript and Its bytecode Interpreter
WMLScript has a set of defined bytecode and a interpreter reference structure. In wireless network transmission, WMLScript data is transmitted in binary format. Therefore, you can use a bandwidth communication channel to ensure that the client mobile phone only needs the minimum memory. After ecmascript is modified, the WMLScript can compile programs in bytecode format faster, smaller, and easier. All of these features are that WMLScript has many advantages and functions that WML cannot possess.
4.2.1 main advantages of WMLScript
WMLScript is designed to provide general script processing capabilities for WMLScript systems. With WMLScript, we can further supplement the programming functions of the XML-based WML language and develop network applications and content for bandwidth, for example, text, images, and selection lists, we can use simple formats to write more flexible and readable user interfaces. WMLScript WML does not have the following advantages and functions: (1) Check the legality of user input:
(2) Extend the user's browser functions, such as allowing programmers to develop mobile phone calls, send short messages, store phone numbers, manage phone books or SIM cards;
(3) generate confirmation, prompt, warning information or operation dialog box on the user side, and display it on the browser quickly;
(4) The software and parameters of the browser can be expanded and configured after the browser is changed;
(5) Overcome the upper limit of the client's bandwidth communication connection to the greatest extent, and provide a wide range of program functions;
(6) supplement WML and enable it to provide a variety of services for micro-mobile terminal devices, such as supporting advanced user interfaces, adding Client Intelligence, and providing access capabilities for user browser peripheral functions, browsing and transmitting data on servers and clients reduces bandwidth usage.
4.2.2 WMLScript bytecode Interpreter
Before the WMLScript bytecode interpreter is interpreted, the text format program written in the WMLScript language is first compiled into binary code. During compilation, the compiler usually divides the WMLScript program into several editing units. Each unit contains a certain number of statement rows and WMLScript functions, based on these compilation units, the WMLScript compiler uses the WMLScript program as the input content and the corresponding bytecode as the output content. When a user calls the WMLScript program through a WAP mobile phone, the encoding function of the compiler is activated and executed.
4.3 basic WMLScript rules
WMLScript follows WML practices in many basic rules. However, because WMLScript is specified based on the C language, its syntax features are very similar to that of the C language. If you are familiar with the C language, it is easier to learn and master this part.
4.3.1 WMLScript and URL
Like WML, WMLScript also follows the URL and HTTP specifications for WWW and HTML resource access, and expands the scope of URL usage. In WMLScript, not only hyperlinks, file paths, that is, file names can be processed as URLs, but also external functions and access control information can be processed as URLs.
Therefore, WMLScript adopts WML workarounds to improve the way of naming resources as values in HTML, and uses fragment anchor to process resource location. The program segment anchor is defined according to the URL rules of the document and written in the format of # before the program segment identifier. Using the program segment anchor, the WMLScript program can specify any function in the unit of WMLScript compilation, and can pass the required parameters while calling the function.
4.3.2 lexical structure
The lexical structure in WMLScript programming is not complex. The following describes the relevant rules in terms of case sensitivity, spaces, line breaks, comments, and reserved words.
(1) content type. The content type of WMLScript is mainly applicable to the file format and binary format. The type structure can be specified on the server side. The specific format is:
Text Format: text/vnd. WAP. WMLScript;
Binary format: Application/vnd. WAP. wmlscriptc.
We have already introduced specific methods in chapter 4th and will not repeat them here.
(2) case sensitive. Wmlscript1.1 is a case-sensitive scripting language. All the keywords, variables, and function names it designs must be case sensitive.
(3) space and line feed. Generally, all spaces and tabulation fields are ignored in the form of WMLScript values. However, if these special characters are expressed in code or processed as strings, WMLScript will not ignore them. For example, if the string "Oct 28,2001" contains spaces, the space will not be ignored during execution. It is different from the string "oct28, 2001" without spaces.
(4) annotations. Like WML programming, you can add comments to the WMLScript script program. The comment content is not executed by the program, and the comment cannot be nested. There are two annotation methods for WMLScript:
First, line comment. That is, the Double Oblique Line number (//) is used to guide the content of a line, and all the content of this line is used as the comment content. For example:
// This is a line comment, which starts from the double slash and ends with a comment.
Second, select a block. It starts with the symbol "/*" and ends with the symbol. For example:
/* This is a block comment, and the content added in the middle is the comment content */
(5) data type and direct encoding. WMLScript allows four types of data to be directly encoded, nested, and embedded in a program. In directly encoded 4, the data types are integers, floating-point numbers, strings, and boolean values. In addition, the "invalid" value can also be directly encoded "1. integer. When integers are used in decimal, hexadecimal, or octal notation, these integers can be directly encoded.
During program compilation, all decimal numbers do not start with 0 and only contain 0 ~ A numeric string of 9. The hexadecimal data starts with ox or ox and only contains 0 ~ 9. ~ F or ~ F String; the number of octal segments starts with 0 and only contains 0 ~ A numeric string of 7.
2. Floating Point Number. Floating Point Numbers are generally defined as numbers containing decimal points. They can contain decimal points and exponent points. There are many floating point numbers, which can be decimal integers or floating point numbers, which can be scores or indexes. But a floating point number must have at least one number.
The index starts with E or E, followed by an integer. The base power of an index is 10. For example: E0 10 zero power, for example: E0 10 another power, E-2 10 negative 2 power set is equal to 0.01. An index can be signed, namely (+) or minus (-). They represent positive and negative indexes respectively.
3. String. A string is defined between pair double quotation marks ("") or single quotation marks.
Since WMLScript only allows pair double quotation marks or quotes to define strings, a compilation error occurs when a single or one double quotation mark is used in the program.
Given that some special characters cannot be directly displayed in strings, WMLScript provides a translation sequence to represent these special characters.
4. boolean type. It is only a true or false value, used to represent the "true" or "false" value in WMLScript ". Boolean data can be involved in operations such as XOR, or. The specific rules are described later.
5. invalid type. It is also known as "null type". It is a volume supported by WMLScript to indicate invalid values, expressed as invalid. This is similar to null in C.
4. Reserved Words. A reserved word set is defined in WMLScript. Words Containing special meanings cannot be defined separately or be the most unique identifier. The reserved words in WMLScript are as follows:
Acces HTTP agent if break isvalid continue meta Header
Div name DIV = path domain return else typeof while
Equiv URL extern use for User Function VaR
In addition, WMLScript reserves reserved words for future versions, mainly including:
Case finally catch import class private const public Debugger
Sizeof default struct do super Enum switch export throw
Extends try
WMLScript has some unused reserved words:
Delete null in this lib void new
7. identifier. WMLScript identifiers can specify or name three elements: variables, functions, and annotations. The identifier cannot start with a number, but can start with an underscore (-). Moreover, the identifier cannot be a reserved word of WMLScript. For example, timeofday, speed, quality, home_address, _ myname, _, Varo, and so on are all valid identifiers, and strings starting with numbers or special non-short underscores, and Reserved Words are invalid tokens, such as while, for, if, and my ~ Name, $ sys, 123, 3 piecs, and take. This.
Because WMLScript is case-sensitive, the identifiers with the same letters but different cases are not the same. For example, work and work are different identifiers.
8. namespace. WMLScript provides a free namespace. The same identifier can be used for different purposes at the same time. For example, an identifier of a function name can also be used as a variable name, function parameter, or program annotation. Their attributes or values do not affect each other during use. In the following simple routine, the identifier mytest is used as the function name and variable name, function parameter name, function parameter name, and constant name. Obviously, this feature of WMLScript provides great convenience for programming.
4.3.3 basic writing rules of the WMLScript Program
Basic book writing rules of the WMLScript program:
1. A program is composed of several statements or functions. A function consists of several statements;
2. Each complete statement must be followed by a semicolon (;), and space must be added between the operands of the key words of the statement. A semicolon (;) is a component of the WMLScript program;
3. A pair of curly braces ({}) must be used between the function bodies, and a semicolon (;) must be added after the right curly braces (}) at the end of the function; function description, for example, the function name, function type, and function parameters must be placed before the braces;
4. Some statements may also need to use curly braces ({}) for content. Such statements can also be placed in functions, so curly braces ({}) can be nested.
Of course, there may be more detailed requirements for different statements, parameters, variables, and other elements in the declaration and writing. The specific requirements are described later,
4.4 variables and Data Types
Variables, that is, data types, are the concepts and components of all programming languages. WMLScript is no exception to this. It provides detailed provisions on its variable usage and data type definition methods. A variable usually corresponds to a certain data. We can assign values to the variable and change the value of the variable during program execution. The following describes the WMLScript variables and data types.
4.4.1 variables and their declarations
A variable is a symbolic name or identifier that has a value in a WMLScript program. You can use variables to store and change the data required by the program. Different from the C language, WMLScript only supports variables defined in functions or parameters used to transmit numbers.
Before using the variable, you must declare it, that is, define the variable, that is, specify the name of the variable. The keyword for declaring a variable is var. The root of the variable is the legal identifier of the variable name, and a semicolon (;) is added at the end to complete the declaration of a volume. When declaring a variable, you can use VaR to declare multiple variable names at a time, and use the "," interval between adjacent variables.
Generally, we want to use meaningful variable names when naming variables. For example, when you need to use a variable to represent the price of a book, although it is not wrong to name the variable J or book, however, if the name can be bookprice, the WMLScript program has better readability and can be used by programmers to compile and debug the script. In addition, because WMLScript cannot use reserved words for variable naming, the variable name may not conflict with reserved words due to temporary negligence, we recommend that you use multiple words in combination as the name of a variable. This is a better solution. For example, if you want to define a variable to store the price of a book, you can use bookprice or book_price as the variable name. In this way, the variable can be clearer, on the other hand, it can avoid conflicts between variables and reserved words.
The above are just our suggestions for variable naming, and they are not mandatory. Users can simply follow the WMLScript's requirements for identifier naming, but developing a good programming style is very meaningful for programmers and programmers.
4.4.2 scope and life cycle of Variables
The scope of a variable refers to a piece of code that can reference this variable in a program. Since WMLScript only supports variables defined in functions, the function of WMLScript variables is usually the function that defines them. In addition to this function, variables no longer play a direct role.
The life start value of the variable starts from the declaration of the variable to the expiration date. The life cycle of a variable is also called the persistence period and survival period of the variable. A variable is valid within the entire function that defines it. Any statement block in the function does not reduce the life cycle of the variable or limit the scope of the variable.
If a variable is directly used without life, or declared again, the life cycle of the variable will be destroyed. In the previous case, the variable does not start the declaration period, that is, there is no "life". In the latter case, the variable declaration period is re-granted before the declaration period ends, that is, let it be "Born" multiple times ". This will cause invalid use of variables. The usage of the variables in the following functions illustrates this problem:
Function Foo (){
X = 1; // error: the variable is not declared before use, and the change volume is not "declared ".
VaR x, y, z;
Y = x + 3;
VaR ZD = invalid
If (x ){
VaR (y); // error: this variable has been declared. Here it is repeated declaration.
};
};
4.4.3 use of Variables
WMLScript variables can only be used in the function that defines them. Variables must be declared during use. Declared variables can assign values to variables at the same time, or even perform operations on variables. For example, the following simple function illustrates the flexible usage of variables:
Function ourage (){
VaR myage = 38;
VaR yourage = 26;
VaR ourage = myage + yourage;
Return ourage;
};
When using a variable, you can call the variable name. In the above example, the "Var ourage = myage + yourage;" clause calls the variable name. The variable ourage sums the variable myage and the variable yourage.
4.4.4 variable type and Data Type
WMLScript is a language of "weak type" and Its variables are not of a definite type. The type of the WMLScript variable depends on the type of the Data assigned by the change volume, and changes according to the change of the data type. WMLScript only supports internally defined data. Therefore, you do not need to specify the type of the WMLScript variable for programming. WMLScript automatically matches the Data Type assigned by the variable according to the variable. The Data Types of WMLScript include integer, floating point number, String, Boolean, and invalid. Therefore, the types of WMLScript variables can match these five types.
4.4.5 variable value range
Because the variable type, especially the type of the assigned Data, is determined, the variable value is equivalent to the value range of the Data assigned by the field. The following section describes the range of integer, floating point number, string, and boolean values to determine the value range of the expected variable. 1. Integer Range. WMLScript supports 32-bit integers, that is, the range of integer values ranges from 2147483648 to + 2147483647. We can use the Lang function to obtain these values during the runtime, such:
Lang. maxint (); // obtain the largest integer
Lang. maxint (); // obtain the smallest integer
2. Floating Point Range. It refers to the minimum and maximum values that can be expressed by WMLScript floating point numbers. WMLScript supports 32 single-precision floating point numbers. The maximum value is 3.40282127e + 38, the smallest non-zero number is 1.17549435e-38 or smaller (based on normal precision ).
We can use the floating-point float function library to get these values during the program running:
Float. maxfloa (); // obtain the maximum floating point value supported by WMLScript
Float. maxfloa (); // obtain the minimum floating point value supported by WMLScript
For some special floating point numbers that appear during the runtime, WMLScript will follow the rules below:
First, if the operation result is a value that cannot be expressed by a single precision floating point number, the result will be considered as invalid, that is, the invalid value;
Second, if the operation result overflows, the result will be processed as 0.0:
Third, the negative zero and positive zero are completely equal.
3. String range. Any symbol string consisting of letters, numbers, or special characters is a string operation defined in WMLScript or a function control string in the string library.
4. boolean data range. Boolean data only has two values: True and flase, which are two types of values of Boolean variables. We can use boolean data to take the value of an initialization or specified variable, or write a Boolean variable into a statement that requires a Boolean value as a parameter. A boolean value can be the result of a numeric operation or a logical operation.
The following example defines a Boolean variable and assigns an initial value:
VaR truth = truth;
VaR lie =! Truth;
4.5 operators and expressions
In WMLScript, expressions can combine variables, constants, and operators to produce certain computation results. The result produced after expression operation can be integer, floating-point, string, or boolean data. In fact, we are no stranger to expressions. For example, 1 + 2 is a simple expression.
There are two types of WMLScript expressions. One is a value expression, that is, an expression that assigns data to a variable, for example, mybook = 3. In this expression, 3 is assigned to the variable mybook, this expression also has an operation result, namely 3. The other is an operation expression that generates an operation result without assigning values. For example, 1 + 2 is an operation expression, the result produced by this expression operation is 3, but this expression does not assign the operation result to the variable.
In the process of expression operation, the expression operates one or both of the data to generate operational symbols for the operator, and the data operated by the operator is called the operand. In WMLScript, we will use various operators, the following describes operators and related expressions in detail.
4.5.1 value assignment operator
The value assignment operator is used to assign values to a variable. It can return the result of an operation with an operand to the variable. The simplest value assignment operator is "=", for example, x = 2, assign 2 to variable X. In the following statements, values are assigned:
Var = "ABC ";
VaR B =;
B = "def ";
The value assignment operator does not need to specify an object or change the value of the variable on the right of the value assignment operator. The value assignment operators of WMLScript mainly include the following types:
1. =. This operation is used to assign an operand to the left operation.
2 + =. Returns the result of an operation that matches the left operand, and assigns the value to the left operand. For example, if X = 3, the result after X + = 2 is X = 5.
+ = Is a special operator, because it can connect two strings, so the + = operator can also operate on the string, and then assign the connected string to the left operand. For example, if X = "happy", x + = "New Year" is calculated and the result is X = "Happy New Year ".
3.-=. Describe the left operand with an operand, and then assign the result to the left operand. For example, if X = 3, then the result after X-= is x = 1.
4 * =. The left operation belongs to an operator for multiplication, and then the result is assigned to the left operand. For example, if X = 6, the result after X * = 2 is X = 6.
5/=. The right operation belongs to the right operator for multiplication, and then the operation result is assigned to the left operand. For example, if X = 6, the result after X * = 2 is X = 6.
6div =. Having the operand to process the left operand, and then assigning the integer part of the operation result to the left operand, for example, x = 7, then the result after the xdiv = 2 operation is X = 3.
7% =. The function is to evaluate the remainder and assign a value. Divide the right operand by the operand, and finally return the remainder of the operation to the operand. For example, if X = 7, the result after X % = 3 is x = 1.
8
9> =. You can perform the right operation on the left operation and the right operation on the right operation to perform the zero-left operation and assign the result to the left operand.
4.5.2 mathematical operators
Mathematical operators can perform operations on the operands of the numeric type, and then return an operation result of the numerical type.
1 +. This is an addition operation. It corresponds to an addition operation in a mathematical operation. For example, the result of expression 1 + 2 is 3.
The addition operator can also perform operations on the operands of the string type, and then connect the two strings as operations
2 -. That is, the check operator corresponds to the subtraction operation in the fast learning operation. For example, the calculation result of the expression 2-1 is 1.
"-" Is also a negative operator. When "-" is used as the negative operator, it is an operand. The negative operator function causes the opposite number of return operands.
3 *. This is a multiplication operator, which corresponds to the multiplication operation in mathematical operations. For example, the result of expression 2*3 is 6.
4 /. That is, the Division operator corresponds to the division operation in the mathematical operation, but the division operation in WMLScript is somewhat special. In WMLScript, the result after the division operation is a floating point number, instead of using the C language or Java language to perform Division operations on integers, the result is forcibly converted to integers. In WMLScript, 1/2 = 0.5, while in Jave, 1/2 = 0.
5div. This is the division operator, which corresponds to the Integer Operation in the mathematical operation. The result is an integer, which is the same as that in C or Java, you can perform Division operations on Integers to forcibly convert the calculation results to integers.
6%. That is, the modulo operator, which corresponds to the math operation, that is, Division of two operations, returns the remainder after division.
The modulo operator is used to determine whether a number can be divisible by another number.
(The year % 3 = 0) & (the year % 100! = 0) (the year % 400 = 0)
Here, & represents logical operations and operations, and represents the or operations in logical operations. We will introduce them in detail later.
7 ++. This is an incremental operator. It has only one operand. operations can be performed on the left side of the operator or on the right side of the operator. The operation completed by this operator is to add the operand to 1. Assume that the operand name is J and the value is 2. Then, ++ J adds 1 and returns the value 3 of J. Then, J ++ returns the value 2 of J, then add J to 1.
In a loop, we often use the incremental operator to do the opposite. The operation completed by the descending operation is reduced by 1 by the operand. For example, if the operand name is J and the value is 2, -- J first drops J by 1, and then returns 1, while j -- first returns J's value 2, then, reduce J by 1.
4.5.3 bitwise operators
To convert an operation to a 32-bit binary number in an operation, and then perform bitwise operations on each operand respectively. After the operation, the operation ends when the binary result is returned according to the standard WMLScript value type.
1 &. This is an operator. It can operate two operands by bit. Its operation rules are as follows:
0 & 0 = 0 0 & 1 = 0, 1 & 0 = 0, 1 & 1 = 1
2. This is a bit or operator. It can perform or operate on two operands by bit. The operation rule is:
00 = 0, 01 = 1, 11 = 1
3 ^. This is a unique or operator. It can perform bitwise XOR operations on two operations. The operation rule is: 0 ^ 0 = 0 0 ^ 1 = ^ 0 = ^ 1 = 0
4 ~. This is a bitwise non-operator. It has only one operand and can perform non-operations on the operands by bit. the operation rules are as follows :~ 0 = 1 ,~ = 0
5
6>. This is the right shift operator. It can shift the left operand to the right. given by the operand, the number of digits to be moved is discarded during the shift.
7 >>>. This is the right shift operator of 0, which is similar to the right shift operator. When positive numbers are operated, they have the same effect. The difference is that when a negative integer is shifted to the right, the maximum value is 1 after being converted to binary, therefore, after the right shift operation, the highest bit is still supplemented with 1, while when the right shift operation is filled with 0, the highest value is supplemented with 0. Therefore, this is a negative number and will be converted to a positive number.
4.5.4 logical operators
Logical operators can combine boolean expressions to complete logical operations, and then return the results of logical operations-True or false, so that complex logical judgment can be completed. There are three logical operations;
1 &&. That is, when both the logic and operator are true, the return result is true. In other cases, the return result is false or invalid.
2. This is an OR operator. when either of the two operands is true, the return result is true. In other cases, the return result is false or invalid.
3 !. It is a non-operator and has only one operand. If the operand is true, the returned result is flase. The returned result is true.
4.5.5 comparison Operators
The comparison operator can compare the operands and return a logical value to indicate whether the result of the comparison operation is true. The comparison operation can be numeric or string data. Comparison operators are also called Relational operators.
WMLScript supports a total of 6 Comparison operators, which are described below.
1. =. Equals operator. It can compare whether two operands are equal. If the two operands are equal, true is returned. Otherwise, false is returned.
2 .! =. That is, unequal operators. It can compare whether two operands are equal. If the two operands are equal, false is returned; otherwise, true is returned.
3.>. That is, greater than the operation. If the left operand is greater than the right operand, true is returned.