Copyright (C) 2000-2012 |
Whole document tree
## Macros for doing arithmetic
Integer arithmetic is included in ## Decrement and increment operators
Increment and decrement of integers are supported using the builtins
incr(
which expand to the numerical value of incr(4) =>5 decr(7) =>6
The builtin macros ## Evaluating integer expressions
Integer expressions are evaluated with eval(
which expands to the value of Expressions can contain the following operators, listed in order of decreasing precedence. `-` - Unary minus
`**` - Exponentiation
`* / %` - Multiplication, division and modulo
`+ -` - Addition and subtraction
`<< >>` - Shift left or right
`== != > >= < <=` - Relational operators
`!` - Logical negation
`~` - Bitwise negation
`&` - Bitwise and
`^` - Bitwise exclusive-or
`|` - Bitwise or
`&&` - Logical and
`||` - Logical or
All operators, except exponentiation, are left associative.
Note that many
Numbers without special prefix are given decimal. A simple
Parentheses may be used to group subexpressions whenever needed. For the
relational operators, a true relation returns
Here are a few examples of use of eval(-3 * 5) =>-15 eval(index(`Hello world', `llo') >= 0) =>1 define(`square', `eval(($1)**2)') => square(9) =>81 square(square(5)+1) =>676 define(`foo', `666') => eval(`foo'/6) error-->51.eval:14: m4: Bad expression in eval: foo/6 => eval(foo/6) =>111
As the second to last example shows,
If eval(666, 10) =>666 eval(666, 11) =>556 eval(666, 6) =>3030 eval(666, 6, 10) =>0000003030 eval(-666, 6, 10) =>-000003030
Take note that
The builtin macro Go to the first, previous, next, last section, table of contents. |