! NOTE!!! The accompanying C source was generated by imptoc from this .imp source
! which was manually translated from the Algol 60 code. Note that Imp passes %name
! parameters by address and does not use the thunks that the Algol 60 call-by-name
! mechanism requires. In many instances this will produce the wrong result - but
! not in this particular case. A good translator from Algol 60 to C ought to be
! able to identify any case where pass-by-reference would work, and use that instead,
! which would lead to translations which look natural, like the one accompanying
! this file (in game_of_life-imp.c).
! The only deviation from the Algol original is that the boolean variable in
! countNeighboursAlive has been removed and the test placed inline. This was
! not primarily due to any limitation of Imp (an integer could have been used
! in place of the boolean) but was mainly because I had to earlier remove that
! boolean from the Algol as well when I had some issue with it not compiling
! under one of the modern Algol compilers. I since sorted that issue, but
! the modified code was simpler than the original and was how it should have
! been written in the first place anyway.
%begin
%routine %spec copytable (%integer %array (2) %name prevGen, %integer %array (2) %name table, %integer M, %integer N)
%routine %spec countNeighboursAlive (%integer %array (2) %name table, %integer M, %integer N, %integer x, %integer y, %integer %name Neighbours)
%comment Game of Life
%integer %array table(0:2,0:2)
%routine initialisetable (%integer %array (2) %name table, %integer M, %integer N)
%integer i,j
%for i = 0, 1, M-1 %cycle
%for j = 0, 1, N-1 %cycle
%if (i # j) %start
table(i,j) = 0
%else
table(i,j) = 1
%finish
%repeat
%repeat
%end
%routine printtable (%integer %array (2) %name table, %integer M, %integer N)
%integer i,j
%for i = 0, 1, M-1 %cycle
%for j = 0, 1, N-1 %cycle
write(table(i,j), 1)
%repeat
printstring(to string(NL))
%repeat
%end
%routine applyRules (%integer %array (2) %name table, %integer M, %integer N)
%integer Neighbours,actual,i,j
%integer %array prevGen(0:2,0:2)
copytable(prevGen,table,M,N)
%for i = 0, 1, M-1 %cycle
%for j = 0, 1, N-1 %cycle
actual = prevGen(i,j)
countNeighboursAlive (prevGen,M,N,i,j,Neighbours)
%if (actual = 1) %start
%if Neighbours < 2 %start
table(i,j) = 0
%else
%if Neighbours <= 3 %start
table(i,j) = 1
%else
table(i,j) = 0
%finish
%finish
%else
%if Neighbours = 3 %then table(i,j) = 1
%finish
%repeat
%repeat
%end
%routine countNeighboursAlive (%integer %array (2) %name table, %integer M, %integer N, %integer x, %integer y, %integer %name Neighbours)
%integer i,j
Neighbours = 0
%for i = x-1, 1, x+1 %cycle
%for j = y-1, 1, y+1 %cycle
%if i>=0 %and j>=0 %and i