Home | History | Annotate | Line # | Download | only in unit-tests
hanoi-include.mk revision 1.4
      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 
     19 N?=	5			# Move this number of disks ...
     20 FROM?=	A			# ... from this stack ...
     21 VIA?=	B			# ... via this stack ...
     22 TO?=	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
     27 N:=	count ${:U:${:Urange=$N}}	# 'count' + one word for every disk
     28 .endif
     29 
     30 .if ${N:[#]} == 2
     31 .  for from to in ${FROM} ${TO}
     32 all::
     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}
     41 all::
     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