slang.tm

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

TM
1,763 行
字号
   The language supports several different types of statements such as   assignment statements, conditional statements, and so forth.  These   are described in detail in the following sections.\sect{Variable Declaration Statements}   Variable declarations were already discussed in chapter ???.  For   the sake of completeness, a variable declaration is a statement of   the form\begin{tscreen}     variable \em{variable-declaration-list} ;\end{tscreen}   where the \em{variable-declaration-list} is a comma separated list   of one or more variable names with optional initializations, e.g.,#v+     variable x, y = 2, z;#v-\sect{Assignment Statements} #%{{{   Perhaps the most well known form of statement is the \em{assignment   statement}.  Statements of this type consist of a left-hand side,   an assignment operator, and a right-hand side.  The left-hand side   must be something to which an assignment can be performed.  Such   an object is called an \em{lvalue}.      The most common assignment operator is the simple assignment   operator \var{=}.  Simple of its use include#v+      x = 3;      x = some_function (10);      x = 34 + 27/y + some_function (z);      x = x + 3;#v-   In addition to the simple assignment operator, \slang   also supports the assignment operators \var{+=} and \var{-=}.   Internally, \slang transforms#v+       a += b;#v-   to#v+       a = a + b;#v-   Similarly, \exmp{a -= b} is transformed to \exmp{a = a - b}.  It is   extremely important to realize that, in general, \exmp{a+b} is not   equal to \exmp{b+a}.  This means that \exmp{a+=b} is not the same   as \exmp{a=b+a}.  As an example consider#v+      a = "hello"; a += "world";#v-   After execution of these two statements, \var{a} will have the   value \exmp{"helloworld"} and not \exmp{"worldhello"}.   Since adding or subtracting \exmp{1} from a variable is quite   common, \slang also supports the unary increment and decrement   operators \exmp{++}, and \exmp{--}, respectively.  That is, for   numeric data types, #v+       x = x + 1;       x += 1;       x++;#v-   are all equivalent.  Similarly,#v+       x = x - 1;       x -= 1;       x--;#v-   are also equivalent.   Strictly speaking, \var{++} and \var{--} are unary operators.  When   used as \var{x++}, the \var{++} operator is said to be a   \em{postfix-unary} operator.  However, when used as \var{++x} it is   said to be a \em{prefix-unary} operator.  The current   implementation does not distinguish between the two forms, thus   \var{x++} and \var{++x} are equivalent.  The reason for this   equivalence is \em{that assignment expressions do not return a value in   the \slang language} as they do in C.  Thus one should exercise care   and not try to write C-like code such as#v+      x = 10;      while (--x) do_something (x);     % Ok in C, but not in S-Lang#v-   The closest valid \slang form involves a \em{comma-expression}:#v+      x = 10;      while (x--, x) do_something (x);  % Ok in S-Lang and in C#v-   \slang also supports a \em{multiple-assignment} statement.  It is   discussed in detail in section ???.#%}}}\sect{Conditional and Looping Statements} #%{{{  \slang supports a wide variety of conditional and looping  statements.  These constructs operate on statements grouped together  in \em{blocks}.  A block is a sequence of \slang statements enclosed  in braces and may contain other blocks. However, a block cannot  include function declarations.  In the following,  \em{statement-or-block} refers to either a single  \slang statement or to a block of statements, and  \em{integer-expression} is an integer-valued expression.  \em{next-statement} represents the statement following the form   under discussion.\sect1{Conditional Forms} #%{{{\sect2{if}   The simplest condition statement is the \kw{if} statement.  It   follows the syntax\begin{tscreen}        if (\em{integer-expression}) \em{statement-or-block}        \em{next-statement}\end{tscreen}   If \em{integer-expression} evaluates to a non-zero result, then the   statement or group of statements implied \em{statement-or-block}   will get executed.  Otherwise, control will proceed to   \em{next-statement}.   An example of the use of this type of conditional statement is#v+       if (x != 0)          {            y = 1.0 / x;            if (x > 0) z = log (x);         }#v-   This example illustrates two \var{if} statements where the second   \var{if} statement is part of the block of statements that belong to   the first.\sect2{if-else}   Another form of \kw{if} statement is the \em{if-else} statement.   It follows the syntax:\begin{tscreen}      if (\em{integer-expression}) \em{statement-or-block-1}      else \em{statement-or-block-2}      \em{next-statement}\end{tscreen}   Here, if \em{expression} returns non-zero,   \em{statement-or-block-1} will get executed and control will pass   on to \em{next-statement}. However, if \em{expression} returns zero,   \em{statement-or-block-2} will get executed before continuing with   \em{next-statement}.  A simple example of this form is#v+     if (x > 0) z = log (x); else error ("x must be positive");#v-   Consider the more complex example:#v+     if (city == "Boston")       if (street == "Beacon") found = 1;     else if (city == "Madrid")        if (street == "Calle Mayor") found = 1;     else found = 0;#v-   This example illustrates a problem that beginners have with   \em{if-else} statements.  The grammar presented above shows that   the this example is equivalent to#v+     if (city == "Boston")       {         if (street == "Beacon") found = 1;         else if (city == "Madrid")           {             if (street == "Calle Mayor") found = 1;             else found = 0;           }       }#v-   It is important to understand the grammar and not be seduced by the   indentation!\sect2{!if}   One often encounters \kw{if} statements similar to\begin{tscreen}     if (\em{integer-expression} == 0) \em{statement-or-block}\end{tscreen}   or equivalently,\begin{tscreen}     if (not(\em{integer-expression})) \em{statement-or-block}\end{tscreen}   The \kw{!if} statement was added to the language to simplify the   handling of such statements.  It obeys the syntax\begin{tscreen}     !if (\em{integer-expression}) \em{statement-or-block}\end{tscreen}   and is functionally equivalent to\begin{tscreen}     if (not (\em{expression})) \em{statement-or-block}\end{tscreen}\sect2{orelse, andelse}  These constructs were discussed earlier.  The syntax for the  \var{orelse} statement is:\begin{tscreen}     orelse {\em{integer-expression-1}} ... {\em{integer-expression-n}}\end{tscreen}  This causes each of the blocks to be executed in turn until one of  them returns a non-zero integer value.  The result of this statement  is the integer value returned by the last block executed.  For  example,#v+     orelse { 0 } { 6 } { 2 } { 3 }#v-  returns \var{6} since the second block is the first to return a  non-zero result.  The last two block will not get executed.  The syntax for the \var{andelse} statement is:\begin{tscreen}     andelse {\em{integer-expression-1}} ... {\em{integer-expression-n}}\end{tscreen}  Each of the blocks will be executed in turn until one of  them returns a zero value.  The result of this statement is the  integer value returned by the last block executed.  For example,#v+     andelse { 6 } { 2 } { 0 } { 4 }#v-  returns \var{0} since the third block will be the last to execute.\sect2{switch}  The switch statement deviates the most from its C counterpart.  The  syntax is:#v+          switch (x)            { ...  :  ...}              .              .            { ...  :  ...}#v-   The `\var{:}' operator is a special symbol which means to test   the top item on the stack, and if it is non-zero, the rest of the block   will get executed and control will pass out of the switch statement.   Otherwise, the execution of the block will be terminated and the process   will be repeated for the next block.  If a block contains no   \var{:} operator, the entire block is executed and control will   pass onto the next statement following the \kw{switch} statement.   Such a block is known as the \em{default} case.      As a simple example, consider the following:#v+      switch (x)        { x == 1 : message("Number is one.");}        { x == 2 : message("Number is two.");}        { x == 3 : message("Number is three.");}        { x == 4 : message("Number is four.");}        { x == 5 : message("Number is five.");}        { message ("Number is greater than five.");}#v-   Suppose \var{x} has an integer value of \exmp{3}.  The first two   blocks will terminate at the `\var{:}' character because each of the   comparisons with \var{x} will produce zero.  However, the third   block will execute to completion.  Similarly, if \var{x} is   \exmp{7}, only the last block will execute in full.   A more familiar way to write the previous example used the   \kw{case} keyword:#v+      switch (x)        { case 1 : print("Number is one.");}        { case 2 : print("Number is two.");}        { case 3 : print("Number is three.");}        { case 4 : print("Number is four.");}        { case 5 : print("Number is five.");}        { print ("Number is greater than five.");}#v-   The \var{case} keyword is a more useful comparison operator because   it can perform a comparison between different data types while   using \var{==} may result in a type-mismatch error.  For example,#v+      switch (x)        { (x == 1) or (x == "one") : print("Number is one.");}        { (x == 2) or (x == "two") : print("Number is two.");}        { (x == 3) or (x == "three") : print("Number is three.");}        { (x == 4) or (x == "four") : print("Number is four.");}        { (x == 5) or (x == "five") : print("Number is five.");}        { print ("Number is greater than five.");}#v-  will fail because the \var{==} operation is not defined between  strings and integers.  The correct way to write this to use the  \var{case} keyword:#v+      switch (x)        { case 1 or case "one" : print("Number is one.");}        { case 2 or case "two" : print("Number is two.");}        { case 3 or case "three" : print("Number is three.");}        { case 4 or case "four" : print("Number is four.");}        { case 5 or case "five" : print("Number is five.");}        { print ("Number is greater than five.");}#v-#%}}}\sect1{Looping Forms} #%{{{\sect2{while}   The \kw{while} statement follows the syntax\begin{tscreen}      while (\em{integer-expression}) \em{statement-or-block}      \em{next-statement}\end{tscreen}   It simply causes \em{statement-or-block} to get executed as long as   \em{integer-expression} evaluates to a non-zero result.  For   example, #v+      i = 10;       while (i)         {          i--;          newline ();        }#v-   will cause the \var{newline} function to get called 10 times.   However, #v+      i = -10;      while (i)         {          i--;          newline ();        }#v-   would loop forever (or until \var{i} wraps from the most negative   integer value to the most positive and then decrements to zero).      If you are a C programmer, do not let the syntax of the language   seduce you into writing this example as you would in C:#v+      i = 10;      while (i--) newline ();#v-   The fact is that expressions such as \var{i--} do not return a   value in \slang as they do in C.  If you must write this way, use   the comma operator as in#v+      i = 10;      while (i, i--) newline ();#v-\sect2{do...while}   The \kw{do...while} statement follows the syntax\begin{tscreen}      do          \em{statement-or-block}      while (\em{integer-expression});\end{tscreen}   The main difference between this statement and the \var{while}  

⌨️ 快捷键说明

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