
19-6
Return to
2600AS-901-01 Rev. B / September 2008
Section 19: Remote Commands
Series 2600A System SourceMeter® Instruments Reference Manual
Precedence
Operator precedence in TSL follows the table below, from higher to lower priority:
^
not - (unary)
*
/
+
-
.. (concatenation)
<
>
<=>=~===
and
or
All operators are left associative, except for ‘
^
’ (exponentiation) and ‘
..
’, which are right
associative. Therefore, the following expressions on the left are equivalent to those on the right:
a+i < b/2+1
(a+i) < ((b/2)+1)
5+x^2*8
5+((x^2)*8)
a < y and y <= z
(a < y) and (y <= z)
-x^2
-(x^2)
x^y^z x^(y^z)
Logical operators
The logical operators are
and
,
or
, and
not
. Like control structures, all logical operators consider
false
and
nil
as false and anything else as true. The operator
and
returns its first argument if it
is false, otherwise it returns its second argument. The operator
or
returns its first argument if it is
not false; otherwise it returns its second argument:
print(4 and 5)
print(nil and 13)
print(false and 13)
print(4 or 5)
print(false or 5)
Output of code above:
5
nil
false
4
5
Both
and
and
or
use short-cut evaluation, that is, they evaluate their second operand only when
necessary. A useful TSL construct is
x = x or v
, which is equivalent to:
if not x then x = v end
For example, it sets
x
to a default value
v
when
x
is not set (provided that
x
is not set to
false
).
To select the maximum of two numbers
x
and
y
, use the following statement (note the
and
operator has a higher precedence than
or
):
max = (x > y) and x or y
When
x > y
is true, the first expression of the
and
is true, so the
and
results in its second
argument
x
(which is also true, because it is a number), and then the
or
expression, results in the
value of its first expression,
x
. When
x > y
is false, the
and
expression is false and so are the
or
results in its second expression,
y
.
The operator
not
always returns
true
or
false
: