1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#!./parrot
# Copyright (C) 2006-2009, Parrot Foundation.
#
# Ack by Leopold Toetsch
# Fib and Tak by Joshua Isom
# modified default value to n=3. Karl Forner
# ./parrot -Oc recursive.pir N
.sub main :main
.param pmc argv
.local int argc, n
argc = argv
n = 3
unless argc == 2 goto argsok
$S0 = argv[1]
n = $S0
argsok:
$P0 = getinterp
$P0.'recursion_limit'(100000)
.local pmc array
array = new 'FixedFloatArray'
array = 11
dec n
$I0 = n + 1
$I1 = ack(3, $I0)
array[0] = $I0
array[1] = $I1
$N0 = 28.0 + n
array[2] = $N0
$N0 = FibNum($N0)
array[3] = $N0
$I0 = n * 3
$I1 = n * 2
array[4] = $I0
array[5] = $I1
array[6] = n
$I0 = TakInt($I0, $I1, n)
array[7] = $I0
$I0 = FibInt(3)
array[8] = $I0
$N0 = TakNum(3.0, 2.0, 1.0)
array[9] = $N0
$S0 = sprintf <<"END", array
Ack(3,%d): %d
Fib(%.1f): %.1f
Tak(%d,%d,%d): %d
Fib(3): %d
Tak(3.0,2.0,1.0): %.1f
END
print $S0
.end
.sub ack
.param int x
.param int y
if x goto a1
$I0 = y + 1
.return ($I0)
a1:
if y goto a2
$I0 = x - 1
$I1 = 1
.tailcall ack($I0, $I1)
a2:
$I2 = y - 1
$I3 = ack(x, $I2)
$I4 = x - 1
.tailcall ack($I4, $I3)
.end
.sub FibInt
.param int n
unless n < 2 goto endif
.return(1)
endif:
.local int tmp
tmp = n - 2
$I0 = FibInt(tmp)
tmp = n - 1
$I1 = FibInt(tmp)
$I0 += $I1
.return($I0)
.end
.sub FibNum
.param num n
unless n < 2 goto endif
.return(1.0)
endif:
.local num tmp
tmp = n - 2
$N0 = FibNum(tmp)
tmp = n - 1
$N1 = FibNum(tmp)
$N0 += $N1
.return($N0)
.end
.sub TakNum
.param num x
.param num y
.param num z
unless y >= x goto endif
.return(z)
endif:
.local num tmp
tmp = x - 1.0
$N0 = TakNum(tmp, y, z)
tmp = y - 1.0
$N1 = TakNum(tmp, z, x)
tmp = z - 1.0
$N2 = TakNum(tmp, x, y)
.tailcall TakNum($N0, $N1, $N2)
.end
.sub TakInt
.param int x
.param int y
.param int z
unless y >= x goto endif
.return(z)
endif:
.local int tmp
tmp = x - 1
$I0 = TakInt(tmp, y, z)
tmp = y - 1
tmp = TakInt(tmp, z, x)
dec z
z = TakInt(tmp, x, y)
.tailcall TakInt($I0, tmp, z)
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir:
|