1# $NetBSD: hanoi-include.mk,v 1.5 2023/10/19 18:24:33 rillig Exp $
2#
3# Implements the Towers of Hanoi puzzle, demonstrating a bunch of more or less
4# useful programming techniques:
5#
6#	* default assignment using the ?= assignment operator
7#	* including the same file recursively (rather unusual)
8#	* extracting the current value of a variable using the .for loop
9#	* using the :: dependency operator for adding commands to a target
10#	* on-the-fly variable assignment expressions using the ::= modifier
11#
12# usage:
13#	env N=3 make -r -f hanoi-include.mk
14#
15# Specifying N in the command line instead of in the environment would produce
16# an endless loop, since variables from the command line cannot be overridden
17# by global variables:
18#	make -r -f hanoi-include.mk N=3
19
20N?=	5			# Move this number of disks ...
21FROM?=	A			# ... from this stack ...
22VIA?=	B			# ... via this stack ...
23TO?=	C			# ... to this stack.
24
25# Since make has no built-in arithmetic functions, convert N to a list of
26# words and use the built-in word counting instead.
27.if ${N:[#]} == 1
28N:=	count ${:U:${:Urange=$N}}	# 'count' + one word for every disk
29.endif
30
31.if ${N:[#]} == 2
32.  for from to in ${FROM} ${TO}
33all::
34	@echo "Move the upper disk from stack ${from} to stack ${to}."
35.  endfor
36.else
37_:=	${N::=${N:[1..-2]}} ${TMP::=${VIA}} ${VIA::=${TO}} ${TO::=${TMP}}
38.  include "${.PARSEDIR}/${.PARSEFILE}"
39_:=	${N::+=D} ${TMP::=${VIA}} ${VIA::=${TO}} ${TO::=${TMP}}
40
41.  for from to in ${FROM} ${TO}
42all::
43	@echo "Move the upper disk from stack ${from} to stack ${to}."
44.  endfor
45
46_:=	${N::=${N:[1..-2]}} ${TMP::=${VIA}} ${VIA::=${FROM}} ${FROM::=${TMP}}
47.  include "${.PARSEDIR}/${.PARSEFILE}"
48_:=	${N::+=D} ${TMP::=${VIA}} ${VIA::=${FROM}} ${FROM::=${TMP}}
49.endif
50