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, and in /test.

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: i and j for integer, s and t for string. All operators group left-to-right except assignment, which groups right-to-left.

(expr)
Parentheses, for grouping.

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

!i
Boolean NOT (1 if i==0, otherwise 0).
+i
Unary positive (useful for converting a string to an integer).
-i
Unary negative.
++v
Increments variable v and returns its new value.
--v
Decrements variable v and returns its new value.

i * j
Integer multiplication.
i / j
Integer division.

i + j
Integer addition.
i - j
Integer subtraction.

i = j
Integer equality.
i == j
Integer equality.
i != j
Integer 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.
i < j
Integer less than.
i <= j
Integer less than or equal.
i > j
Integer greater than.
i >= j
Integer greater than or equal.

i & j
Boolean AND. j will be evaluated if and only if i is true.

i | j
Boolean OR. j will be evaluated if and only if i is false.

i ? x : y
i ? : y
Conditional. If i is nonzero, the result is the value of expression x; otherwise it is the value of expression y. If x is omitted, the value of i 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.

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

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

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.

Variable substitutions of the form "{var}" and "{var-default}" may be used to access any variable (named or positional). Note that there is no leading '%', and the '{' and '}' are required.

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.

All operands will be automatically converted to the type expected by the operator. String to integer conversion is done by interpreting leading digits as an integer; e.g., "12ab" becomes 12, and "xyz" becomes 0. Integer to string conversion is straightfoward. Enumerated variables (i.e., special variables that are allowed to have only a limited set of values, such as visual, which can only be "off" or "on") are converted to strings in a straightforward manner. Enumerated variables are converted to integers by having one integer stand for each of the allowed values. "Off" is always 0, "on" is always "1", etc. This makes "!visual" and "visual == 0" the same as "visual =~ 'off'". Other (non-enumerated) variable references are treated as strings.

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, 1996, 1997 Ken Keys