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