slang.tm
来自「一个C格式的脚本处理函数库源代码,可让你的C程序具有执行C格式的脚本文件」· TM 代码 · 共 1,763 行 · 第 1/5 页
TM
1,763 行
#v- See section ??? for more information about string variables.#%}}}\sect{Referencing and Dereferencing} #%{{{ The unary prefix operator, \var{&}, may be used to create a \em{reference} to an object, which is similar to a pointer in other languages. References are commonly used as a mechanism to pass a function as an argument to another function as the following example illustrates:#v+ define compute_functional_sum (funct) { variable i, s; s = 0; for (i = 0; i < 10; i++) { s += (@funct)(i); } return s; } variable sin_sum = compute_functional_sum (&sin); variable cos_sum = compute_functional_sum (&cos);#v- Here, the function \var{compute_functional_sum} applies the function specified by the parameter \var{funct} to the first \exmp{10} integers and returns the sum. The two statements following the function definition show how the \var{sin} and \var{cos} functions may be used. Note the \var{@} operator in the definition of \var{compute_functional_sum}. It is known as the \em{dereference} operator and is the inverse of the reference operator. Another use of the reference operator is in the context of the \var{fgets} function. For example,#v+ define read_nth_line (file, n) { variable fp, line; fp = fopen (file, "r"); while (n > 0) { if (-1 == fgets (&line, fp)) return NULL; n--; } return line; }#v- uses the \var{fgets} function to read the nth line of a file. In particular, a reference to the local variable \var{line} is passed to \var{fgets}, and upon return \var{line} will be set to the character string read by \var{fgets}. Finally, references may be used as an alternative to multiple return values by passing information back via the parameter list. The example involving \var{fgets} presented above provided an illustration of this. Another example is#v+ define set_xyz (x, y, z) { @x = 1; @y = 2; @z = 3; } variable X, Y, Z; set_xyz (&X, &Y, &Z);#v- which, after execution, results in \var{X} set to \exmp{1}, \var{Y} set to \exmp{2}, and \var{Z} set to \exmp{3}. A C programmer will note the similarity of \var{set_xyz} to the following C implementation:#v+ void set_xyz (int *x, int *y, int *z) { *x = 1; *y = 2; *z = 3; }#v-#%}}}\sect{Arrays} #%{{{ The \slang language supports multi-dimensional arrays of all datatypes. For example, one can define arrays of references to functions as well as arrays of arrays. Here are a few examples of creating arrays:#v+ variable A = Integer_Type [10]; variable B = Integer_Type [10, 3]; variable C = [1, 3, 5, 7, 9];#v- The first example creates an array of \var{10} integers and assigns it to the variable \var{A}. The second example creates a 2-d array of \var{30} integers arranged in \var{10} rows and \var{3} columns and assigns the result to \var{B}. In the last example, an array of \var{5} integers is assigned to the variable \var{C}. However, in this case the elements of the array are initialized to the values specified. This is known as an \em{inline-array}. \slang also supports something called an \em{range-array}. An example of such an array is#v+ variable C = [1:9:2];#v- This will produce an array of 5 integers running from \exmp{1} through \exmp{9} in increments of \exmp{2}. Arrays are passed by reference to functions and never by value. This permits one to write functions which can initialize arrays. For example,#v+ define init_array (a) { variable i, imax; imax = length (a); for (i = 0; i < imax; i++) { a[i] = 7; } } variable A = Integer_Type [10]; init_array (A);#v- creates an array of \var{10} integers and initializes all its elements to \var{7}. There are more concise ways of accomplishing the result of the previous example. These include:#v+ variable A = [7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; variable A = Integer_Type [10]; A[[0:9]] = 7; variable A = Integer_Type [10]; A[*] = 7;#v- The second and third methods use an array of indices to index the array \var{A}. In the second, the range of indices has been explicitly specified, whereas the third example uses a wildcard form. See section ??? for more information about array indexing. Although the examples have pertained to integer arrays, the fact is that \slang arrays can be of any type, e.g.,#v+ variable A = Double_Type [10]; variable B = Complex_Type [10]; variable C = String_Type [10]; variable D = Ref_Type [10];#v- create \var{10} element arrays of double, complex, string, and reference types, respectively. The last example may be used to create an array of functions, e.g.,#v+ D[0] = &sin; D[1] = &cos;#v- The language also defines unary, binary, and mathematical operations on arrays. For example, if \var{A} and \var{B} are integer arrays, then \exmp{A + B} is an array whose elements are the sum of the elements of \var{A} and \var{B}. A trivial example that illustrates the power of this capability is#v+ variable X, Y; X = [0:2*PI:0.01]; Y = 20 * sin (X);#v- which is equivalent to the highly simplified C code:#v+ double *X, *Y; unsigned int i, n; n = (2 * PI) / 0.01 + 1; X = (double *) malloc (n * sizeof (double)); Y = (double *) malloc (n * sizeof (double)); for (i = 0; i < n; i++) { X[i] = i * 0.01; Y[i] = 20 * sin (X[i]); }#v-#%}}}\sect{Structures and User-Defined Types} #%{{{ A \em{structure} is similar to an array in the sense that it is a container object. However, the elements of an array must all be of the same type (or of \var{Any_Type}), whereas a structure is heterogeneous. As an example, consider#v+ variable person = struct { first_name, last_name, age }; variable bill = @person; bill.first_name = "Bill"; bill.last_name = "Clinton"; bill.age = 51;#v- In this example a structure consisting of the three fields has been created and assigned to the variable \var{person}. Then an \em{instance} of this structure has been created using the dereference operator and assigned to \var{bill}. Finally, the individual fields of \var{bill} were initialized. This is an example of an \em{anonymous} structure. A \em{named} structure is really a new data type and may be created using the \kw{typedef} keyword:#v+ typedef struct { first_name, last_name, age } Person_Type; variable bill = @Person_Type; bill.first_name = "Bill"; bill.last_name = "Clinton"; bill.age = 51;#v- The big advantage of creating a new type is that one can go on to create arrays of the data type#v+ variable People = Person_Type [100]; People[0].first_name = "Bill"; People[1].first_name = "Hillary";#v- The creation and initialization of a structure may be facilitated by a function such as#v+ define create_person (first, last, age) { variable person = @Person_Type; person.first_name = first; person.last_name = last; person.age = age; return person; } variable Bill = create_person ("Bill", "Clinton", 51);#v- Other common uses of structures is the creation of linked lists, binary trees, etc. For more information about these and other features of structures, see section ???.#%}}}\sect{Namespaces} In addition to the global namespace, each compilation unit (e.g., a file) is given a private namespace. A variable or function name that is declared using the \var{static} keyword will be placed in the private namespace associated with compilation unit. For example, #v+ variable i; static variable i;#v- defines two variables called \var{i}. The first declaration defines \var{i} in the global namespace, but the second declaration defines \var{i} in the private namespace. The \exmp{->} operator may be used in conjunction with the name of the namespace to access objects in the name space. In the above example, to access the variable \var{i} in the global namespace, one would use \exmp{Global->i}. Unless otherwise specified, a private namespace has no name and its objects may not be accessed from outside the compilation unit. However, the \var{implements} function may be used give the private namespace a name, allowing access to its objects. For example, if the file \exmp{t.sl} contains#v+ implements ("A"); static variable i;#v- then another file may access the variable \var{i} via \exmp{A->i}. #%}}}\chapter{Data Types and Literal Constants} #%{{{ The current implementation of the \slang language permits up to 256 distinct data types, including predefined data types such as integer and floating point, as well as specialized applications specific data types. It is also possible to create new data types in the language using the \kw{typedef} mechanism. Literal constants are objects such as the integer \exmp{3} or the string \exmp{"hello"}. The actual data type given to a literal constant depends upon the syntax of the constant. The following sections describe the syntax of literals of specific data types.\sect{Predefined Data Types} #%{{{ The current version of \slang defines integer, floating point, complex, and string types. It also defines special purpose data types such as \var{Null_Type}, \var{DataType_Type}, and \var{Ref_Type}. These types are discussed below.\sect1{Integers} #%{{{ The \slang language supports both signed and unsigned characters, short integer, long integer, and plain integer types. On most 32 bit systems, there is no difference between an integer and a long integer; however, they may differ on 16 and 64 bit systems. Generally speaking, on a 16 bit system, plain integers are 16 bit quantities with a range of -32767 to 32767. On a 32 bit system, plain integers range from -2147483648 to 2147483647. An plain integer \em{literal} can be specified in one of several ways:\begin{itemize}\item As a decimal (base 10) integer consisting of the characters \var{0} through \var{9}, e.g., \var{127}. An integer specified this way cannot begin with a leading \var{0}. That is, \var{0127} is \em{not} the same as \var{127}.\item Using hexadecimal (base 16) notation consisting of the characters \var{0} to \var{9} and \var{A} through \var{F}. The hexadecimal number must be preceded by the characters \var{0x}. For example, \var{0x7F} specifies an integer using hexadecimal notation and has the same value as decimal \var{127}.\item In Octal notation using characters \var{0} through \var{7}. The Octal number must begin with a leading \var{0}. For example, \var{0177} and \var{127} represent the same integer. Short, long, and unsigned types may be specified by using the proper suffixes: \var{L} indicates that the integer is a long integer, \var{h} indicates that the integer is a short integer, and \var{U} indicates that it is unsigned. For example, \exmp{1UL} specifies an unsigned long integer. Finally, a character literal may be specified using a notation containing a character enclosed in single quotes as \exmp{'a'}. The value of the character specified this way will lie in the range 0 to 256 and will be determined by the ASCII value of the character in quotes. For example,#v+ i = '0';#v- assigns to \var{i} the character 48 since the \exmp{'0'} character has an ASCII value of 48.\end{itemize} Any integer may be preceded by a minus sign to indicate that it is a
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?