Author: bernhard
Date: Wed Oct 19 15:57:56 2005
New Revision: 9518

Added:
   trunk/examples/pir/hanoi.pir
      - copied, changed from r9517, trunk/examples/assembly/hanoi.pasm
Removed:
   trunk/examples/assembly/hanoi.pasm
Modified:
   trunk/CREDITS
   trunk/MANIFEST
   trunk/t/examples/pir.t
Log:
Make the example hanoi.pasm work again by converting it
to PIR baby talk.
Add a test for hanoi.pir.
Add credit to Tony Payne


Modified: trunk/CREDITS
==============================================================================
--- trunk/CREDITS       (original)
+++ trunk/CREDITS       Wed Oct 19 15:57:56 2005
@@ -401,6 +401,9 @@ D: ParTcl tests and inspiration
 N: TOGoS
 D: Some FAQ questions and answers
 
+N: Tony Payne
+D: Example hanoi.pasm
+
 N: Tom Hughes
 
 E: [EMAIL PROTECTED]

Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST      (original)
+++ trunk/MANIFEST      Wed Oct 19 15:57:56 2005
@@ -445,7 +445,6 @@ encodings/utf8.h                        
 examples/README                                   [main]doc
 examples/assembly/Makefile                        [main]doc
 examples/assembly/acorn.l                         [main]doc
-examples/assembly/hanoi.pasm                      [main]doc
 examples/assembly/hello-dwim.imc                  [main]doc
 examples/assembly/io1.pasm                        [main]doc
 examples/assembly/io2.pasm                        [main]doc
@@ -601,6 +600,7 @@ examples/pasm/cat.pasm                  
 examples/pasm/fact.pasm                           [main]doc
 examples/pasm/hello.pasm                          [main]doc
 examples/pir/euclid.pir                           [main]doc
+examples/pir/hanoi.pir                            [main]doc
 examples/pir/mandel.pir                           [main]doc
 examples/pir/sudoku.pir                           [main]doc
 examples/pni/PQt.C                                [main]doc

Copied: trunk/examples/pir/hanoi.pir (from r9517, 
trunk/examples/assembly/hanoi.pasm)
==============================================================================
--- trunk/examples/assembly/hanoi.pasm  (original)
+++ trunk/examples/pir/hanoi.pir        Wed Oct 19 15:57:56 2005
@@ -1,4 +1,4 @@
-# Copyright (C) 2001-2003 The Perl Foundation.  All rights reserved.
+# Copyright (C) 2001-2005 The Perl Foundation.  All rights reserved.
 # $Id$
 
 =head1 NAME
@@ -14,7 +14,7 @@ You have to pass in the height of the to
 =head1 DESCRIPTION
 
 Towers of Hanoi (http://www.cut-the-knot.org/recurrence/hanoi.shtml) is
-a combinatorial puzzle. The PASM shows manipulation of arrays of
+a combinatorial puzzle. The PIR shows manipulation of arrays of
 integers.
 
 =head1 Data Structure
@@ -77,104 +77,123 @@ In pseudocode:
         print(array, size);
     }
 
+
+=head1 TODO
+
+Convert this to proper PIR, with subroutines and such.
+
+=head1 HISTORY
+
+First version Tony Payne              2002-15-05
+Converted to PIR Bernhard Schmalhofer 2005-10-20
+
 =cut
 
-#
-# Towers of hanoi in parrot assembler
-#
-#
-
-MAIN:
-        set I0, P5
-        lt I0, 2, ERROR
-
-       set S5, P5[1]           # S5 = argv[0]
-        set I5, S5              # Convert to an int
-       new P0, .FixedPMCArray
+# Towers of hanoi in Parrot Intermediate Representation
+.sub "hanoi" :main
+        .param pmc argv
+        .local int size
+
+        # check number of command line arguments
+        $I0 = argv
+        if $I0 < 2 goto USE_DEFAULT_SIZE
+        S5 = argv[1]    
+        size = S5
+        print "Building a tower of size "
+        print size
+        print ".\n"
+        goto SIZE_IS_NOW_KNOWN        
+USE_DEFAULT_SIZE:
+        print "Using default size 3 for tower.\n"
+        size = 3
+SIZE_IS_NOW_KNOWN:        
+        print "\n"
+
+        new P0, .FixedPMCArray
         set P0, 3
-       new P1, .ResizableIntegerArray
-       new P2, .ResizableIntegerArray
-       new P3, .ResizableIntegerArray
-       set P0[0], P1         #P0 = [[],[],[]]
-       set P0[1], P2
-       set P0[2], P3
+        new P1, .ResizableIntegerArray
+        new P2, .ResizableIntegerArray
+        new P3, .ResizableIntegerArray
+        set P0[0], P1                         #P0 = [[],[],[]]
+        set P0[1], P2
+        set P0[2], P3
 
-       set I0, 0
+        set I0, 0
 loop_populate:
-       add I1, I0, 1
-       set P1[I0], I1        #P0=[[1,2,3,...],[0,0,0...],[0,0,0...]]
-       set P2[I0], 0
-       set P3[I0], 0
-       inc I0
-       lt  I0, I5, loop_populate
-       set I1, I5  # size
-       set I2, 0   # start_col
-       set I3, 2   # target_col
-       set I4, 1   # storage_col
-       bsr MOVE_STACK
-END:   end
+        add I1, I0, 1
+        set P1[I0], I1                        
#P0=[[1,2,3,...],[0,0,0...],[0,0,0...]]
+        set P2[I0], 0
+        set P3[I0], 0
+        inc I0
+        lt  I0, size, loop_populate
+        set I1, size                            # size
+        set I2, 0                             # start_col
+        set I3, 2                             # target_col
+        set I4, 1                             # storage_col
+        bsr MOVE_STACK
+END:    end
 
 #Params
 #  P0 = array (const)
 #  I0 = size  (const)
-PRINT: # vars used: I1, I2, I3, I4, I5, I6, P1
-       set I1, 0                #I1 = i
-       set I2, 0                #I2 = j
-       set I3, 0                #I3 = k
+PRINT:  # vars used: I1, I2, I3, I4, size, I6, P1
+        set I1, 0                             #I1 = i
+        set I2, 0                             #I2 = j
+        set I3, 0                             #I3 = k
 loop_rows:
-       set I2, 0
+        set I2, 0
 loop_cols:
-       set P1,P0[I2]       #P1 = P0[j]
-       set I4,P1[I1]       #I4 = cursize; cursize=array[j][i]
+        set P1,P0[I2]                         #P1 = P0[j]
+        set I4,P1[I1]                         #I4 = cursize; 
cursize=array[j][i]
 
-       sub I5, I0, I4           #I5 = size-cursize
-       repeat S0, " ", I5
-       print S0
-       mul I6, I4, 2            #I6 = cursize * 2
-       repeat S0, "=", I6
-       print S0
-       repeat S0, " ", I5
-       print S0
-
-       inc I2                   # j++
-       eq I2, 3, done_loop
-       print " | "
-       if 1, loop_cols      # j < 3
+        size = I0 - I4                          #size = size-cursize
+        repeat S0, " ", size
+        print S0
+        mul I6, I4, 2                         #I6 = cursize * 2
+        repeat S0, "=", I6
+        print S0        
+        repeat S0, " ", size
+        print S0
+
+        inc I2                                # j++
+        eq I2, 3, done_loop
+        print " | "
+        if 1, loop_cols                       # j < 3
 done_loop:
-       print "\n"
-       inc I1                   # i++
-       lt I1, I0, loop_rows     # i < size
-       print "\n"
-       ret
+        print "\n"
+        inc I1                                # i++
+        lt I1, I0, loop_rows                  # i < size
+        print "\n"
+        ret
 
 # params
 # P0 = array
 # I0 = size
 # I2 = start_col
 # I3 = dest_col
-MOVE:  #vars used: I4, I5, I6, I7, I8, P1, P2
-       set I4, 0                         #I4 = i
-       set P1, P0[I2]              #P1 = array[start_col]
+MOVE:        #vars used: I4, size, I6, I7, I8, P1, P2
+        set I4, 0                             #I4 = i
+        set P1, P0[I2]                        #P1 = array[start_col]
 loop_find_start_row:
-       set I7, P1[I4]              #I7 = array[start_col][i]
-       ne I7, 0, found_start_row
-       inc I4                            #  i++
-       lt I4, I0, loop_find_start_row    #  i < size
+        set I7, P1[I4]                        #I7 = array[start_col][i]
+        ne I7, 0, found_start_row
+        inc I4                                #  i++
+        lt I4, I0, loop_find_start_row        #  i < size
 found_start_row:
-       set I5, I4                        #I5 = start_row = i
-       set P2, P0[I3]              #P2 = array[dest_col]
-       set I4, 0                         #  for( i = 0
+        set size, I4                            #size = start_row = i
+        set P2, P0[I3]                        #P2 = array[dest_col]
+        set I4, 0                             #  for( i = 0
 loop_find_dest_row:
-       set I8, P2[I4]            #I8 = array[dest_col][i]
-       ne I8, 0, found_dest_row          #  if(array[dest_col][i])
-       inc I4                            #  i++
-       lt I4, I0, loop_find_dest_row     #  i < size
+        set I8, P2[I4]                        #I8 = array[dest_col][i]
+        ne I8, 0, found_dest_row              #  if(array[dest_col][i])
+        inc I4                                #  i++
+        lt I4, I0, loop_find_dest_row         #  i < size
 found_dest_row:
-       sub I6, I4, 1                     #I6 = dest_row = i - 1
-       set P2[I6], I7            # array[dc][dr]=array[sc][sr]
-       set P1[I5], 0               # array[sc][sr]=0
-       bsr PRINT
-       ret
+        sub I6, I4, 1                         #I6 = dest_row = i - 1
+        set P2[I6], I7                        # array[dc][dr]=array[sc][sr]
+        set P1[size], 0                         # array[sc][sr]=0
+        bsr PRINT
+        ret
 
 # P0 = array
 # I0 = size
@@ -183,53 +202,45 @@ found_dest_row:
 # I3 = target_col
 # I4 = storage_col
 MOVE_STACK:
-       gt I1, 1, move_multiple         # if num == 1
-       bsr MOVE                        # return move(...)
-       ret
+        gt I1, 1, move_multiple               # if num == 1
+        bsr MOVE                              # return move(...)
+        ret
 move_multiple:
-       save I1
-       dec I1
-       save I4
-       save I3
-       save I2
-       set I5, I3
-       set I3, I4
-       set I4, I5
-       bsr MOVE_STACK                  #move_stack(...)
-       restore I2
-       restore I3
-       restore I4
-       restore I1
-       save I1
-       save I4
-       save I3
-       save I2
-       bsr MOVE                        #move(...)
-       restore I2
-       restore I3
-       restore I4
-       restore I1
-       save I1
-       save I4
-       save I3
-       save I2
-       dec I1
-       set I5, I2
-       set I2, I4
-       set I4, I5
-       bsr MOVE_STACK                  #move_stack(...)
-       restore I2
-       restore I3
-       restore I4
-       restore I1
-       ret
-
-ERROR:  print "Error: no size specified for tower\n"
-        end
-
-=head1 HISTORY
-
-First version Tony Payne 05/15/2002.
-
-=cut
+        save I1
+        dec I1
+        save I4
+        save I3
+        save I2
+        set size, I3
+        set I3, I4
+        set I4, size
+        bsr MOVE_STACK                        #move_stack(...)
+        restore I2
+        restore I3
+        restore I4
+        restore I1
+        save I1
+        save I4
+        save I3
+        save I2
+        bsr MOVE                              #move(...)
+        restore I2
+        restore I3
+        restore I4
+        restore I1
+        save I1
+        save I4
+        save I3
+        save I2
+        dec I1
+        set size, I2
+        set I2, I4
+        set I4, size
+        bsr MOVE_STACK                        #move_stack(...)
+        restore I2
+        restore I3
+        restore I4
+        restore I1
+        ret
 
+.end

Modified: trunk/t/examples/pir.t
==============================================================================
--- trunk/t/examples/pir.t      (original)
+++ trunk/t/examples/pir.t      Wed Oct 19 15:57:56 2005
@@ -32,7 +32,7 @@ Bernhard Schmalhofer - <Bernhard.Schmalh
 =cut
 
 use strict;
-use Parrot::Test tests => 3;
+use Parrot::Test tests => 4;
 use Test::More;
 use Parrot::Config;
 
@@ -45,6 +45,39 @@ Algorithm E (Euclid's algorithm)
 The greatest common denominator of 96 and 64 is 32.
 END_EXPECTED
 
+    'hanoi.pir'        =>  << 'END_EXPECTED',
+Using default size 3 for tower.
+
+       |        |       
+ ====  |        |       
+====== |        |   ==  
+
+       |        |       
+       |        |       
+====== |  ====  |   ==  
+
+       |        |       
+       |   ==   |       
+====== |  ====  |       
+
+       |        |       
+       |   ==   |       
+       |  ====  | ======
+
+       |        |       
+       |        |       
+  ==   |  ====  | ======
+
+       |        |       
+       |        |  ==== 
+  ==   |        | ======
+
+       |        |   ==  
+       |        |  ==== 
+       |        | ======
+
+END_EXPECTED
+
     'mandel.pir'        =>  << 'END_EXPECTED',
 ................::::::::::::::::::::::::::::::::::::::::::::...............
 ...........::::::::::::::::::::::::::::::::::::::::::::::::::::::..........

Reply via email to