Next Previous Contents

2. Introduction

S-Lang 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.

2.1 slsh -- The S-Lang shell

The S-Lang distribution contains a standalone application called slsh that may be used for writing S-Lang scripts and full-blown S-Lang based applications. For example, the author has used slsh to create a mediacenter for his home entertainment system that integrates internet radio and tv, podcasts, digital pictures and video, CDs, and so forth. The use of slsh in such non-interactive modes is discussed in the chapter on slsh.

slsh also may be used interactively and has full access to all components of the S-Lang interpreter. With features such as customizable command-line editing, history recall and completion, slsh is a convenient environment for learning and using the language. In fact, as you are reading this manual, it is recommended that you use slsh in its interactive mode as an aid to understanding the language.

While a standard S-Lang installation includes slsh, some some binary distributions package slsh separately from the S-Lang library, and as such must be installed separately. For example, on Debian Linux it can be installed via

    apt-get install slsh

When called without arguments, slsh will start in interactive mode by issuing a (customizable) slsh> prompt and waits for input. While most of the time one would enter S-Lang statements at the prompt, slsh also accepts some other commands, most notably help:

   slsh> help
   Most commands must end in a semi-colon.
   If a command begins with '!', then the command is passed to the shell.
   Examples: !ls, !pwd, !cd foo, ...
   Special commands:
     help <help-topic>
     apropos <something>
     start_log( <optional-log-file> );
       start logging input to a file (default is slsh.log)
     stop_log();
       stop logging input
     save_input (<optional-file>);
       save all previous input to a file (default: slsh.log)
     quit;

Although the language normally requires variables to be declared before use, it is not necessary to do so when using slsh interactively. For example, in this document you will see examples such as

    variable x = [1:10];
    variable y = sin (x^2);
At the slsh command line, the use of the variable keyword in such statements is optional:
    slsh> x = [1:10]; y = sin(x^2);

As the above example suggests, one use of slsh is as a sophisticated calculator. For example,

    slsh> sin (1.24) + 3*cos (1.3*PI);
    -0.817572
This is especially true when combined with modules, e.g.,
    slsh> require ("fits");
    slsh> require ("histogram");
    slsh> tbl = fit_read_table ("evt1a.fits");
    slsh> engrid = [min(tbl.energy):max(energy):#1024];
    slsh> spectrum = hist1d (tbl.energy[where(tbl.status==0)], engrid);
In this example, the fits module was used to read data from a binary file called evt1a.fits, and the histogram module was used to bin the data in the energy column into a histogram to create a spectrum. The expression involving where filters the data by accepting only those energy values whose status is set to 0. The fits and histogram modules are not distributed with S-Lang but may be obtained separately-- see http://www.jedsoft.org/slang/modules/ for links to them. For more information about modules, see the Modules chapter in this document.

For more information about using slsh, see the chapter on slsh.

2.2 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.

2.3 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, lists, and associative arrays. To facilitate the construction of sophisticated data structures such as linked lists and trees, the language also includes a ``reference'' type. 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 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, including user-defined types. For example, the addition operator (+) 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 a and b are arrays, then a + b produces an array whose elements are the result of element by element addition of a and b. This permits one to do vector operations without explicitly looping over the array indices.

2.4 Statements and Functions

The S-Lang language supports several types of looping constructs and conditional statements. The looping constructs include while, do...while, for, forever, loop, foreach, and _for. The conditional statements include if, if-then-else, and ifnot.

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 reference data type that allows one to simulate pass by reference.

Unlike many interpreted languages, S-Lang 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 stem from the C language and should be quite familiar to any one familiar with C.

2.5 Error Handling

The S-Lang language has a try/throw/catch/finally exception model whose semantics are similar to that of other languages. Users may also extend the exception class hierarchy with user-defined exceptions. The ERROR_BLOCK based exception model of S-Lang 1.x is still supported but deprecated.

2.6 Run-Time Library

Functions that compose the S-Lang run-time library are called intrinsics. Examples of S-Lang intrinsic functions available to every S-Lang application include string manipulation functions such as strcat, strchop, and strcmp. The S-Lang library also provides mathematical functions such as sin, cos, and 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.

Operating systems that support dynamic linking allow a slang interpreter to dynamically link additional libraries of intrinsic functions and variables into the interpreter. Such loadable objects are called modules. A separate chapter of this manual is devoted to this important feature.

2.7 Input/Output

The language supports C-like stdio input/output functions such as fopen, fgets, fputs, and fclose. In addition it provides two functions, message and 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.

2.8 Obtaining more information about S-Lang

Comprehensive information about the library may be obtained via the World Wide Web from http://www.jedsoft.org/slang/. In particular see http://www.jedsoft.org/slang/download.html for downloading the latest version of the library.

Users with generic questions about the interpreter are encouraged to post questions to the Usenet newsgroup alt.lang.s-lang. More specific questions relating to the use of S-Lang within some application may be better answered in an application-specific forum. For example, users with questions about using S-Lang as embedded in the jed editor are more likely to be answered in the comp.editors newsgroup or on the jed mailing list. Similarly users with questions concerning slrn will find news.software.readers to be a valuable source of information.

Developers who have embedded the interpreter are encouraged to join the S-Lang mailing list. To subscribe to the list or just browse the archives, visit http://www.jedsoft.org/slang/mailinglists.html.


Next Previous Contents