slang.tm
来自「一个C格式的脚本处理函数库源代码,可让你的C程序具有执行C格式的脚本文件」· TM 代码 · 共 1,763 行 · 第 1/5 页
TM
1,763 行
negative integer.#%}}}\sect1{Floating Point Numbers} #%{{{ Single and double precision floating point literals must contain either a decimal point or an exponent (or both). Here are examples of specifying the same double precision point number:#v+ 12. 12.0 12e0 1.2e1 120e-1 .12e2 0.12e2#v- Note that \var{12} is \em{not} a floating point number since it contains neither a decimal point nor an exponent. In fact, \var{12} is an integer. One may append the \var{f} character to the end of the number to indicate that the number is a single precision literal.#%}}}\sect1{Complex Numbers} #%{{{ The language implements complex numbers as a pair of double precision floating point numbers. The first number in the pair forms the \em{real} part, while the second number forms the \em{imaginary} part. That is, a complex number may be regarded as the sum of a real number and an imaginary number. Strictly speaking, the current implementation of the \slang does not support generic complex literals. However, it does support imaginary literals and a more generic complex number with a non-zero real part may be constructed from the imaginary literal via addition of a real number. An imaginary literal is specified in the same way as a floating point literal except that \var{i} or \var{j} is appended. For example,#v+ 12i 12.0i 12e0j#v- all represent the same imaginary number. Actually, \var{12i} is really an imaginary integer except that \slang automatically promotes it to a double precision imaginary number. A more generic complex number may be constructed from an imaginary literal via addition, e.g.,#v+ 3.0 + 4.0i#v- produces a complex number whose real part is \exmp{3.0} and whose imaginary part is \exmp{4.0}. The intrinsic functions \var{Real} and \var{Imag} may be used to retrieve the real and imaginary parts of a complex number, respectively.#%}}}\sect1{Strings} #%{{{ A string literal must be enclosed in double quotes as in:#v+ "This is a string".#v- Although there is no imposed limit on the length of a string, string literals must be less than 256 characters in length. It is possible to go beyond this limit by string concatenation, e.g.,#v+ "This is the first part of a long string" + "and this is the second half"#v- Any character except a newline (ASCII 10) or the null character (ASCII 0) may appear explicitly in a string literal. However, these characters may be used implicitly using the mechanism described below. The backslash character is a special character and is used to include other special characters (such as a newline character) in the string. The special characters recognized are:#v+ \" -- double quote \' -- single quote \\ -- backslash \a -- bell character (ASCII 7) \t -- tab character (ASCII 9) \n -- newline character (ASCII 10) \e -- escape character (ASCII 27) \xhhh -- character expressed in HEXADECIMAL notation \ooo -- character expressed in OCTAL notation \dnnn -- character expressed in DECIMAL#v- For example, to include the double quote character as part of the string, it must be preceded by a backslash character, e.g.,#v+ "This is a \"quote\""#v- Similarly, the next illustrates how a newline character may be included:#v+ "This is the first line\nand this is the second"#v-#%}}}\sect1{Null_Type} Objects of type \var{Null_Type} can have only one value: \var{NULL}. About the only thing that you can do with this data type is to assign it to variables and test for equality with other objects. Nevertheless, \var{Null_Type} is an important and extremely useful data type. Its main use stems from the fact that since it can be compared for equality with any other data type, it is ideal to represent the value of an object which does not yet have a value, or has an illegal value. As a trivial example of its use, consider#v+ define add_numbers (a, b) { if (a == NULL) a = 0; if (b == NULL) b = 0; return a + b; } variable c = add_numbers (1, 2); variable d = add_numbers (1, NULL); variable e = add_numbers (1,); variable f = add_numbers (,);#v- It should be clear that after these statements have been executed, \var{c} will have a value of \exmp{3}. It should also be clear that \var{d} will have a value of \exmp{1} because \var{NULL} has been passed as the second parameter. One feature of the language is that if a parameter has been omitted from a function call, the variable associated with that parameter will be set to \var{NULL}. Hence, \var{e} and \var{f} will be set to \exmp{1} and \exmp{0}, respectively. The \var{Null_Type} data type also plays an important role in the context of \em{structures}.\sect1{Ref_Type} Objects of \var{Ref_Type} are created using the unary \em{reference} operator \var{&}. Such objects may be \em{dereferenced} using the dereference operator \var{@}. For example, #v+ variable sin_ref = &sin; variable y = (@sin_ref) (1.0);#v- creates a reference to the \var{sin} function and assigns it to \var{sin_ref}. The second statement uses the dereference operator to call the function that \var{sin_ref} references. The \var{Ref_Type} is useful for passing functions as arguments to other functions, or for returning information from a function via its parameter list. The dereference operator is also used to create an instance of a structure. For these reasons, further discussion of this important type can be found in section ??? and section ???.\sect1{Array_Type and Struct_Type} Variables of type \var{Array_Type} and \var{Struct_Type} are known as \em{container objects}. They are much more complicated than the simple data types discussed so far and each obeys a special syntax. For these reasons they are discussed in a separate chapters. See ???.\sect1{DataType_Type Type} #%{{{ \slang defines a type called \var{DataType_Type}. Objects of this type have values that are type names. For example, an integer is an object of type \var{Integer_Type}. The literals of \var{DataType_Type} include:#v+ Char_Type (signed character) UChar_Type (unsigned character) Short_Type (short integer) UShort_Type (unsigned short integer) Integer_Type (plain integer) UInteger_Type (plain unsigned integer) Long_Type (long integer) ULong_Type (unsigned long integer) Float_Type (single precision real) Double_Type (double precision real) Complex_Type (complex numbers) String_Type (strings, C strings) BString_Type (binary strings) Struct_Type (structures) Ref_Type (references) Null_Type (NULL) Array_Type (arrays) DataType_Type (data types)#v- as well as the names of any other types that an application defines. The built-in function \var{typeof} returns the data type of its argument, i.e., a \var{DataType_Type}. For instance \exmp{typeof(7)} returns \var{Integer_Type} and \var{typeof(Integer_Type)} returns \var{DataType_Type}. One can use this function as in the following example:#v+ if (Integer_Type == typeof (x)) message ("x is an integer");#v- The literals of \var{DataType_Type} have other uses as well. One of the most common uses of these literals is to create arrays, e.g.,#v+ x = Complex_Type [100];#v- creates an array of \exmp{100} complex numbers and assigns it to \var{x}.#%}}}#%}}}\sect{Typecasting: Converting from one Type to Another} Occasionally, it is necessary to convert from one data type to another. For example, if you need to print an object as a string, it may be necessary to convert it to a \var{String_Type}. The \var{typecast} function may be used to perform such conversions. For example, consider#v+ variable x = 10, y; y = typecast (x, Double_Type);#v- After execution of these statements, \var{x} will have the integer value \exmp{10} and \var{y} will have the double precision floating point value \exmp{10.0}. If the object to be converted is an array, the \var{typecast} function will act upon all elements of the array. For example, #v+ variable x = [1:10]; % Array of integers variable y = typecast (x, Double_Type);#v- will create an array of \exmp{10} double precision values and assign it to \var{y}. One should also realize that it is not always possible to perform a typecast. For example, any attempt to convert an \var{Integer_Type} to a \var{Null_Type} will result in a run-time error. Often the interpreter will perform implicit type conversions as necessary to complete calculations. For example, when multiplying an \var{Integer_Type} with a \var{Double_Type}, it will convert the \var{Integer_Type} to a \var{Double_Type} for the purpose of the calculation. Thus, the example involving the conversion of an array of integers to an array of doubles could have been performed by multiplication by \exmp{1.0}, i.e.,#v+ variable x = [1:10]; % Array of integers variable y = 1.0 * x;#v- The \var{string} intrinsic function is similar to the typecast function except that it converts an object to a string representation. It is important to understand that a typecast from some type to \var{String_Type} is \em{not} the same as converting an object to its string operation. That is, \exmp{typecast(x,String_Type)} is not equivalent to \exmp{string(x)}. The reason for this is that when given an array, the \var{typecast} function acts on each element of the array to produce another array, whereas the \var{string} function produces a a string. The \var{string} function is useful for printing the value of an object. This use is illustrated in the following simple example:#v+ define print_object (x) { message (string (x)); }#v- Here, the \var{message} function has been used because it writes a string to the display. If the \var{string} function was not used and the \var{message} function was passed an integer, a type-mismatch error would have resulted.#%}}}\chapter{Identifiers} #%{{{ The names given to variables, functions, and data types are called \em{identifiers}. There are some restrictions upon the actual characters that make up an identifier. An identifier name must start with a letter (\var{[A-Za-z]}), an underscore character, or a dollar sign. The rest of the characters in the name can be any combination of letters, digits, dollar signs, or underscore characters. However, all identifiers whose name begins with two underscore characters are reserved for internal use by the interpreter and declarations of objects with such names should be avoided. Examples of valid identifiers include:#v+ mary _3 _this_is_ok a7e1 $44 _44$_Three#v- However, the following are not legal:#v+ 7abc 2e0 #xx#v- In fact, \exmp{2e0} actually specifies the real number \exmp{2.0}. Although the maximum length of identifiers is unspecified by the language, the length should be kept below \exmp{64} characters. The following identifiers are reserved by the language for use as keywords:#v+ !if _for do mod sign xor ERROR_BLOCK abs do_while mul2 sqr public EXIT_BLOCK and else not static private USER_BLOCK0 andelse exch or struct USER_BLOCK1 break for orelse switch USER_BLOCK2 case foreach pop typedef USER_BLOCK3 chs forever return using USER_BLOCK4 continue if shl variable __tmp define loop shr while#v- In addition, the next major \slang release (v2.0) will reserve \exmp{try} and \exmp{catch}, so it is probably a good idea to avoid those words until then.#%}}}\chapter{Variables} #%{{{ A variable must be declared before it can be used, otherwise an undefined name error will be generated. A variable is declared using the \kw{variable} keyword, e.g,#v+ variable x, y, z;#v- declares three variables, \exmp{x}, \exmp{y}, and \exmp{z}. This is an example of a variable declaration statement, and like all statements, it must end in a semi-colon. Variables declared this way are untyped and inherit a type upon assignment. The actual type checking is performed at run-time. For example,#v+ x = "This is a string"; x = 1.2; x = 3; x = 2i;#v- results in x being set successively to a string, a float, an integer, and to a complex number (\exmp{0+2i}). Any attempt to use a variable before it has acquired a type will result in an uninitialized variable error.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?