[comp.lang.pascal] A NICE selfproduction-process

victor@cs.vu.nl (L. Victor Allis) (01/03/88)

The one thing which struck me seeing all those self-replicating
programs was the way they looked: UGLY.
Every program needed apart from the standard idea, some tricks to
solve the quotemarkproblem and most of them involved counting the
number of characters in the program.

Therefore I tried to write a Father program which will produce a
selfreplicating Son, without dirty tricks.
I must admit that I did not have to solve the quotemark problem, since I
used the ord() and chr() functions.
This method works on all machines as long as the Father program and the
resulting Son work on the same machine.

Furthermore I did not try to get a very short selfreplicater or Father.
Clarity was the main goal, and I think I succeeded. (Notice that all
programs have a nice layout).

To test it, feed the following program as input to itself.
The output is a selfreplicating program (which does not get input).
As you will see, the Father and Son are much alike, while the Grandson
will be identical to the Son.

--------------------------
program Reproduce(input, output);
const StartLines =   10;
      StopLines  =   27;
      Maximum    =   80;
var Start        : array[1..StartLines, 1..Maximum] of char;
    Stop         : array[1..StopLines, 1..Maximum] of char;
    StartLength  : array[1..StartLines] of integer;
    StopLength   : array[1..StopLines] of integer;
    i, j         : integer;
begin
  for i := 1 to StartLines do
    begin
      j := 0;
      while not eoln do
	begin
	  j := j + 1;  
	  read(Start[i, j])
	end;
      StartLength[i] := j;
      readln
    end;
  for i := 1 to 23 do readln;
  for i := 1 to StopLines do
    begin
      j := 0;
      while not eoln do
	begin
	  j := j + 1;  
	  read(Stop[i, j])
	end;
      StopLength[i] := j;
      readln
    end;
  for i := 1 to StartLines do
    begin
      for j := 1 to StartLength[i] do
	write(Start[i, j]);
      writeln
    end;
  for i := 1 to StartLines do
    begin
      for j := 1 to StartLength[i] do
	writeln('  Start[', i:1, ', ', j:1,
		'] := chr(', ord(Start[i, j]):1, ');');
      writeln('  StartLength[', i:1, '] := ', StartLength[i]:1, ';')
    end;
  for i := 1 to StopLines do
    begin
      for j := 1 to StopLength[i] do
	writeln('  Stop[', i:1, ', ', j:1,
		'] := chr(', ord(Stop[i, j]):1, ');');
      writeln('  StopLength[', i:1, '] := ', StopLength[i]:1, ';')
    end;
  for i := 1 to StopLines do
    begin
      for j := 1 to StopLength[i] do
	write(Stop[i, j]);
      writeln
    end
end.
--------------------------

Victor Allis.
Vrije Universiteit van Amsterdam.
The Netherlands.