1 1.1 rillig # $NetBSD: hanoi-include.mk,v 1.1 2020/10/03 17:30:54 rillig Exp $ 2 1.1 rillig # 3 1.1 rillig # Implements the Towers of Hanoi puzzle, thereby demonstrating a bunch of 4 1.1 rillig # useful programming techniques: 5 1.1 rillig # 6 1.1 rillig # * default assignment using the ?= assignment operator 7 1.1 rillig # * including the same file recursively 8 1.1 rillig # * extracting the current value of a variable using the .for loop 9 1.1 rillig # * using shell commands for calculations since make is a text processor 10 1.1 rillig # * using the :: dependency operator for adding commands to a target 11 1.1 rillig # * on-the-fly variable assignment expressions using the ::= modifier 12 1.1 rillig # 13 1.1 rillig # usage: 14 1.1 rillig # env N=3 make -f hanoi-include.mk 15 1.1 rillig # endless loop: 16 1.1 rillig # make -f hanoi-include.mk N=3 17 1.1 rillig 18 1.1 rillig N?= 5 # Move this number of disks ... 19 1.1 rillig FROM?= A # ... from this stack ... 20 1.1 rillig VIA?= B # ... via this stack ... 21 1.1 rillig TO?= C # ... to this stack. 22 1.1 rillig 23 1.1 rillig .if $N == 1 24 1.1 rillig . for from to in ${FROM} ${TO} 25 1.1 rillig all:: 26 1.1 rillig @echo "Move the upper disk from stack ${from} to stack ${to}." 27 1.1 rillig . endfor 28 1.1 rillig .else 29 1.1 rillig _:= ${N::!=expr $N - 1} ${TMP::=${VIA}} ${VIA::=${TO}} ${TO::=${TMP}} 30 1.1 rillig . include "${.PARSEDIR}/${.PARSEFILE}" 31 1.1 rillig _:= ${N::!=expr $N + 1} ${TMP::=${VIA}} ${VIA::=${TO}} ${TO::=${TMP}} 32 1.1 rillig 33 1.1 rillig . for from to in ${FROM} ${TO} 34 1.1 rillig all:: 35 1.1 rillig @echo "Move the upper disk from stack ${from} to stack ${to}." 36 1.1 rillig . endfor 37 1.1 rillig 38 1.1 rillig _:= ${N::!=expr $N - 1} ${TMP::=${VIA}} ${VIA::=${FROM}} ${FROM::=${TMP}} 39 1.1 rillig . include "${.PARSEDIR}/${.PARSEFILE}" 40 1.1 rillig _:= ${N::!=expr $N + 1} ${TMP::=${VIA}} ${VIA::=${FROM}} ${FROM::=${TMP}} 41 1.1 rillig .endif 42