xref: /openbsd/regress/usr.bin/dc/t20.in (revision 14d3a4a4)
1# $OpenBSD: t20.in,v 1.1 2003/12/09 12:46:32 otto Exp $
2#
3# Copyright (c) 2003 Amit Singh <amit_s@acm.org>
4#
5# Permission to use, copy, modify, and distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16#
17#
18# The Towers Of Hanoi
19# dc (Unix Desk Calculator) version
20# http://hanoi.kernelthread.com
21#
22
23[ # move(from, to)
24    n           # print from
25    [ --> ]n    # print " --> "
26    p           # print to\n
27    sw          # p doesn't pop, so get rid of the value
28]sm
29
30[ # init(n)
31    sw          # tuck n away temporarily
32    9           # sentinel as bottom of stack
33    lw          # bring n back
34    1           # "from" tower's label
35    3           # "to" tower's label
36    0           # processed marker
37]si
38
39[ # Move()
40    lt          # push to
41    lf          # push from
42    lmx         # call move(from, to)
43]sM
44
45[ # code block <d>
46    ln          # push n
47    lf          # push from
48    lt          # push to
49    1           # push processed marker 1
50    ln          # push n
51    1           # push 1
52    -           # n - 1
53    lf          # push from
54    ll          # push left
55    0           # push processed marker 0
56]sd
57
58[ # code block <e>
59    ln          # push n
60    1           # push 1
61    -           # n - 1
62    ll          # push left
63    lt          # push to
64    0           # push processed marker 0
65]se
66
67[ # code block <x>
68    ln 1 =M
69    ln 1 !=d
70]sx
71
72[ # code block <y>
73    lMx
74    lex
75]sy
76
77[ # quit()
78    q           # exit the program
79]sq
80
81[ # run()
82    d 9 =q      # if stack empty, quit()
83    sp          # processed
84    st          # to
85    sf          # from
86    sn          # n
87    6           #
88    lf          #
89    -           #
90    lt          #
91    -           # 6 - from - to
92    sl          #
93    lp 0 =x     #
94    lp 0 !=y    #
95    lrx         # loop
96]sr
97
985lix # init(n)
99lrx # run()
100
101