3 # macros borrowed from m4 examples
7 # forloop_arg(from, to, macro) - invoke MACRO(value) for
8 # each value between FROM and TO, without define overhead
9 define(`forloop_arg', `ifelse(eval(`($1) <= ($2)'), `1',
10 `_forloop(`$1', eval(`$2'), `$3(', `)')')')
11 # forloop(var, from, to, stmt) - refactored to share code
12 define(`forloop', `ifelse(eval(`($2) <= ($3)'), `1',
13 `pushdef(`$1')_forloop(eval(`$2'), eval(`$3'),
14 `define(`$1',', `)$4')popdef(`$1')')')
16 `$3`$1'$4`'ifelse(`$1', `$2', `',
17 `$0(incr(`$1'), `$2', `$3', `$4')')')
20 # quote(args) - convert args to single-quoted string
21 define(`quote', `ifelse(`$#', `0', `', ``$*'')')
22 # dquote(args) - convert args to quoted list of quoted strings
23 define(`dquote', ``$@'')
24 # dquote_elt(args) - convert args to list of double-quoted strings
25 define(`dquote_elt', `ifelse(`$#', `0', `', `$#', `1', ```$1''',
26 ```$1'',$0(shift($@))')')
29 # foreachq(x, `item_1, item_2, ..., item_n', stmt)
30 # quoted list, version based on forloop
32 `ifelse(`$2', `', `', `_$0(`$1', `$3', $2)')')
34 `pushdef(`$1', forloop(`$1', `3', `$#',
35 `$0_(`1', `2', indir(`$1'))')`popdef(
36 `$1')')indir(`$1', $@)')
38 ``define(`$$1', `$$3')$$2`''')
41 # foreach(x, (item_1, item_2, ..., item_n), stmt)
42 # parenthesized list, improved version
43 define(`foreach', `pushdef(`$1')_$0(`$1',
44 (dquote(dquote_elt$2)), `$3')popdef(`$1')')
46 define(`_foreach', `ifelse(`$2', `(`')', `',
47 `define(`$1', _arg1$2)$3`'$0(`$1', (dquote(shift$2)), `$3')')')