📄 slang.txt
字号:
bill.first_name = "Bill"; bill.last_name = "Clinton"; bill.age = 51; The big advantage of creating a new type is that one can go on to cre- ate arrays of the data type variable People = Person_Type [100]; People[0].first_name = "Bill"; People[1].first_name = "Hillary"; The creation and initialization of a structure may be facilitated by a function such as 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); 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 ???. 3.6. 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 static keyword will be placed in the private namespace associated with compilation unit. For example, variable i; static variable i; defines two variables called i. The first declaration defines i in the global namespace, but the second declaration defines i in the pri- vate namespace. The -> 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 i in the global namespace, one would use 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 implements function may be used give the private namespace a name, allowing access to its objects. For example, if the file t.sl contains implements ("A"); static variable i; then another file may access the variable i via A->i. 4. Data Types and Literal Constants The current implementation of the S-Lang 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 typedef mechanism. Literal constants are objects such as the integer 3 or the string "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. 4.1. Predefined Data Types The current version of S-Lang defines integer, floating point, complex, and string types. It also defines special purpose data types such as Null_Type, DataType_Type, and Ref_Type. These types are discussed below. 4.1.1. Integers The S-Lang 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 literal can be specified in one of several ways: o As a decimal (base 10) integer consisting of the characters 0 through 9, e.g., 127. An integer specified this way cannot begin with a leading 0. That is, 0127 is not the same as 127. o Using hexadecimal (base 16) notation consisting of the characters 0 to 9 and A through F. The hexadecimal number must be preceded by the characters 0x. For example, 0x7F specifies an integer using hexadecimal notation and has the same value as decimal 127. o In Octal notation using characters 0 through 7. The Octal number must begin with a leading 0. For example, 0177 and 127 represent the same integer. Short, long, and unsigned types may be specified by using the proper suffixes: L indicates that the integer is a long integer, h indicates that the integer is a short integer, and U indicates that it is unsigned. For example, 1UL specifies an unsigned long integer. Finally, a character literal may be specified using a notation containing a character enclosed in single quotes as '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, i = '0'; assigns to i the character 48 since the '0' character has an ASCII value of 48. Any integer may be preceded by a minus sign to indicate that it is a negative integer. 4.1.2. 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: 12. 12.0 12e0 1.2e1 120e-1 .12e2 0.12e2 Note that 12 is not a floating point number since it contains neither a decimal point nor an exponent. In fact, 12 is an integer. One may append the f character to the end of the number to indicate that the number is a single precision literal. 4.1.3. Complex Numbers The language implements complex numbers as a pair of double precision floating point numbers. The first number in the pair forms the real part, while the second number forms the 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 S-Lang 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 i or j is appended. For example, 12i 12.0i 12e0j all represent the same imaginary number. Actually, 12i is really an imaginary integer except that S-Lang 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., 3.0 + 4.0i produces a complex number whose real part is 3.0 and whose imaginary part is 4.0. The intrinsic functions Real and Imag may be used to retrieve the real and imaginary parts of a complex number, respectively. 4.1.4. Strings A string literal must be enclosed in double quotes as in: "This is a string". 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., "This is the first part of a long string" + "and this is the second half" Any character except a newline (ASCII 10) or the null character (ASCII 0) may appear explicitly in a string literal. However, these charac- ters 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: \" -- 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 For example, to include the double quote character as part of the string, it must be preceded by a backslash character, e.g., "This is a \"quote\"" Similarly, the next illustrates how a newline character may be included: "This is the first line\nand this is the second" 4.1.5. Null_Type Objects of type Null_Type can have only one value: 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, 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 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 (,); It should be clear that after these statements have been executed, c will have a value of 3. It should also be clear that d will have a value of 1 because 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 NULL. Hence, e and f will be set to 1 and 0, respectively. The Null_Type data type also plays an important role in the context of structures. 4.1.6. Ref_Type Objects of Ref_Type are created using the unary reference operator &. Such objects may be dereferenced using the dereference operator @. For example, variable sin_ref = &sin; variable y = (@sin_ref) (1.0); creates a reference to the sin function and assigns it to sin_ref. The second statement uses the dereference operator to call the func- tion that sin_ref references. The 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 ???. 4.1.7. Array_Type and Struct_Type Variables of type Array_Type and Struct_Type are known as 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 ???. 4.1.8. DataType_Type Type S-Lang defines a type called DataType_Type. Objects of this type have values that are type names. For example, an integer is an object of type Integer_Type. The literals of DataType_Type include: 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) as well as the names of any other types that an application defines. The built-in function typeof returns the data type of its argument, i.e., a DataType_Type. For instance typeof(7) returns Integer_Type and typeof(Integer_Type) returns DataType_Type. One can use this function as in the following example: if (Integer_Type == typeof (x)) message ("x is an integer"); The literals of DataType_Type have other uses as well. One of the most common uses of these literals is to create arrays, e.g., x = Complex_Type [100]; creates an array of 100 complex numbers and assigns it to x. 4.2. 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 String_Type. The typecast function may be used to perform such conversions. For example, consider variable x = 10, y; y = typecast (x, Double_Type); After execution of these statements, x will have the integer value 10 and y will have the double precision floating point value 10.0. If the object to be converted is an array, the typecast function will act upon all elements of the array. For example, variable x = [1:10]; % Array of integers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -