11.2 Evaluating integer expressions
Integer expressions are evaluated with `eval':
-- Builtin: eval (EXPRESSION, [RADIX = `10'], [WIDTH])
Expands to the value of EXPRESSION. The expansion is empty if an
error is encountered while parsing the arguments. If specified,
RADIX and WIDTH control the format of the output.
The macro `eval' is recognized only with parameters.
Expressions can contain the following operators, listed in order of
Unary plus and minus
`* / %'
Multiplication, division and modulo
Addition and subtraction
Shift left or right
`== != > >= < <='
All operators, except exponentiation, are left associative.
Note that some older `m4' implementations use `^' as an alternate
operator for exponentiation, although POSIX requires the C behavior of
bitwise exclusive-or. On the other hand, the precedence of `~' and `!'
are different in GNU `m4' than they are in C, matching the precedence
in traditional `m4' implementations. This behavior is likely to change
in a future version to match POSIX, so use parentheses to force the
Within EXPRESSION, (but not RADIX or WIDTH), numbers without a
special prefix are decimal. A simple `0' prefix introduces an octal
number. `0x' introduces a hexadecimal number. `0b' introduces a
binary number. `0r' introduces a number expressed in any radix between
1 and 36: the prefix should be immediately followed by the decimal
expression of the radix, a colon, then the digits making the number.
For radix 1, leading zeros are ignored and all remaining digits must be
`1'; for all other radices, the digits are `0', `1', `2', .... Beyond
`9', the digits are `a', `b' ... up to `z'. Lower and upper case
letters can be used interchangeably in numbers prefixes and as number
Parentheses may be used to group subexpressions whenever needed.
For the relational operators, a true relation returns `1', and a false
relation return `0'.
Here are a few examples of use of `eval'.
eval(`-3 * 5')
eval(index(`Hello world', `llo') >= 0)
eval(`0r1:0111 + 0b100 + 0r3:12')
error-->m4:stdin:8: bad expression in eval: foo/6
As the last two lines show, `eval' does not handle macro names, even
if they expand to a valid expression (or part of a valid expression).
Therefore all macros must be expanded before they are passed to `eval'.
All evaluation is done with 32-bit signed integers, assuming
2's-complement with wrap-around. The shift operators are defined in GNU
`m4' by doing an implicit bit-wise and of the right-hand operand with
0x1f, and sign-extension with right shift.
eval(0x80000000 / -1)
eval(0x80000000 % -1)
eval(-4 >> 33)
If RADIX is specified, it specifies the radix to be used in the
expansion. The default radix is 10; this is also the case if RADIX is
the empty string. It is an error if the radix is outside the range of
1 through 36, inclusive. The result of `eval' is always taken to be
signed. No radix prefix is output, and for radices greater than 10,
the digits are lower case. The WIDTH argument specifies the minimum
output width, excluding any negative sign. The result is zero-padded
to extend the expansion to the requested width. It is an error if the
width is negative. On error, the expansion of `eval' is empty.
eval(`666', `6', `10')
eval(`-666', `6', `10')
eval(`10', `', `0')
`0r1:'eval(`10', `1', `11')
automatically generated byinfo2html