slang.tm

来自「一个C格式的脚本处理函数库源代码,可让你的C程序具有执行C格式的脚本文件」· TM 代码 · 共 1,763 行 · 第 1/5 页

TM
1,763 行
字号
   It is legal to put executable code in a variable declaration list.   That is,#v+         variable x = 1, y = sin (x);#v-   are legal variable declarations.  This also provides a convenient way   of initializing a variable.   Variables are classified as either \em{global} or \em{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 \var{public}, \var{static}, or \var{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:#v+      $0 $1 $2 $3 $4 $5 $6 $7 $8 $9#v-   An \em{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   \var{PI} which is a read-only double precision variable with a value   of approximately \exmp{3.14159265358979323846}.   #%}}}\chapter{Operators} #%{{{   \slang 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 \var{not} is an example of an unary operator.  Examples of   binary operators include the usual arithmetic operators   \var{+}, \var{-}, \var{*}, and \var{/}.  The operator given by   \var{-} 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., \exmp{a + b}.   Unary operators fall in one of two classes: postfix-unary or   prefix-unary.  For example, in the expression \exmp{-x}, the minus   sign is a prefix-unary operator.      Not all data types have binary or unary operations defined.  For   example, while \var{String_Type} objects support the \var{+}   operator, they do not admit the \var{*} operator.\sect{Unary Operators}   The \bf{unary} operators operate only upon a single operand.  They   include: \var{not}, \var{~}, \var{-}, \var{@}, \var{&}, as well as the   increment and decrement operators \var{++} and \var{--},   respectively.   The boolean operator \var{not} acts only upon integers and produces   \var{0} if its operand is non-zero, otherwise it produces \var{1}.   The bit-level not operator \var{~} performs a similar function,   except that it operates on the individual bits of its integer   operand.   The arithmetic negation operator \var{-} is the most well-known   unary operator.  It simply reverses the sign of its operand.   The reference (\var{&}) and dereference (\var{@}) operators will be   discussed in greater detail in section ???.  Similarly, the   increment (\var{++}) and decrement (\var{--}) operators will be   discussed in the context of the assignment operator.\sect{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   \var{String_Type} data type to permit concatenation between strings.\sect1{Arithmetic Operators} #%{{{   The arithmetic operators include \var{+}, \var{-}, \var{*}, \var{/},   which perform addition, subtraction, multiplication, and division,   respectively.  In addition to these, \slang supports the \var{mod}   operator as well as the power operator \var{^}.   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 \exmp{8/5} which   indicates division of the integer \var{8} by the integer \var{5}.   The result will be the integer \var{1} and \em{not} the floating   point value \var{1.6}.  However, \exmp{8/5.0} will produce   \var{1.6} because \exmp{5.0} is a floating point number.#%}}}\sect1{Relational Operators} #%{{{   The relational operators are \var{>}, \var{>=}, \var{<}, \var{<=},   \var{==}, and \var{!=}.  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 \var{1} if the comparison is true, or   \var{0} if the comparison is false.  For example, \exmp{6 >= 5}   returns \var{1}, but \var{6 == 5} produces   \var{0}.#%}}}\sect1{Boolean Operators} #%{{{   There are only two boolean binary operators: \var{or} and   \var{and}.  These operators are defined only for integers and   produce an integer result.  The \var{or} operator returns \var{1}   if either of its operands are non-zero, otherwise it produces   \var{0}.  The \var{and} operator produces \var{1} if and only if   both its operands are non-zero, otherwise it produces \var{0}.   Neither of these operators perform the so-called boolean   short-circuit evaluation.  For example, consider the expression:#v+      (x != 0) and (1/x > 10)#v-   Here, if \var{x} were to have a value of zero, a division by zero error   would occur because even though \var{x!=0} evaluates to zero, the   \var{and} operator is not short-circuited and the \var{1/x} expression   would still be evaluated.  Although these operators are not   short-circuited, \slang does have another mechanism of performing   short-circuit boolean evaluation via the \kw{orelse} and   \kw{andelse} expressions.  See below for information about these   constructs.#%}}}\sect1{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 \var{&}, \var{|}, \var{shl}, \var{shr}, and   \var{xor}.  The \var{&} operator performs a boolean AND operation   between the corresponding bits of the operands.  Similarly, the   \var{|} operator performs the boolean OR operation on the bits.   The bit-shifting operators \var{shl} and \var{shr} shift the bits   of the first operand by the number given by the second operand to   the left or right, respectively.  Finally, the \var{xor} performs   an EXCLUSIVE-OR operation.   These operators are commonly used to manipulate variables whose   individual bits have distinct meanings.  In particular, \var{&} is   usually used to test bits, \var{|} can be used to set bits, and   \var{xor} may be used to flip a bit.   As an example of using \var{&} 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   \var{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#v+      define get_buffer_flags ()      {         variable flags;         (,,,flags) = getbuf_info ();         return flags;      }#v-   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 \var{0} the   buffer has not been modified, otherwise it has.  Thus we can create   the function,#v+     define is_buffer_modified ()     {        variable flags = get_buffer_flags ();        return (flags & 1);     }#v-   where the integer \exmp{1} has been used since it has all of its   bits set to \var{0}, except for the zeroth one, which is set to   \var{1}.  (At this point, it should also be apparent that bits are   numbered from zero, thus an \var{8} bit integer consists of bits   \var{0} to \var{7}, where \var{0} is the least significant bit and   \var{7} is the most significant one.)  Similarly, we can create another   function#v+     define is_autosave_on ()     {        variable flags = get_buffer_flags ();        return (flags & 2);     }#v-   to determine whether or not autosave has been turned on for the   buffer.   The \var{shl} operator may be used to form the integer with only   the \em{nth} bit set.  For example, \exmp{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:#v+     define test_nth_bit (flags, nth)     {        return flags & (1 shl nth);     }#v-  #%}}}\sect1{Namespace operator}   The operator \var{->} is used to in conjunction with the name of a   namespace to access an object within the namespace.  For example,   if \exmp{A} is the name of a namespace containing the variable   \var{v}, then \exmp{A->v} refers to that variable.\sect1{Operator Precedence}\sect1{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 \slang language will produce incorrect results if both   operands of a binary expression return multiple values.  \em{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,#v+    define read_line (fp)    {       variable line, status;              status = fgets (&line, fp);       if (status == -1)         return -1;       return (line, status);    }#v-   defines a function, \var{read_line} that takes a single argument, a   handle to an open file, and returns one or two values, depending   upon the return value of \var{fgets}.  Now consider#v+        while (read_line (fp) > 0)          {             text = ();             % Do something with text             .             .          }#v-   Here the relational binary operator \var{>} forms a comparison   between one of the return values (the one at the top of the stack)   and \var{0}.  In accordance with the above rule, since \var{read_line}   returns multiple values, it occurs as the left binary operand.   Putting it on the right as in#v+        while (0 < read_line (fp))    % Incorrect          {             text = ();             % Do something with text             .             .          }#v-   violates the rule and will result in the wrong answer.#%}}}#%}}}\sect{Mixing Integer and Floating Point Arithmetic}   If a binary operation (\var{+}, \var{-}, \var{*} , \var{/}) is   performed on two integers, the result is an integer.  If at least   one of the operands is a float, the other is converted to float and   the result is float.  For example:#v+      11 / 2           --> 5   (integer)      11 / 2.0         --> 5.5 (float)      11.0 / 2         --> 5.5 (float)      11.0 / 2.0       --> 5.5 (float)#v-   Finally note that only integers may be used as array indices,    loop control variables, and bit operations.  The conversion   functions, \var{int} and \var{float}, may be used convert between   floats and ints where appropriate, e.g.,#v+      int (1.5)         --> 1 (integer)      float(1.5)        --> 1.5 (float)      float (1)         --> 1.0 (float)#v-\sect{Short Circuit Boolean Evaluation}   The boolean operators \var{or} and \var{and} \em{are not short   circuited} as they are in some languages.  \slang uses    \var{orelse} and \var{andelse} expressions for short circuit boolean   evaluation.  However, these are not binary operators. Expressions   of the form:\begin{tscreen}        \em{expr-1} and \em{expr-2} and ... \em{expr-n}\end{tscreen}   can be replaced by the short circuited version using \var{andelse}:\begin{tscreen}        andelse {\em{expr-1}} {\em{expr-2}} ... {\em{expr-n}}\end{tscreen}   A similar syntax holds for the \var{orelse} operator.  For example, consider   the statement:#v+      if ((x != 0) and (1/x > 10)) do_something ();#v-   Here, if \var{x} were to have a value of zero, a division by zero error   would occur because even though \var{x!=0} evaluates to zero, the   \var{and} operator is not short circuited and the \var{1/x} expression   would be evaluated causing division by zero. For this case, the   \var{andelse} expression could be used to avoid the problem:#v+      if (andelse          {x != 0}          {1 / x > 10})  do_something ();#v-#%}}}\chapter{Statements} #%{{{   Loosely speaking, a \em{statement} is composed of \em{expressions}   that are grouped according to the syntax or grammar of the language   to express a complete computation.  Statements are analogous to   sentences in a human language and expressions are like phrases.   All statements in the \slang language must end in a semi-colon.   A statement that occurs within a function is executed only during   execution of the function.  However, statements that occur outside   the context of a function are evaluated immediately.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?