A Guide to the S-Lang Language: Overview of the Language

Next Previous Contents

## 3. Overview of the Language

This purpose of this section is to give the reader a feel for the S-Lang 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.

## 3.1 Variables and Functions

S-Lang 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 `variable` keyword, e.g.,

> `

>          variable x, y, z;

> `

declares three variables, `x`, `y`, and `z`. Note the semicolon at the end of the statement. All S-Lang statements must end in a semi-colon.

Unlike compiled languages such as C, it is not necessary to specify the data type of a S-Lang variable. The data type of a S-Lang variable is determined upon assignment. For example, after execution of the statements

> `

>          x = 3;
>          y = sin (5.6);
>          z = "I think, therefore I am.";

> `

`x` will be an integer, `y` will be a double, and `z` will be a string. In fact, it is even possible to re-assign `x` to a string:

> `

>          x = "x was an integer, but now is a string";

> `

Finally, one can combine variable declarations and assignments in the same statement:

> `

>          variable x = 3, y = sin(5.6), z = "I think, therefore I am.";

> `

Most functions are declared using the `define` keyword. A simple example is

> `

>           define compute_average (x, y)
>           {
>              variable s = x + y;
>              return s / 2.0;
>           }

> `

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 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:

> `

>           define go_left_5 ()
>           {
>              go_left (5);
>           }

> `

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 procedures. However, S-Lang, like C, does not make such a distinction.

The language permits recursive functions, i.e., functions that call themselves. The way to do this in S-Lang is to first declare the function using the form:

> `define function-name ();`

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

> `

>          define factorial ();   % declare it for recursion

>          define factorial (n)
>          {
>             if (n < 2) return 1;
>             return n * factorial (n - 1);
>          }

> `

This example also shows how to mix comments with code. S-Lang uses the ``%`' character to start a comment and all characters from the comment character to the end of the line are ignored.

## 3.2 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 S-Lang handles string variables with a lower level language such as C. Consider a function that concatenates three strings. An example in S-Lang is:

> `

>          define concat_3_strings (a, b, c)
>          {
>             return strcat (a, strcat (b, c));
>          }

> `

This function uses the built-in `strcat` function for concatenating two strings. In C, the simplest such function would look like:

> `

>          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;
>          }

> `

Even this C example is misleading since none of the issues of memory management of the strings has been dealt with. The S-Lang language hides all these issues from the user.

Binary operators have been defined to work with the string data type. In particular the `+` operator may be used to perform string concatenation. That is, one can use the `+` operator as an alternative to `strcat`:

> `

>           define concat_3_strings (a, b, c)
>           {
>              return a + b + c;
>           }

> `

See section ??? for more information about string variables.

## 3.3 Referencing and Dereferencing

The unary prefix operator, `&`, may be used to create a reference to an object, which is similar to a pointer in other languages. References are commonly used as a mechanism to pass a function as an argument to another function as the following example illustrates:

> `

>            define compute_functional_sum (funct)
>            {
>               variable i, s;

>               s = 0;
>               for (i = 0; i < 10; i++)
>                {
>                   s += (@funct)(i);
>                }
>               return s;
>            }

>            variable sin_sum = compute_functional_sum (&sin);
>            variable cos_sum = compute_functional_sum (&cos);

> `

Here, the function `compute_functional_sum` applies the function specified by the parameter `funct` to the first `10` integers and returns the sum. The two statements following the function definition show how the `sin` and `cos` functions may be used.

Note the `@` operator in the definition of `compute_functional_sum`. It is known as the dereference operator and is the inverse of the reference operator.

Another use of the reference operator is in the context of the `fgets` function. For example,

> `

>           define read_nth_line (file, n)
>           {
>              variable fp, line;
>              fp = fopen (file, "r");

>              while (n > 0)
>                {
>                   if (-1 == fgets (&line, fp))
>                     return NULL;
>                   n--;
>                }
>              return line;
>           }

> `

uses the `fgets` function to read the nth line of a file. In particular, a reference to the local variable `line` is passed to `fgets`, and upon return `line` will be set to the character string read by `fgets`.

Finally, references may be used as an alternative to multiple return values by passing information back via the parameter list. The example involving `fgets` presented above provided an illustration of this. Another example is

> `

>            define set_xyz (x, y, z)
>            {
>               @x = 1;
>               @y = 2;
>               @z = 3;
>            }
>            variable X, Y, Z;
>            set_xyz (&X, &Y, &Z);

> `

which, after execution, results in `X` set to `1`, `Y` set to `2`, and `Z` set to `3`. A C programmer will note the similarity of `set_xyz` to the following C implementation:

> `

>           void set_xyz (int *x, int *y, int *z)
>           {
>              *x = 1;
>              *y = 2;
>              *z = 3;
>           }

> `

## 3.4 Arrays

The S-Lang language supports multi-dimensional arrays of all datatypes. For example, one can define arrays of references to functions as well as arrays of arrays. Here are a few examples of creating arrays:

> `

>            variable A = Integer_Type [10];
>            variable B = Integer_Type [10, 3];
>            variable C = [1, 3, 5, 7, 9];

> `

The first example creates an array of `10` integers and assigns it to the variable `A`. The second example creates a 2-d array of `30` integers arranged in `10` rows and `3` columns and assigns the result to `B`. In the last example, an array of `5` integers is assigned to the variable `C`. However, in this case the elements of the array are initialized to the values specified. This is known as an inline-array.

S-Lang also supports something called an range-array. An example of such an array is

> `

>           variable C = [1:9:2];

> `

This will produce an array of 5 integers running from `1` through `9` in increments of `2`.

Arrays are passed by reference to functions and never by value. This permits one to write functions which can initialize arrays. For example,

> `

>           define init_array (a)
>           {
>              variable i, imax;

>              imax = length (a);
>              for (i = 0; i < imax; i++)
>                {
>                   a[i] = 7;
>                }
>           }

>           variable A = Integer_Type [10];
>           init_array (A);

> `

creates an array of `10` integers and initializes all its elements to `7`.

There are more concise ways of accomplishing the result of the previous example. These include:

> `

>           variable A = [7, 7, 7, 7, 7, 7, 7, 7, 7, 7];
>           variable A = Integer_Type [10];  A[[0:9]] = 7;
>           variable A = Integer_Type [10];  A[*] = 7;

> `

The second and third methods use an array of indices to index the array `A`. In the second, the range of indices has been explicitly specified, whereas the third example uses a wildcard form. See section ??? for more information about array indexing.

Although the examples have pertained to integer arrays, the fact is that S-Lang arrays can be of any type, e.g.,

> `

>            variable A = Double_Type [10];
>            variable B = Complex_Type [10];
>            variable C = String_Type [10];
>            variable D = Ref_Type [10];

> `

create `10` element arrays of double, complex, string, and reference types, respectively. The last example may be used to create an array of functions, e.g.,

> `

>           D[0] = &sin;
>           D[1] = &cos;

> `

The language also defines unary, binary, and mathematical operations on arrays. For example, if `A` and `B` are integer arrays, then `A + B` is an array whose elements are the sum of the elements of `A` and `B`. A trivial example that illustrates the power of this capability is

> `

>             variable X, Y;
>             X = [0:2*PI:0.01];
>             Y = 20 * sin (X);

> `

which is equivalent to the highly simplified C code:

> `

>             double *X, *Y;
>             unsigned int i, n;

>             n = (2 * PI) / 0.01 + 1;
>             X = (double *) malloc (n * sizeof (double));
>             Y = (double *) malloc (n * sizeof (double));
>             for (i = 0; i < n; i++)
>               {
>                 X[i] = i * 0.01;
>                 Y[i] = 20 * sin (X[i]);
>               }

> `

## 3.5 Structures and User-Defined Types

A structure is similar to an array in the sense that it is a container object. However, the elements of an array must all be of the same type (or of `Any_Type`), whereas a structure is heterogeneous. As an example, consider

> `

>           variable person = struct
>           {
>              first_name, last_name, age
>           };
>           variable bill = @person;
>           bill.first_name = "Bill";
>           bill.last_name = "Clinton";
>           bill.age = 51;

> `

In this example a structure consisting of the three fields has been created and assigned to the variable `person`. Then an instance of this structure has been created using the dereference operator and assigned to `bill`. Finally, the individual fields of `bill` were initialized. This is an example of an anonymous structure.

A named structure is really a new data type and may be created using the `typedef` keyword:

> `

>           typedef struct
>           {
>              first_name, last_name, age
>           }
>           Person_Type;

>           variable bill = @Person_Type;
>           bill.first_name = "Bill";
>           bill.last_name = "Clinton";
>           bill.age = 51;

> `

The big advantage of creating a new type is that one can go on to create arrays of the data type

> `

>           variable People = Person_Type [100];
>           People[0].first_name = "Bill";
>           People[1].first_name = "Hillary";

> `

The creation and initialization of a structure may be facilitated by a function such as

> `

>           define create_person (first, last, age)
>           {
>               variable person = @Person_Type;
>               person.first_name = first;
>               person.last_name = last;
>               person.age = age;
>               return person;
>           }
>           variable Bill = create_person ("Bill", "Clinton", 51);

> `

Other common uses of structures is the creation of linked lists, binary trees, etc. For more information about these and other features of structures, see section ???.

## 3.6 Namespaces

In addition to the global namespace, each compilation unit (e.g., a file) is given a private namespace. A variable or function name that is declared using the `static` keyword will be placed in the private namespace associated with compilation unit. For example,

> `

>            variable i;
>            static variable i;

> `

defines two variables called `i`. The first declaration defines `i` in the global namespace, but the second declaration defines `i` in the private namespace.

The `->` operator may be used in conjunction with the name of the namespace to access objects in the name space. In the above example, to access the variable `i` in the global namespace, one would use `Global->i`. Unless otherwise specified, a private namespace has no name and its objects may not be accessed from outside the compilation unit. However, the `implements` function may be used give the private namespace a name, allowing access to its objects. For example, if the file `t.sl` contains

> `

>           implements ("A");
>           static variable i;

> `

then another file may access the variable `i` via `A->i`.

Next Previous Contents