S-Lang supports a variety of operators that are grouped into three classes: assignment operators, binary operators, and unary operators.
An assignment operator is used to assign a value to a variable. They will be discussed more fully in the context of the assignment statement in section ???.
An unary operator acts only upon a single quantity while a binary
operation is an operation between two quantities. The boolean
operator not
is an example of an unary operator. Examples of
binary operators include the usual arithmetic operators
+
, -
, *
, and /
. The operator given by
-
can be either an unary operator (negation) or a binary operator
(subtraction); the actual operation is determined from the context
in which it is used.
Binary operators are used in algebraic forms, e.g., a + b
.
Unary operators fall in one of two classes: postfix-unary or
prefix-unary. For example, in the expression -x
, the minus
sign is a prefix-unary operator.
Not all data types have binary or unary operations defined. For
example, while String_Type
objects support the +
operator, they do not admit the *
operator.
The unary operators operate only upon a single operand. They
include: not
, ~
, -
, @
, &
, as well as the
increment and decrement operators ++
and --
,
respectively.
The boolean operator not
acts only upon integers and produces
0
if its operand is non-zero, otherwise it produces 1
.
The bit-level not operator ~
performs a similar function,
except that it operates on the individual bits of its integer
operand.
The arithmetic negation operator -
is the most well-known
unary operator. It simply reverses the sign of its operand.
The reference (&
) and dereference (@
) operators will be
discussed in greater detail in section ???. Similarly, the
increment (++
) and decrement (--
) operators will be
discussed in the context of the assignment operator.
The binary operators may be grouped according to several classes: arithmetic operators, relational operators, boolean operators, and bitwise operators.
All binary and unary operators may be overloaded. For example, the
arithmetic plus operator has been overloaded by the
String_Type
data type to permit concatenation between strings.
The arithmetic operators include +
, -
, *
, /
,
which perform addition, subtraction, multiplication, and division,
respectively. In addition to these, S-Lang supports the mod
operator as well as the power operator ^
.
The data type of the result produced by the use of one of these
operators depends upon the data types of the binary participants.
If they are both integers, the result will be an integer. However,
if the operands are not of the same type, they will be converted to
a common type before the operation is performed. For example, if
one is a floating point value and the other is an integer, the
integer will be converted to a float. In general, the promotion
from one type to another is such that no information is lost, if
possible. As an example, consider the expression 8/5
which
indicates division of the integer 8
by the integer 5
.
The result will be the integer 1
and not the floating
point value 1.6
. However, 8/5.0
will produce
1.6
because 5.0
is a floating point number.
The relational operators are >
, >=
, <
, <=
,
==
, and !=
. These perform the comparisons greater
than, greater than or equal, less than, less than or equal, equal,
and not equal, respectively. The result of one of these
comparisons is the integer 1
if the comparison is true, or
0
if the comparison is false. For example, 6 >= 5
returns 1
, but 6 == 5
produces
0
.
There are only two boolean binary operators: or
and
and
. These operators are defined only for integers and
produce an integer result. The or
operator returns 1
if either of its operands are non-zero, otherwise it produces
0
. The and
operator produces 1
if and only if
both its operands are non-zero, otherwise it produces 0
.
Neither of these operators perform the so-called boolean short-circuit evaluation. For example, consider the expression:
(x != 0) and (1/x > 10)
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
operator is not short-circuited and the 1/x
expression
would still be evaluated. Although these operators are not
short-circuited, S-Lang does have another mechanism of performing
short-circuit boolean evaluation via the orelse
and
andelse
expressions. See below for information about these
constructs.
The bitwise binary operators are defined only with integer operands
and are used for bit-level operations. Operators that fall in this
class include &
, |
, shl
, shr
, and
xor
. The &
operator performs a boolean AND operation
between the corresponding bits of the operands. Similarly, the
|
operator performs the boolean OR operation on the bits.
The bit-shifting operators shl
and shr
shift the bits
of the first operand by the number given by the second operand to
the left or right, respectively. Finally, the xor
performs
an EXCLUSIVE-OR operation.
These operators are commonly used to manipulate variables whose
individual bits have distinct meanings. In particular, &
is
usually used to test bits, |
can be used to set bits, and
xor
may be used to flip a bit.
As an example of using &
to perform tests on bits, consider
the following: The jed text editor stores some of the information
about a buffer in a bitmapped integer variable. The value of this
variable may be retrieved using the jed intrinsic function
getbuf_info
, which actually returns four quantities: the
buffer flags, the name of the buffer, directory name, and file
name. For the purposes of this section, only the buffer flags are
of interest and can be retrieved via a function such as
define get_buffer_flags ()
{
variable flags;
(,,,flags) = getbuf_info ();
return flags;
}
The buffer flags is a bitmapped quantity where the 0th bit
indicates whether or not the buffer has been modified, the first
bit indicates whether or not autosave has been enabled for the
buffer, and so on. Consider for the moment the task of determining
if the buffer has been modified. This can be
determined by looking at the zeroth bit, if it is 0
the
buffer has not been modified, otherwise it has. Thus we can create
the function,
define is_buffer_modified ()
{
variable flags = get_buffer_flags ();
return (flags & 1);
}
where the integer 1
has been used since it has all of its
bits set to 0
, except for the zeroth one, which is set to
1
. (At this point, it should also be apparent that bits are
numbered from zero, thus an 8
bit integer consists of bits
0
to 7
, where 0
is the least significant bit and
7
is the most significant one.) Similarly, we can create another
function
define is_autosave_on ()
{
variable flags = get_buffer_flags ();
return (flags & 2);
}
to determine whether or not autosave has been turned on for the
buffer.
The shl
operator may be used to form the integer with only
the nth bit set. For example, 1 shl 6
produces an
integer with all bits set to zero except the sixth bit, which is
set to one. The following example exploits this fact:
define test_nth_bit (flags, nth)
{
return flags & (1 shl nth);
}
The operator ->
is used to in conjunction with the name of a
namespace to access an object within the namespace. For example,
if A
is the name of a namespace containing the variable
v
, then A->v
refers to that variable.
Care must be exercised when using binary operators with an operand the returns multiple values. In fact, the current implementation of the S-Lang language will produce incorrect results if both operands of a binary expression return multiple values. At most, only one of operands of a binary expression can return multiple values, and that operand must be the first one, not the second. For example,
define read_line (fp)
{
variable line, status;
status = fgets (&line, fp);
if (status == -1)
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 values, 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.
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 appropriate, e.g.,
int (1.5) --> 1 (integer)
float(1.5) --> 1.5 (float)
float (1) --> 1.0 (float)
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
operator 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 ();