⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 slang.txt

📁 一个C格式的脚本处理函数库源代码,可让你的C程序具有执行C格式的脚本文件
💻 TXT
📖 第 1 页 / 共 5 页
字号:
                return -1;              return (line, status);           }  defines a function, 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 fgets.  Now consider               while (read_line (fp) > 0)                 {                    text = ();                    % Do something with text                    .                    .                 }  Here the relational binary operator > forms a comparison between one  of the return values (the one at the top of the stack) and 0.  In  accordance with the above rule, since read_line returns multiple val-  ues, it occurs as the left binary operand.  Putting it on the right as  in               while (0 < read_line (fp))    % Incorrect                 {                    text = ();                    % Do something with text                    .                    .                 }  violates the rule and will result in the wrong answer.  7.3.  Mixing Integer and Floating Point Arithmetic  If a binary operation (+, -, * , /) 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:             11 / 2           --> 5   (integer)             11 / 2.0         --> 5.5 (float)             11.0 / 2         --> 5.5 (float)             11.0 / 2.0       --> 5.5 (float)  Finally note that only integers may be used as array indices, loop  control variables, and bit operations.  The conversion functions, int  and float, may be used convert between floats and ints where appropri-  ate, e.g.,             int (1.5)         --> 1 (integer)             float(1.5)        --> 1.5 (float)             float (1)         --> 1.0 (float)  7.4.  Short Circuit Boolean Evaluation  The boolean operators or and and are not short circuited as they are  in some languages.  S-Lang uses orelse and andelse expressions for  short circuit boolean evaluation.  However, these are not binary  operators. Expressions of the form:       expr-1 and expr-2 and ... expr-n  can be replaced by the short circuited version using andelse:       andelse {expr-1} {expr-2} ... {expr-n}  A similar syntax holds for the orelse operator.  For example, consider  the statement:             if ((x != 0) and (1/x > 10)) do_something ();  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 be evaluated  causing division by zero. For this case, the andelse expression could  be used to avoid the problem:             if (andelse                 {x != 0}                 {1 / x > 10})  do_something ();  8.  Statements  Loosely speaking, a statement is composed of 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 S-Lang 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.  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.  8.1.  Variable Declaration Statements  Variable declarations were already discussed in chapter ???.  For the  sake of completeness, a variable declaration is a statement of the  form       variable variable-declaration-list ;  where the variable-declaration-list is a comma separated list of one  or more variable names with optional initializations, e.g.,            variable x, y = 2, z;  8.2.  Assignment Statements  Perhaps the most well known form of statement is the 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 lvalue.  The most common assignment operator is the simple assignment operator  =.  Simple of its use include             x = 3;             x = some_function (10);             x = 34 + 27/y + some_function (z);             x = x + 3;  In addition to the simple assignment operator, S-Lang also supports  the assignment operators += and -=.  Internally, S-Lang transforms              a += b;  to              a = a + b;  Similarly, a -= b is transformed to a = a - b.  It is extremely impor-  tant to realize that, in general, a+b is not equal to b+a.  This means  that a+=b is not the same as a=b+a.  As an example consider             a = "hello"; a += "world";  After execution of these two statements, a will have the value "hel-  loworld" and not "worldhello".  Since adding or subtracting 1 from a variable is quite common, S-Lang  also supports the unary increment and decrement operators ++, and --,  respectively.  That is, for numeric data types,              x = x + 1;              x += 1;              x++;  are all equivalent.  Similarly,              x = x - 1;              x -= 1;              x--;  are also equivalent.  Strictly speaking, ++ and -- are unary operators.  When used as x++,  the ++ operator is said to be a postfix-unary operator.  However, when  used as ++x it is said to be a prefix-unary operator.  The current  implementation does not distinguish between the two forms, thus x++  and ++x are equivalent.  The reason for this equivalence is that  assignment expressions do not return a value in the S-Lang language as  they do in C.  Thus one should exercise care and not try to write C-  like code such as             x = 10;             while (--x) do_something (x);     % Ok in C, but not in S-Lang  The closest valid S-Lang form involves a comma-expression:        x = 10;        while (x--, x) do_something (x);  % Ok in S-Lang and in C  S-Lang also supports a multiple-assignment statement.  It is discussed  in detail in section ???.  8.3.  Conditional and Looping Statements  S-Lang supports a wide variety of conditional and looping statements.  These constructs operate on statements grouped together in blocks.  A  block is a sequence of S-Lang statements enclosed in braces and may  contain other blocks. However, a block cannot include function  declarations.  In the following, statement-or-block refers to either a  single S-Lang statement or to a block of statements, and integer-  expression is an integer-valued expression.  next-statement represents  the statement following the form under discussion.  8.3.1.  Conditional Forms  8.3.1.1.  if  The simplest condition statement is the if statement.  It follows the  syntax       if (integer-expression) statement-or-block next-statement  If integer-expression evaluates to a non-zero result, then the state-  ment or group of statements implied statement-or-block will get exe-  cuted.  Otherwise, control will proceed to next-statement.  An example of the use of this type of conditional statement is              if (x != 0)                {                   y = 1.0 / x;                   if (x > 0) z = log (x);                }  This example illustrates two if statements where the second if state-  ment is part of the block of statements that belong to the first.  8.3.1.2.  if-else  Another form of if statement is the if-else statement.  It follows the  syntax:       if (integer-expression) statement-or-block-1 else statement-or-block-2       next-statement  Here, if expression returns non-zero, statement-or-block-1 will get  executed and control will pass on to next-statement. However, if  expression returns zero, statement-or-block-2 will get executed before  continuing with next-statement.  A simple example of this form is            if (x > 0) z = log (x); else error ("x must be positive");  Consider the more complex example:            if (city == "Boston")              if (street == "Beacon") found = 1;            else if (city == "Madrid")              if (street == "Calle Mayor") found = 1;            else found = 0;  This example illustrates a problem that beginners have with if-else  statements.  The grammar presented above shows that the this example  is equivalent to            if (city == "Boston")              {                if (street == "Beacon") found = 1;                else if (city == "Madrid")                  {                    if (street == "Calle Mayor") found = 1;                    else found = 0;                  }              }  It is important to understand the grammar and not be seduced by the  indentation!  8.3.1.3.  !if  One often encounters if statements similar to       if (integer-expression == 0) statement-or-block  or equivalently,       if (not(integer-expression)) statement-or-block  The !if statement was added to the language to simplify the handling  of such statements.  It obeys the syntax       !if (integer-expression) statement-or-block  and is functionally equivalent to  if (not (expression)) statement-or-block  8.3.1.4.  orelse, andelse  These constructs were discussed earlier.  The syntax for the orelse  statement is:       orelse {integer-expression-1} ... {integer-expression-n}  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 exam-  ple,            orelse { 0 } { 6 } { 2 } { 3 }  returns 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 andelse statement is:       andelse {integer-expression-1} ... {integer-expression-n}  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,            andelse { 6 } { 2 } { 0 } { 4 }  returns 0 since the third block will be the last to execute.  8.3.1.5.  switch  The switch statement deviates the most from its C counterpart.  The  syntax is:                 switch (x)                   { ...  :  ...}                     .                     .                   { ...  :  ...}  The `:' 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 stateme

⌨️ 快捷键说明

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