1# $NetBSD: hanoi-include.mk,v 1.4 2023/01/19 22:48:42 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 shell commands for calculations since make is a text processor 10# * using the :: dependency operator for adding commands to a target 11# * on-the-fly variable assignment expressions using the ::= modifier 12# 13# usage: 14# env N=3 make -r -f hanoi-include.mk 15# 16# endless loop, since command line variables cannot be overridden: 17# make -r -f hanoi-include.mk N=3 18 19N?= 5 # Move this number of disks ... 20FROM?= A # ... from this stack ... 21VIA?= B # ... via this stack ... 22TO?= C # ... to this stack. 23 24# Since make has no built-in arithmetic functions, convert N to a list of 25# words and use the built-in word counting instead. 26.if ${N:[#]} == 1 27N:= count ${:U:${:Urange=$N}} # 'count' + one word for every disk 28.endif 29 30.if ${N:[#]} == 2 31. for from to in ${FROM} ${TO} 32all:: 33 @echo "Move the upper disk from stack ${from} to stack ${to}." 34. endfor 35.else 36_:= ${N::=${N:[1..-2]}} ${TMP::=${VIA}} ${VIA::=${TO}} ${TO::=${TMP}} 37. include "${.PARSEDIR}/${.PARSEFILE}" 38_:= ${N::+=D} ${TMP::=${VIA}} ${VIA::=${TO}} ${TO::=${TMP}} 39 40. for from to in ${FROM} ${TO} 41all:: 42 @echo "Move the upper disk from stack ${from} to stack ${to}." 43. endfor 44 45_:= ${N::=${N:[1..-2]}} ${TMP::=${VIA}} ${VIA::=${FROM}} ${FROM::=${TMP}} 46. include "${.PARSEDIR}/${.PARSEFILE}" 47_:= ${N::+=D} ${TMP::=${VIA}} ${VIA::=${FROM}} ${FROM::=${TMP}} 48.endif 49