expressions

Expressions apply operators to numeric and string operands, and return a result. They can be used in $[...] expression subs, the condition of /if and /while statements, the condition of /def -E, and as arguments to /return, /result, and /test commands.

Operands

Operands can be any of:

Named variables may be accessed by simply using their name (with no leading '%'). This is called a variable reference, and is the preferred way of using a variable in an expression. The special substitutions (*, ?, #, n, Ln, Pn, R) may not be used this way.

Variable substitutions of the form "{selector}" and "{selector-default}" may be used. They follow the same rules as variable substitution in macros, except that there is no leading '%', and the '{' and '}' are required. The special substitutions (*, ?, #, n, Ln, Pn, R) are allowed.

Macro-style variable substitutions beginning with '%' may also be used, but are not recommended, since the multiple '%'s required in nested macros can quickly get confusing. It always easier to use one of the above methods.

Operators

In the following list, operators are listed in groups, from highest to lowest precedence. Operators listed together have equal precedence. The letters in the table below correspond to the type of objects acted on by the operators: n and m for numeric (integer or float), s and t for string. All operators group left-to-right except assignment, which groups right-to-left. If any binary numeric operator is applied to two integers, the result will be an integer; but if either operand is a float, the other will be converted to float if it is not already a float, and the result will be a float.

(expression)
Parentheses, for grouping.

fn(args)
Perform function fn on args (see: functions).

!n
Boolean NOT (1 if n==0, otherwise 0).
+n
Unary positive (useful for converting a string to a number).
-n
Unary negative.
++v
Adds 1 to variable v and returns its new value.
--v
Subtracts 1 from variable v and returns its new value.

n * m
Numeric multiplication.
n / m
Numeric division.

n + m
Numeric addition.
n - m
Numeric subtraction.

n = m
Numeric equality.
n == m
Numeric equality.
n != m
Numeric inequality.
s =~ t
String equality (case sensitive).
s !~ t
String inequality (case sensitive).
s =/ t
String s matches glob pattern t.
s !/ t
String s does not match glob pattern t.
n < m
Numeric less than.
n <= m
Numeric less than or equal.
n > m
Numeric greater than.
n >= m
Numeric greater than or equal.

n & m
Boolean AND. m will be evaluated if and only if n is true.

n | m
Boolean OR. m will be evaluated if and only if n is false.

n ? a : b
n ? : b
Conditional. If n is nonzero, the result is the value of expression a; otherwise it is the value of expression b. If a is omitted, the value of n is used in its place.

v := s
Assignment. The identifier "v" refers to the variable in the nearest scope. If not found, a new variable is created at the global level, as if by /set.

a , b
Comma. Expressions a and b are evaluated; the result is the value of b. Only useful if a has some side effect.

The comparison operators return 0 for false, nonzero for true. The boolean operators (& and |) stop evaluating as soon as the value of the expression is known ("short-circuit"), and return the value of the last operand evaluated. This does not affect the value of the expression, but is important when the second operand performs side effects.

All operands will be automatically converted to the type expected by the operator.

Examples

Given the variables
    /set X=5
    /set name=Hawkeye
    /set visual=1
here are some expressions and their values:
    Expression		   Value   Comments
    ----		   -----   --------
    3 + X * 2		      13   3 + (5 * 2) = 13.
    "foo" =~ "bar"	       0   "foo" is not identical to "bar".
    name =/ 'hawk*'	       1   "Hawkeye" matches the glob "hawk*".
    X =~ "+5"		       0   X is interpreted as string "5".
    X == "+5"		       1   string "+5" is converted to integer 5.
    visual & (X > 0)	       1   visual is nonzero, AND %X is positive.

See: functions, /test, evaluation, patterns


Back to index
Back to tf home page
Copyright © 1995 - 1999 Ken Keys