Expressions Scripting
DESCRIPTION
Expressions in the JANOS scripting language combine operands, operators and
variables and when fully evaluated result in a value or string. An expression
is evaluated from left to right in accordance with a defined Order of
Precedence. Once processed the result may be stored in a variable, output,
or applied in a comparison.
The operators can perform arithmetic or logic operations. Some operators
involve two operands, a left and right. Others may be unary and affect the
handling of the operand either to the left (suffix operator) or right (prefix
operator).
ARITHMETIC OPERATORS
Some of the simplest expressions manipulate numbers in performing a
calculation. The obvious being the four operations:
'+' addition: 2 + 2 = 4
'-' subtraction: 8 - 5 = 3
'*' multiplication: 2 * 3 = 6
'/' division: 12 / 4 = 3
Common but less obvious are the modulo and exponentiation operations.
'%' modulo: 12 % 5 = 2
'**' exponentiation: 3 2 = 9
Modulo arithmetic is useful in many situations when employed creatively. This
returns the remainder or signed remainder of the division. The '%' operator
does this with integers and returns an integer remainder. The fmod()
function described later can be used with floating point (double) values.
Exponentiation, taking a number to a power, such as squaring can be performed
with the '**' operator. This is a shortcut for the pow() function to be
described later. This operator is unique to JANOS scripting as it is not
part of the PHP language. It is used in languages like Python and Basic. The
'^' operator may be written in texts to indicate superscript and the raising
of a number to some power but in this, and many languages, it is reserved
for a logical operation.
UNARY ARITHMETIC OPERATORS
A unary operation involves just one operand either that immediately before
or after the operator. There are only few such unary operations and they
are typically a kind of shorthand.
'++' increment by 1
'--' decrement by 1
These are applicable only to variable values in that their use not only
returns the incremented or decremented value but also alters the variable's
value for the future. They can be used either as a prefix or suffix. As a
prefix the operation increments the variable and returns the incremented
value for use. But as a suffix the variable's value is returned for use
but then incremented as stored for future reference.
++$a returns $a + 1 and the value of $a is then
also incremented. Note the sequence:
$a = $a + 1;
echo $a;
$a++ returns $a but increases $a by 1 for its
next use. Note the sequence:
echo $a;
$a = $a + 1;
There are additional unary operations that relate to bitwise logical
operations or variable type casting. In these cases they are only valid
as a prefix to their operand and are not limited to variables. Those will
be detailed in the sections that follow.
LOGICAL OPERATORS
A variable not only represents a number (either integer or double), it may
also take on a boolean value. There are only the TRUE or FALSE values
in that case. Boolean values are critical in handling the result of numeric
comparisons and in logical expressions. A boolean value is set using those
two keywords (not case-dependent).
$perfect = TRUE;
$sloppy = FALSE;
There are four (4) basic logical operations: AND, OR, XOR and NOT. The first
three being binary operations takin two operands and the last, NOT, being
a unary prefix.
The AND logical operation returns TRUE only when both operands are also
TRUE. If either operand is FALSE then the result of the AND is FALSE. Both
the 'and' and '&&' operators are used for this logic. You may code the
operation either way although, as we will see in a moment, there is a
difference in the Order of Precedence.
true and true = true
true and false = false
false && true = false
false && false = false
Similarly there is the OR logical operation which returns TRUE when either
of its operands is also TRUE. The OR is FALSE when both operands are FALSE.
The 'or' may also be coded as '||' with two vertical bars.
true or true = true
true or false = true
false || true = true
false || false = false
The XOR logical operation performs the exclusive_or which returns TRUE
only when both operands differ.
true xor true = false
true xor false = true
false xor true = true
false xor false = false
There is no alternative form (such as '&&') for this XOR operation. The '^'
operator performs a bitwise logical exclusive-or however that operates on
numeric (integer) values as we will see shortly.
The NOT operation uses the '!' operator to invert a boolean value. In this
case there is no 'not' form and the exclamation point must be applied as a
prefix on the value to be inverted.
!true = false
!false = true
$a = false;
echo !$a;
TRUE
Boolean variables store either TRUE or FALSE. These have equivalent integer
values of 1 and 0 respectively. In fact any non-zero numeric value is
considered equivalent to TRUE. Any 0 value is considered as FALSE. This permits
mixed variable types to be used in logical expressions. Even a string
representation of a value may be used. Therefore:
echo "1" || false;
TRUE
The AND '&&'" and OR '||' operators are executed carefully in that the left
hand operand is evaluated and if that determines the outcome of the logical
operation the right hand operand IS NOT executed. This is important to know
if the right hand operand is an expression that might modify variables either
using the inc/dec unary operators or through a function call. For example
consider the difference here:
$a = 0;
echo false && $a++;
FALSE
echo $a;
0
$a = 0;
echo true && $a++;
FALSE
echo $a;
1
$a = 0;
echo true && ++$a;
TRUE
echo $a;
1
In the first example the right hand operand $a+ is not evaluated and the
value of $a is not altered. This is because the outcome of the AND operation
is false regardless of the right hand expression. However in the next two
examples the right hand operand must be evaluated to determine the result of
the logical operation. Here we show the difference in the increment operator
used first as a suffix and then as a prefix. $a is altered in both cases but
only when the incremented value is used with the expression represent TRUE.
BITWISE LOGICAL OPERATORS
Integer values are stored using 64 bits each being either a 0 or 1. This
enables a variable to store a large range of integers either positive or
negative.
There are a number of logical operations that may be performed between two
integers taking each associated bit as either TRUE (1) of FALSE (0). This
would allow a single integer to store a number of flags that could be
set, reset or sampled with a logical operation. These operations can sometimes
be employed to modify or test an integer value knowing the binary relationship
between bits and integer value.
'&' bitwise AND where a bit is 1 if and only if the
corresponding bits in the two operands are also 1.
'|' bitwise OR where a bit is 1 if either of the
corresponding bits is a 1.
'^' bitwise XOR where a bit is 1 if the two corresponding
bits in the operands differ (not both 0 or 1).
There is one prefix unary operator that performs a bitwise operation but
on all of the bits of the integer value.
'~' bitwise complement where each bit is toggled from a
0 to 1 or 1 to 0.
Negative integers are store where the most significant (leftmost) bit indicates
the sign. A byte (8 bits) can represent a positive integer from 0 to 255 or
a signed integer from -128 to 127. SO for a signed byte integer we would
have:
00000000 = 0
00001000 = 8
01111111 = 127
11111111 = -1
10000000 = -128
The system uses two's complement math. As an example of the complement '~'
operation we can perform a two's complement negation by complementing a value
and adding 1.
$a = 1;
echo ~$a + 1;
-1
00000001 = 1
11111110 = ~1 or -2 at this point
11111111 = -1 after adding 1
While not strictly a logical operation there are two operators that are
used to shift an integer's bits either left or right.
'<<' shift left the number of bits defined by the right
operand. Each left shit multiplies the integer by 2.
'>>' shift right the number of bits defined by the right
operand. Each right shift divides the integer by 2.
Note that integers are 64-bit signed values. Therefore shifting a value
right retains the sign (most significant) bit as would simple division by 2.
Shifting a negative number to the right returns a negative number. The
following is also true. Why?
echo -1 >> 1;
-1
You can however shift a value to the left enough to overflow the integer
and create a negative value.
echo 1 << 63;
-9223372036854775808
That result being the minimum 64-bit integer that can be used. The maximum
64-bit value can be obtained using the complement '~' operator as follows:
echo ~(1 << 63);
9223372036854775807
STRING CONCATENATION
The '.' period operator is used to concatenate strings and variables. When
a mixed variable that may be numeric or boolean is concatenated or otherwise
included in a string, a string representation of the variable is created.
For example:
echo "This " . "is " . "a " . "test";
This is a test
$a = false;
echo "The variable is ".$a;
The variable is FALSE
$a = false;
echo "The variable is $a";
The variable is FALSE
$a = false;
echo "\$a is ".$a;
$a is FALSE
Note that there are other ways to echo the string with the value of $a. Also
note the need to escape the '$' if the intention is not to include the
variable's value at that point in the string. The ECHO statement will also
process a list of expressions separated by commas which will appear
essentially as a concatenation.
ASSIGNMENT OPERATORS
Variables are assigned values using the '=' equal sign. There are a number
of assignment operators that combine the various arithmetic, logical and
string operations so as to modify a variable's content. The following two
lines each attain the same result.
$a = $a + 3;
$a += 3;
The following operators may be used to assign and/or modify a variable.
'=' set the variable's value
'+=' add the right operand to the variable
'-=' subtract the right operand from the variable
'*=' multiply the variable's value by the right operand
'/=' divide the variable's value by the right operand
'.=' concatenate to a string variable
'%=' update the variable's value modulo the right operand
'&=' update the variable using bitwise AND
'|=' update the variable using bitwise OR
'^=' update a variable performing bitwise exclusive-OR
'<<=' update the variable by shifting left the number of bits
defined by the right operand
'>>=' update the variable by shifting right the number of bits
defined by the right operand
For example it is common in programming to use bits in an integer as flags
which can be set to indicate different situations/modes. A bit position is
usually assigned for some purpose. That bit can be selected in these bitwise
operations using an integer with only that bit set.
$flags = 0;
$flags |= 8; set the 4th bit
$flags &= ~8; clear the 4th bit
$flags ^= 8; toggle the 4th bit
if ($flags & 8) test the 4th bit
Of course it would be simpler to just assign a single boolean variable
for each situation. Bit flags are used often in protocols as they are
compact and easier to communicate as a set.
COMPARISON OPERATORS
Values and variables may be compared in a number of ways. This is often
required to control flow of the program through loops and conditional
execution. Each comparison operator takes a left and right operand. The
result of the comparison is a boolean value. For instance:
echo 100/25 == 4;
TRUE
The result being TRUE as the left operand evaluates to the same value as the
right operand.
[/flash/manpages/scripting.hlp:378]