xref: /freebsd/contrib/bmake/unit-tests/hanoi-include.mk (revision 954401e68e797868ab04a0147b94849feefbb199)
1*954401e6SSimon J. Gerraty# $NetBSD: hanoi-include.mk,v 1.3 2022/05/08 07:27:50 rillig Exp $
2956e45f6SSimon J. Gerraty#
3*954401e6SSimon J. Gerraty# Implements the Towers of Hanoi puzzle, demonstrating a bunch of more or less
4*954401e6SSimon J. Gerraty# useful programming techniques:
5956e45f6SSimon J. Gerraty#
6956e45f6SSimon J. Gerraty#	* default assignment using the ?= assignment operator
79f45a3c8SSimon J. Gerraty#	* including the same file recursively (rather unusual)
8956e45f6SSimon J. Gerraty#	* extracting the current value of a variable using the .for loop
9956e45f6SSimon J. Gerraty#	* using shell commands for calculations since make is a text processor
10956e45f6SSimon J. Gerraty#	* using the :: dependency operator for adding commands to a target
11956e45f6SSimon J. Gerraty#	* on-the-fly variable assignment expressions using the ::= modifier
12956e45f6SSimon J. Gerraty#
13956e45f6SSimon J. Gerraty# usage:
14*954401e6SSimon J. Gerraty#	env N=3 make -r -f hanoi-include.mk
15*954401e6SSimon J. Gerraty#
16*954401e6SSimon J. Gerraty# endless loop, since command line variables cannot be overridden:
17*954401e6SSimon J. Gerraty#	make -r -f hanoi-include.mk N=3
18956e45f6SSimon J. Gerraty
19956e45f6SSimon J. GerratyN?=	5			# Move this number of disks ...
20956e45f6SSimon J. GerratyFROM?=	A			# ... from this stack ...
21956e45f6SSimon J. GerratyVIA?=	B			# ... via this stack ...
22956e45f6SSimon J. GerratyTO?=	C			# ... to this stack.
23956e45f6SSimon J. Gerraty
24956e45f6SSimon J. Gerraty.if $N == 1
25956e45f6SSimon J. Gerraty.  for from to in ${FROM} ${TO}
26956e45f6SSimon J. Gerratyall::
27956e45f6SSimon J. Gerraty	@echo "Move the upper disk from stack ${from} to stack ${to}."
28956e45f6SSimon J. Gerraty.  endfor
29956e45f6SSimon J. Gerraty.else
30956e45f6SSimon J. Gerraty_:=	${N::!=expr $N - 1} ${TMP::=${VIA}} ${VIA::=${TO}} ${TO::=${TMP}}
31956e45f6SSimon J. Gerraty.  include "${.PARSEDIR}/${.PARSEFILE}"
32956e45f6SSimon J. Gerraty_:=	${N::!=expr $N + 1} ${TMP::=${VIA}} ${VIA::=${TO}} ${TO::=${TMP}}
33956e45f6SSimon J. Gerraty
34956e45f6SSimon J. Gerraty.  for from to in ${FROM} ${TO}
35956e45f6SSimon J. Gerratyall::
36956e45f6SSimon J. Gerraty	@echo "Move the upper disk from stack ${from} to stack ${to}."
37956e45f6SSimon J. Gerraty.  endfor
38956e45f6SSimon J. Gerraty
39956e45f6SSimon J. Gerraty_:=	${N::!=expr $N - 1} ${TMP::=${VIA}} ${VIA::=${FROM}} ${FROM::=${TMP}}
40956e45f6SSimon J. Gerraty.  include "${.PARSEDIR}/${.PARSEFILE}"
41956e45f6SSimon J. Gerraty_:=	${N::!=expr $N + 1} ${TMP::=${VIA}} ${VIA::=${FROM}} ${FROM::=${TMP}}
42956e45f6SSimon J. Gerraty.endif
43