All Source For parrot |
Ackermann's Function |
ACKERMANN:
set I1, 3
set I2, P0[1]
bsr ACK
print "Ack("
print I1
print ","
print I2
print "): "
print I0
print "\n"
end
ACK:
save I1
save I2
unless I1, ACK_M0
unless I2, ACK_N0
dec I2
bsr ACK
set I2, I0
dec I1
bsr ACK
branch ACK_END
ACK_M0:
set I0, I2
inc I0
branch ACK_END
ACK_N0:
dec I1
set I2, 1
bsr ACK
ACK_END:
restore I2
restore I1
ret
|
Array Access |
set I0, P0[1]
new P1, .Array
new P2, .Array
set P1, I0
set P2, I0
dec I0
set I1, 0
set I2, 1
X_LOOP: gt I1, I0, X_DONE
set P1[I1], I2
inc I1
inc I2
branch X_LOOP
X_DONE:
set I1, 0
Y_LOOP: gt I1, 999, Y_DONE
set I2, I0
Z_LOOP: lt I2, 0, Z_DONE
set I3, P2[I2]
set I4, P1[I2]
add I3, I4
set P2[I2], I3
dec I2
branch Z_LOOP
Z_DONE:
inc I1
branch Y_LOOP
Y_DONE:
set I1, P2[0]
print I1
print " "
set I1, P2[I0]
print I1
print "\n"
end
|
Fibonacci Numbers |
set I0, P0[1]
bsr FIBO
print I0
print "\n"
end
FIBO:
lt I0, 2, FIBO_ONE
set I1, I0
set I2, I1
sub I2, 2
set I0, I2
save I1
save I2
save I3
bsr FIBO
restore I3
restore I2
restore I1
set I1, I0
inc I2
set I0, I2
save I1
save I2
save I3
bsr FIBO
restore I3
restore I2
restore I1
add I0, I0, I1
branch FIBO_END
FIBO_ONE: set I0, 1
FIBO_END:
ret
|
Hash (Associative Array) Access |
# $Id: hash.parrot,v 1.0 2002/08/20 11:14:00 dada Exp $
# http://dada.perl.it/shootout/
set I1, P0[1]
new P1, .PerlHash
# new P2, .PerlInt
set I0, 1
BUILD:
bsr DEC2HEX
# set P2, I0
# set P1[S0], P2
set P1[S0], I0
inc I0
le I0, I1, BUILD
set I0, 0
COUNT:
set S0, I1
# print "GET hash("
# print S0
# print ")="
# set P2, P1[S0]
# typeof I2, P2
# typeof S2, P2
# print S2
set I2, P1[S0]
unless I2, NEXT
# eq I2, .PerlUndef, NEXT
# print " ++"
inc I0
NEXT:
# print "\n"
dec I1
if I1, COUNT
print I0
print "\n"
end
DEC2HEX:
set S0, ""
set I10, I0
DEC2HEX_START:
mod I11, I10, 16
lt I11, 10, DEC2HEX_N
eq I11, 15, DEC2HEX_F
eq I11, 14, DEC2HEX_E
eq I11, 13, DEC2HEX_D
eq I11, 12, DEC2HEX_C
eq I11, 11, DEC2HEX_B
eq I11, 10, DEC2HEX_A
DEC2HEX_N:
set S1, I11
concat S0, S1, S0
branch DEC2HEX_NEXT
DEC2HEX_A:
concat S0, "a", S0
branch DEC2HEX_NEXT
DEC2HEX_B:
concat S0, "b", S0
branch DEC2HEX_NEXT
DEC2HEX_C:
concat S0, "c", S0
branch DEC2HEX_NEXT
DEC2HEX_D:
concat S0, "d", S0
branch DEC2HEX_NEXT
DEC2HEX_E:
concat S0, "e", S0
branch DEC2HEX_NEXT
DEC2HEX_F:
concat S0, "f", S0
DEC2HEX_NEXT:
div I10, I10, 16
if I10, DEC2HEX_START
ret
|
Hashes, Part II |
# $Id: hash2.parrot,v 1.0 2002/08/19 17:25:00 dada Exp $
# http://dada.perl.it/shootout/
set I1, P0[1]
set I2, 0
new P1, .PerlHash
new P11, .PerlArray
set P11, 10000
HASH1:
set S0, "foo_"
set S1, I2
concat S0, S1
set P1[S0], I2
set P11[I2], S0
inc I2
lt I2, 10000, HASH1
new P2, .PerlHash
set I0, 1
HASH2:
set I3, P11
set I2, 0
HASH1_KEYS:
set S0, P11[I2]
set I4, P1[S0]
set I5, P2[S0]
add I5, I4
set P2[S0], I5
inc I2
le I2, I3, HASH1_KEYS
inc I0
le I0, I1, HASH2
set S0, "foo_1"
set I0, P1[S0]
set S1, I0
print S1
print " "
set S0, "foo_9999"
set I0, P1[S0]
set S1, I0
print S1
print " "
set S0, "foo_1"
set I0, P2[S0]
set S1, I0
print S1
print " "
set S0, "foo_9999"
set I0, P2[S0]
set S1, I0
print S1
end
|
Hello World |
print "hello world\n"
end
|
Nested Loops |
# $Id: nestedloop.parrot,v 1.0 2002/08/20 17:00:00 dada Exp $
# http://dada.perl.it/shootout/
.constant N I0
.constant A I1
.constant B I2
.constant C I3
.constant D I4
.constant E I5
.constant F I6
.constant X I7
set .N, P0[1]
set .X, 0
set .A, .N
A_LOOP:
set .B, .N
B_LOOP:
set .C, .N
C_LOOP:
set .D, .N
D_LOOP:
set .E, .N
E_LOOP:
set .F, .N
F_LOOP:
inc .X
dec .F
if .F, F_LOOP
dec .E
if .E, E_LOOP
dec .D
if .D, D_LOOP
dec .C
if .C, C_LOOP
dec .B
if .B, B_LOOP
dec .A
if .A, A_LOOP
print .X
print "\n"
end
|
Random Number Generator |
# $Id: random.parrot,v 1.0 2002/08/19 17:18:00 dada Exp $
# http://dada.perl.it/shootout/
.constant IM 139968
.constant IMf 139968.0
.constant IA 3877
.constant IC 29573
.constant LAST I10
set .LAST, 42
set I1, P0[1]
while:
set N0, 100
bsr gen_random
dec I1
if I1, while
print N0
end
gen_random:
#last = (last * IA + IC) % IM;
#return( max * last / IM );
mul I11, .LAST, .IA
add I11, .IC
cmod .LAST, I11, .IM
set N10, .LAST
mul N11, N0, N10
div N0, N11, .IMf
ret
|
Sieve of Erathostenes |
set I0, P0[1]
new P1, .Array
set P1, 8193
N_LOOP: unless I0, N_DONE
set I1, 0
set I2, 0
INIT_ARRAY: set P1[I2], 1
inc I2
lt I2, 8192, INIT_ARRAY
set I2, 2
I_LOOP: ge I2, 8192, I_DONE
set I4, P1[I2]
unless I4, I_NEXT
set I3, I2
add I3, I2
J_LOOP: ge I3, 8192, J_DONE
set P1[I3], 0
add I3, I2
branch J_LOOP
J_DONE: inc I1
I_NEXT: inc I2
branch I_LOOP
I_DONE: dec I0
branch N_LOOP
N_DONE: print "Count: "
print I1
print "\n"
end
|
String Concatenation |
# $Id: strcat.parrot,v 1.0 2002/08/20 17:29:00 dada Exp $
# http://dada.perl.it/shootout/
set I0, P0[1]
set S0, ""
LOOP:
concat S0, "hello\n"
dec I0
if I0, LOOP
length I0, S0
print I0
print "\n"
end
|
Sum a Column of Integers |
set I0, 0
AGAIN:
readline S0, 0
length I1, S0
le I1, 0, MAINLOOP
set I1, S0
add I0, I1
branch AGAIN
MAINLOOP:
print I0
print "\n"
end
|