📄 slang.txt
字号:
variable y = typecast (x, Double_Type); will create an array of 10 double precision values and assign it to y. One should also realize that it is not always possible to perform a typecast. For example, any attempt to convert an Integer_Type to a 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 Integer_Type with a Double_Type, it will convert the Integer_Type to a 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 1.0, i.e., variable x = [1:10]; % Array of integers variable y = 1.0 * x; The 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 String_Type is not the same as converting an object to its string operation. That is, typecast(x,String_Type) is not equivalent to string(x). The reason for this is that when given an array, the typecast function acts on each element of the array to produce another array, whereas the string function produces a a string. The string function is useful for printing the value of an object. This use is illustrated in the following simple example: define print_object (x) { message (string (x)); } Here, the message function has been used because it writes a string to the display. If the string function was not used and the message function was passed an integer, a type-mismatch error would have resulted. 5. Identifiers The names given to variables, functions, and data types are called identifiers. There are some restrictions upon the actual characters that make up an identifier. An identifier name must start with a letter ([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: mary _3 _this_is_ok a7e1 $44 _44$_Three However, the following are not legal: 7abc 2e0 #xx In fact, 2e0 actually specifies the real number 2.0. Although the maximum length of identifiers is unspecified by the language, the length should be kept below 64 characters. The following identifiers are reserved by the language for use as keywords: !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 In addition, the next major S-Lang release (v2.0) will reserve try and catch, so it is probably a good idea to avoid those words until then. 6. 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 variable keyword, e.g, variable x, y, z; declares three variables, x, y, and z. This is an example of a vari- able 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, x = "This is a string"; x = 1.2; x = 3; x = 2i; results in x being set successively to a string, a float, an integer, and to a complex number (0+2i). Any attempt to use a variable before it has acquired a type will result in an uninitialized variable error. It is legal to put executable code in a variable declaration list. That is, variable x = 1, y = sin (x); are legal variable declarations. This also provides a convenient way of initializing a variable. Variables are classified as either global or local. A variable declared inside a function is said to be local and has no meaning outside the function. A variable is said to be global if it was declared outside a function. Global variables are further classified as being public, static, or private, according to the name space where they were defined. See chapter ??? for more information about name spaces. The following global variables are predefined by the language and are mainly used as convenience variables: $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 An intrinsic variable is another type of global variable. Such variables have a definite type which cannot be altered. Variables of this type may also be defined to be read-only, or constant variables. An example of an intrinsic variable is PI which is a read-only double precision variable with a value of approximately 3.14159265358979323846. 7. Operators S-Lang supports a variety of operators that are grouped into three classes: assignment operators, binary operators, and unary operators. An assignment operator is used to assign a value to a variable. They will be discussed more fully in the context of the assignment statement in section ???. An unary operator acts only upon a single quantity while a binary operation is an operation between two quantities. The boolean operator not is an example of an unary operator. Examples of binary operators include the usual arithmetic operators +, -, *, and /. The operator given by - can be either an unary operator (negation) or a binary operator (subtraction); the actual operation is determined from the context in which it is used. Binary operators are used in algebraic forms, e.g., a + b. Unary operators fall in one of two classes: postfix-unary or prefix-unary. For example, in the expression -x, the minus sign is a prefix-unary operator. Not all data types have binary or unary operations defined. For example, while String_Type objects support the + operator, they do not admit the * operator. 7.1. Unary Operators The unary operators operate only upon a single operand. They include: not, ~, -, @, &, as well as the increment and decrement operators ++ and --, respectively. The boolean operator not acts only upon integers and produces 0 if its operand is non-zero, otherwise it produces 1. The bit-level not operator ~ performs a similar function, except that it operates on the individual bits of its integer operand. The arithmetic negation operator - is the most well-known unary operator. It simply reverses the sign of its operand. The reference (&) and dereference (@) operators will be discussed in greater detail in section ???. Similarly, the increment (++) and decrement (--) operators will be discussed in the context of the assignment operator. 7.2. Binary Operators The binary operators may be grouped according to several classes: arithmetic operators, relational operators, boolean operators, and bitwise operators. All binary and unary operators may be overloaded. For example, the arithmetic plus operator has been overloaded by the String_Type data type to permit concatenation between strings. 7.2.1. Arithmetic Operators The arithmetic operators include +, -, *, /, which perform addition, subtraction, multiplication, and division, respectively. In addition to these, S-Lang supports the mod operator as well as the power operator ^. The data type of the result produced by the use of one of these operators depends upon the data types of the binary participants. If they are both integers, the result will be an integer. However, if the operands are not of the same type, they will be converted to a common type before the operation is performed. For example, if one is a floating point value and the other is an integer, the integer will be converted to a float. In general, the promotion from one type to another is such that no information is lost, if possible. As an example, consider the expression 8/5 which indicates division of the integer 8 by the integer 5. The result will be the integer 1 and not the floating point value 1.6. However, 8/5.0 will produce 1.6 because 5.0 is a floating point number. 7.2.2. Relational Operators The relational operators are >, >=, <, <=, ==, and !=. These perform the comparisons greater than, greater than or equal, less than, less than or equal, equal, and not equal, respectively. The result of one of these comparisons is the integer 1 if the comparison is true, or 0 if the comparison is false. For example, 6 >= 5 returns 1, but 6 == 5 produces 0. 7.2.3. Boolean Operators There are only two boolean binary operators: or and and. These operators are defined only for integers and produce an integer result. The or operator returns 1 if either of its operands are non-zero, otherwise it produces 0. The and operator produces 1 if and only if both its operands are non-zero, otherwise it produces 0. Neither of these operators perform the so-called boolean short-circuit evaluation. For example, consider the expression: (x != 0) and (1/x > 10) Here, if x were to have a value of zero, a division by zero error would occur because even though x!=0 evaluates to zero, the and opera- tor is not short-circuited and the 1/x expression would still be eval- uated. Although these operators are not short-circuited, S-Lang does have another mechanism of performing short-circuit boolean evaluation via the orelse and andelse expressions. See below for information about these constructs. 7.2.4. Bitwise Operators The bitwise binary operators are defined only with integer operands and are used for bit-level operations. Operators that fall in this class include &, |, shl, shr, and xor. The & operator performs a boolean AND operation between the corresponding bits of the operands. Similarly, the | operator performs the boolean OR operation on the bits. The bit-shifting operators shl and shr shift the bits of the first operand by the number given by the second operand to the left or right, respectively. Finally, the xor performs an EXCLUSIVE-OR operation. These operators are commonly used to manipulate variables whose individual bits have distinct meanings. In particular, & is usually used to test bits, | can be used to set bits, and xor may be used to flip a bit. As an example of using & to perform tests on bits, consider the following: The jed text editor stores some of the information about a buffer in a bitmapped integer variable. The value of this variable may be retrieved using the jed intrinsic function getbuf_info, which actually returns four quantities: the buffer flags, the name of the buffer, directory name, and file name. For the purposes of this section, only the buffer flags are of interest and can be retrieved via a function such as define get_buffer_flags () { variable flags; (,,,flags) = getbuf_info (); return flags; } The buffer flags is a bitmapped quantity where the 0th bit indicates whether or not the buffer has been modified, the first bit indicates whether or not autosave has been enabled for the buffer, and so on. Consider for the moment the task of determining if the buffer has been modified. This can be determined by looking at the zeroth bit, if it is 0 the buffer has not been modified, otherwise it has. Thus we can create the function, define is_buffer_modified () { variable flags = get_buffer_flags (); return (flags & 1); } where the integer 1 has been used since it has all of its bits set to 0, except for the zeroth one, which is set to 1. (At this point, it should also be apparent that bits are numbered from zero, thus an 8 bit integer consists of bits 0 to 7, where 0 is the least significant bit and 7 is the most significant one.) Similarly, we can create another function define is_autosave_on () { variable flags = get_buffer_flags (); return (flags & 2); } to determine whether or not autosave has been turned on for the buffer. The shl operator may be used to form the integer with only the nth bit set. For example, 1 shl 6 produces an integer with all bits set to zero except the sixth bit, which is set to one. The following example exploits this fact: define test_nth_bit (flags, nth) { return flags & (1 shl nth); } 7.2.5. Namespace operator The operator -> is used to in conjunction with the name of a namespace to access an object within the namespace. For example, if A is the name of a namespace containing the variable v, then A->v refers to that variable. 7.2.6. Operator Precedence 7.2.7. Binary Operators and Functions Returning Multiple Values Care must be exercised when using binary operators with an operand the returns multiple values. In fact, the current implementation of the S-Lang language will produce incorrect results if both operands of a binary expression return multiple values. At most, only one of operands of a binary expression can return multiple values, and that operand must be the first one, not the second. For example, define read_line (fp) { variable line, status; status = fgets (&line, fp); if (status == -1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -