1 # Copyright (C) 2001-2008, Parrot Foundation.
6 examples/pir/life.pir - Conway's Life
10 % ./parrot examples/pir/life.pir
14 Runs Conway's Life cellular automata
15 (L<http://ddi.cs.uni-potsdam.de/HyFISCH/Produzieren/lis_projekt/proj_gamelife/ConwayScientificAmerican.htm>).
19 Convert this into proper PIR.
23 F<examples/library/ncurses_life.pir>.
29 .local int max_generations
31 jmpstack = new 'ResizableIntegerArray'
33 # First the generation count
35 if $I15 < 2 goto USE_DEFAULT_MAX_GENERATIONS
40 print " generations.\n"
41 goto MAX_GENERATIONS_IS_NOW_KNOWN
42 USE_DEFAULT_MAX_GENERATIONS:
43 print "Running 5000 generations by default.\n"
45 MAX_GENERATIONS_IS_NOW_KNOWN:
50 # If true, we don't print
83 local_branch jmpstack, dump
85 loop: ge $I0, $I2, getout
92 local_branch jmpstack, generate
94 local_branch jmpstack, dump
100 print " generations in "
106 print " generations/sec\n"
111 print " bytes were allocated\n"
116 print " GC runs were made\n"
121 print " collection runs were made\n"
124 print "Copying a total of "
131 print " active Buffer structs\n"
136 print " total Buffer structs\n"
141 # $S15 has the incoming string, $S0 is scratch, $S1 is scratch, $S2 is scratch
143 # $I0 is the length of the string
144 # $I1 is the current cell we're checking
145 # $I2 is the count for that cell
146 # $I3 is the offset to the neighbor
148 .local int save_I0, save_I1, save_I2, save_I3
163 # $S0 is always overwritten, so reuse it
164 substr $S0, $S15, $I3, 1
172 substr $S0, $S15, $I3, 1
180 substr $S0, $S15, $I3, 1
188 substr $S0, $S15, $I3, 1
196 substr $S0, $S15, $I3, 1
204 substr $S0, $S15, $I3, 1
212 substr $S0, $S15, $I3, 1
220 substr $S0, $S15, $I3, 1
224 substr $S0, $S15, $I1, 1
225 eq $S0, "*", check_alive
227 # If eq 3, put a star in else a space
251 local_return jmpstack
253 # $S15 has the incoming string, $S0 is scratch
257 print "\n\n\n\n\n\n\n\n\n\n\n"
258 print "------------- generation "
260 print " -------------\n"
264 substr $S0, $S15, $I10, 15
269 ge $I11, 0, printloop
272 local_return jmpstack
280 # vim: expandtab shiftwidth=4 ft=pir: