slang.tm
来自「一个C格式的脚本处理函数库源代码,可让你的C程序具有执行C格式的脚本文件」· TM 代码 · 共 1,763 行 · 第 1/5 页
TM
1,763 行
#% -*- mode: tm; mode: fold -*-#% text-macro definitions #%{{{#i linuxdoc.tm#d slang \bf{S-Lang}#d slrn \bf{slrn}#d jed \bf{jed}#d kw#1 \tt{$1}#d exmp#1 \tt{$1}#d var#1 \tt{$1}#d ldots ...#d times *#d math#1 $1#d sc#1 \tt{$1}#d verb#1 \tt{$1}#d sldxe \bf{sldxe}#d url#1 <htmlurl url="$1" name="$1">#d slang-library-reference \bf{The \slang Library Reference}#d chapter#1 <chapt>$1<p>#d preface <preface>#d tag#1 <tag>$1</tag>#d appendix <appendix>#d NULL <tt>NULL</tt>#d kbd#1 <tt>$1</tt>#d documentstyle book#%}}}\linuxdoc\begin{\documentstyle}\title A Guide to the S-Lang Language\author John E. Davis, \tt{davis@space.mit.edu}\date \__today__\toc#i preface.tm\chapter{Introduction} #%{{{ \slang is a powerful interpreted language that may be embedded into an application to make the application extensible. This enables the application to be used in ways not envisioned by the programmer, thus providing the application with much more flexibility and power. Examples of applications that take advantage of the interpreter in this way include the \jed editor and the \slrn newsreader.\sect{Language Features} The language features both global and local variables, branching and looping constructs, user-defined functions, structures, datatypes, and arrays. In addition, there is limited support for pointer types. The concise array syntax rivals that of commercial array-based numerical computing environments.\sect{Data Types and Operators} #%{{{ The language provides built-in support for string, integer (signed and unsigned long and short), double precision floating point, and double precision complex numbers. In addition, it supports user defined structure types, multi-dimensional array types, and associative arrays. To facilitate the construction of sophisticated data structures such as linked lists and trees, a `reference' type was added to the language. The reference type provides much of the same flexibility as pointers in other languages. Finally, applications embedding the interpreter may also provide special application specific types, such as the \var{Mark_Type} that the \jed editor provides. The language provides standard arithmetic operations such as addition, subtraction, multiplication, and division. It also provides support for modulo arithmetic as well as operations at the bit level, e.g., exclusive-or. Any binary or unary operator may be extended to work with any data type. For example, the addition operator (\var{+}) has been extended to work between string types to permit string concatenation. The binary and unary operators work transparently with array types. For example, if \var{a} and \var{b} are arrays, then \exmp{a + b} produces an array whose elements are the result of element by element addition of \var{a} and \var{b}. This permits one to do vector operations without explicitly looping over the array indices.#%}}}\sect{Statements and Functions} #%{{{ The \slang language supports several types of looping constructs and conditional statements. The looping constructs include \kw{while}, \kw{do...while}, \kw{for}, \kw{forever}, \kw{loop}, \kw{foreach}, and \kw{_for}. The conditional statements include \kw{if}, \kw{if-then-else}, and \kw{!if}. User defined functions may be defined to return zero, one, or more values. Functions that return zero values are similar to `procedures' in languages such as PASCAL. The local variables of a function are always created on a stack allowing one to create recursive functions. Parameters to a function are always passed by value and never by reference. However, the language supports a \em{reference} data type that allows one to simulate pass by reference. Unlike many interpreted languages, \slang allows functions to be dynamically loaded (function autoloading). It also provides constructs specifically designed for error handling and recovery as well as debugging aids (e.g., function tracebacks). Functions and variables may be declared as private belonging to a namespace associated with the compilation unit that defines the function or variable. The ideas behind the namespace implementation stems from the C language and should be quite familiar to any one familiar with C.#%}}}\sect{Error Handling} #%{{{ The \slang language defines a construct called an \em{error-block} that may be used for error handling and recovery. When a non-fatal run-time error is encountered, any error blocks that have been defined are executed as the run-time stack unwinds. An error block can optionally clear the error and the program will continue running after the statement that triggered the error. This mechanism is somewhat similar to try-catch in C++.#%}}}\sect{Run-Time Library} #%{{{ Functions that compose the \slang run-time library are called \em{intrinsics}. Examples of \slang intrinsic functions available to every \slang application include string manipulation functions such as \var{strcat}, \var{strchop}, and \var{strcmp}. The \slang library also provides mathematical functions such as \var{sin}, \var{cos}, and \var{tan}; however, not all applications enable the use of these intrinsics. For example, to conserve memory, the 16 bit version of the \jed editor does not provide support for any mathematics other than simple integer arithmetic, whereas other versions of the editor do support these functions. Most applications embedding the languages will also provide a set of application specific intrinsic functions. For example, the \jed editor adds over 100 application specific intrinsic functions to the language. Consult your application specific documentation to see what additional intrinsics are supported.#%}}}\sect{Input/Output} The language supports C-like stdio input/output functions such as \var{fopen}, \var{fgets}, \var{fputs}, and \var{fclose}. In addition it provides two functions, \var{message} and \var{error}, for writing to the standard output device and standard error. Specific applications may provide other I/O mechanisms, e.g., the \jed editor supports I/O to files via the editor's buffers.\sect{Obtaining \slang} #%{{{ Comprehensive information about the library may be obtained via the World Wide Web from \tt{http://www.s-lang.org}. \slang as well as some programs that embed it are freely available via anonymous ftp in the United States from\begin{itemize} \item \url{ftp://space.mit.edu/pub/davis}.\end{itemize} It is also available outside the United States from the following mirror sites:\begin{itemize} \item \url{ftp://ftp.uni-stuttgart.de/pub/unix/misc/slang/} \item \url{ftp://ftp.fu-berlin.de/pub/unix/news/slrn/} \item \url{ftp://ftp.ntua.gr/pub/lang/slang/}\end{itemize} The Usenet newsgroup \var{alt.lang.s-lang} was created for \slang programmers to exchange information and share macros for the various programs the embed the language. The newsgroup \var{comp.editors} can be a useful resource for \slang macros for the \jed editor. Similarly, \slrn users will find \var{news.software.readers} to be a valuable source of information. Finally, two mailing lists dealing with the \slang library have been created: \begin{itemize} \item \tt{slang-announce@babayaga.math.fu-berlin.de} \item \tt{slang-workers@babayaga.math.fu-berlin.de}\end{itemize} The first list is for announcements of new releases of the library, while the second list is intended for those who use the library for their own code development. To subscribe to the announcement list, send an email to \tt{slang-announce-subscribe@babayaga.math.fu-berlin.de} and include the word \tt{subscribe} in the body of the message. To subscribe to the developers list, use the address \tt{slang-workers-subscribe@babayaga.math.fu-berlin.de}.#%}}}#%}}}\chapter{Overview of the Language} #%{{{ This purpose of this section is to give the reader a feel for the \slang language, its syntax, and its capabilities. The information and examples presented in this section should be sufficient to provide the reader with the necessary background to understand the rest of the document.\sect{Variables and Functions} #%{{{ \slang is different from many other interpreted languages in the sense that all variables and functions must be declared before they can be used. Variables are declared using the \kw{variable} keyword, e.g.,#v+ variable x, y, z;#v- declares three variables, \var{x}, \var{y}, and \var{z}. Note the semicolon at the end of the statement. \em{All \slang statements must end in a semi-colon.} Unlike compiled languages such as C, it is not necessary to specify the data type of a \slang variable. The data type of a \slang variable is determined upon assignment. For example, after execution of the statements#v+ x = 3; y = sin (5.6); z = "I think, therefore I am.";#v- \var{x} will be an integer, \var{y} will be a double, and \var{z} will be a string. In fact, it is even possible to re-assign \var{x} to a string:#v+ x = "x was an integer, but now is a string";#v- Finally, one can combine variable declarations and assignments in the same statement:#v+ variable x = 3, y = sin(5.6), z = "I think, therefore I am.";#v- Most functions are declared using the \kw{define} keyword. A simple example is#v+ define compute_average (x, y) { variable s = x + y; return s / 2.0; }#v- which defines a function that simply computes the average of two numbers and returns the result. This example shows that a function consists of three parts: the function name, a parameter list, and the function body. The parameter list consists of a comma separated list of variable names. It is not necessary to declare variables within a parameter list; they are implicitly declared. However, all other \em{local} variables used in the function must be declared. If the function takes no parameters, then the parameter list must still be present, but empty:#v+ define go_left_5 () { go_left (5); }#v- The last example is a function that takes no arguments and returns no value. Some languages such as PASCAL distinguish such objects from functions that return values by calling these objects \em{procedures}. However, \slang, like C, does not make such a distinction. The language permits \em{recursive} functions, i.e., functions that call themselves. The way to do this in \slang is to first declare the function using the form:\begin{tscreen} define \em{function-name} ();\end{tscreen} It is not necessary to declare a parameter list when declaring a function in this way. The most famous example of a recursive function is the factorial function. Here is how to implement it using \slang:#v+ define factorial (); % declare it for recursion define factorial (n) { if (n < 2) return 1; return n * factorial (n - 1); }#v- This example also shows how to mix comments with code. \slang uses the `\var{%}' character to start a comment and all characters from the comment character to the end of the line are ignored.#%}}}\sect{Strings} #%{{{ Perhaps the most appealing feature of any interpreted language is that it frees the user from the responsibility of memory management. This is particularly evident when contrasting how \slang handles string variables with a lower level language such as C. Consider a function that concatenates three strings. An example in \slang is:#v+ define concat_3_strings (a, b, c) { return strcat (a, strcat (b, c)); }#v- This function uses the built-in \var{strcat} function for concatenating two strings. In C, the simplest such function would look like:#v+ char *concat_3_strings (char *a, char *b, char *c) { unsigned int len; char *result; len = strlen (a) + strlen (b) + strlen (c); if (NULL == (result = (char *) malloc (len + 1))) exit (1); strcpy (result, a); strcat (result, b); strcat (result, c); return result; }#v- Even this C example is misleading since none of the issues of memory management of the strings has been dealt with. The \slang language hides all these issues from the user. Binary operators have been defined to work with the string data type. In particular the \var{+} operator may be used to perform string concatenation. That is, one can use the \var{+} operator as an alternative to \var{strcat}:#v+ define concat_3_strings (a, b, c) { return a + b + c; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?