-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.m4
66 lines (51 loc) · 2.42 KB
/
common.m4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
divert(-1)
include(`data/perks.m4')
include(`data/weapons.m4')
include(gen_root`/src/manual_perks.m4')
include(gen_root`/src/manual_weapons.m4')
define(`single_entry', ``dimwishlist:item='$1`'ifelse($#, 1, , $2, ,, ``&perks='shift($@)')')
dnl replace_arg(index, value, nargs, args...)
define(`replace_arg', `ifelse(
$3, 0, `',
$3, 1, `ifelse($1, 1, $2, $4)',
`ifelse($1, 1, $2, $4)`, '$0(decr($1), $2, decr($3), shift(shift(shift(shift($@)))))')')
define(`unwrap', `$1')
define(`argn', `ifelse(`$1', 1, ``$2'', `argn(decr(`$1'), shift(shift($@)))')')
dnl _wishlist_entry(expanded_count, item, perks...)
define(`_wishlist_entry', `ifelse(
eval($# - 2 - $1 == 0), 1, `single_entry(shift($@))',
`pushdef(`__cur', argn(incr($1), shift(shift($@))))pushdef(`__index', index(__cur, `|'))dnl
ifelse(
__index, -1, `$0(incr($1), shift($@))',
`$0(incr($1), $2, unwrap(replace_arg(incr($1), substr(__cur, 0, __index), decr(decr($#)), shift(shift($@)))))
$0($1, $2, unwrap(replace_arg(incr($1), substr(__cur, incr(__index)), decr(decr($#)), shift(shift($@)))))')dnl
popdef(`__cur')popdef(`__index')dnl
')')
ifelse(`Roughly equivalent python code for the above macro:
def _wishlist_entry(expanded_count, item, *perks):
if len(perks) == expanded_count:
# All arguments have been expanded, just emit the entry.
single_entry(item, *perks)
return
perk = perks[expanded_count]
if "|" in perk:
# We have some alternatives to go through. Split the string into first
# one, and the rest.
a, _, b = perk.partition("|")
# Substitute first alternative in place of the currently processed argument
# and continue with the rest of args.
_wishlist_entry(expanded_count+1, item,
*(perks[:expanded_count] + [a] + perks[expanded_count+1:]))
# Do the same with the rest of alternatives. expanded_count is not
# incremented because there might still be "|" in them.
_wishlist_entry(expanded_count, item,
*(perks[:expanded_count] + [b] + perks[expanded_count+1:]))
else:
# Next perk spec does not include alternatives, move on to the next one.
_wishlist_entry(expanded_count+1, item, *perks)
')
define(`_join', `ifelse($3, 0, $1, $3, 1, $1`'$4, `_join($1`'$4`'$2`', `$2', decr(`$3'), shift(shift(shift(shift($@)))))')')
define(`any_of', `_join(`', `|', $#, $@)')
define(`wishlist_entry', `_wishlist_entry(0, $@)')
changecom(`//')
divert`'dnl