S-lang

Appeared in:
1992
Influenced by:
Influenced:
Paradigm:
Typing discipline:
File extensions:
.sl
Versions and implementations (Collapse all | Expand all):
Programming language

S-lang is a scripting programming language. It is stack-oriented and provides support for array-based operations.

Its interpreter is meant to be embedded in other software, but it can also be used as a stand-alone interpreter slsh.

Elements of syntax:

Inline comments %
Variable assignment varname = value
Variable declaration with assignment varname = vaue
Grouping expressions ( ... )
Block { ... }
Comparison < > <= >=
Function definition define functionName (argname1, ..., argnameN)
Function call functionName(arg1, ..., argN)
Sequence ;
If - then if (condition) trueBlock
For each value in a numeric range, 1 increment for (int i = first; i <= last ; i++) loopBody
For each value in a numeric range, 1 decrement for (int i = last; i >= first; i--) loopBody

Examples:

Hello, World!:

Example for versions S-lang 2.2.2

This example should be executed in slsh interpreter.

message ("Hello, World!");

Factorial:

Example for versions S-lang 2.2.2

This example uses recursive factorial definition and features the usage of recursive functions in S-lang. To create a recursive function, you have first to declare it without listing parameters or implementation — define factorial ();, and then actually define it, with the list of parameters and function body.

Another feature included in this example is usage of $ suffix. A string literal can end with a suffix which defines the way of processing this string. $ suffix requires that variable name substitution is performed on the string before using it. This means that each variable name prefixed by $ character within the string will be replaced with variable value. Variable name can be enclosed in {} braces to separate it from the string contents; the braces can be omitted if variable name is followed by a space.

Note that the default numeric data type in S-lang is integer, so the given example produces overflow error when calculating 13!.

define factorial ();

define factorial (n)
{   if (n==0) return 1;
     return n * factorial (n-1);
};

for (i=0; i<17; i++)
{   f = factorial (i);
    message ("${i}! = ${f}"$);
};

Factorial:

Example for versions S-lang 2.2.2

This example shows array operations in S-lang. [1:i] creates a list of numbers from 1 to i; note that the default data type for array operations is double even if the numbers are integer. Intrinsic function prod (since version 2.1) returns the product of elements of its parameter. Intrinsic function sprintf provides C-style printing, %.0f printing floating point number with zero digits after decimal comma.

for (i=0; i<17; i++)
    sprintf ("%d! = %.0f", i, prod ( [1:i] ) );

Fibonacci numbers:

Example for versions S-lang 2.2.2

This example uses iterative definition of Fibonacci numbers and shows some more features of array processing in S-lang. f is explicitly declared as an array of 16 integer numbers. Elements 0 and 1 of f are set to 1; here [0:1] creates a list of indices to which the operation is applied. Intrinsic function string converts its argument to its string representation.

f = Integer_Type [16];
f[[0:1]] = 1;
for (i=2; i<16; i++)
    f[i] = f[i-1] + f[i-2];
s = "...";
for (i=15; i>=0; i--)
    s = string(f[i]) + ", " + s;
message (s);