_____

Example self-reproducing programs

Downloads, first kind

Download, second kind

Descriptions of first kind implementations

C++

The representation of program source is a vector<string>, each element a line of the source.

All functions and definitions needed to create a self reproducing program are collected in class selfrep.

The desired mode the program is running in is given in the environment variable HELLO_TODO

  1. "original": print S1 (see the recipe)
  2. "replicate": print S2 (see the recipe)
  3. file name: print S2, using file name as name of the file containing the source
  4. blank or non-existent: run the original functionality

Note: sources S0, S1 and S2 are represented as s0.cpp, s1.cpp and s2.cpp, the compilates are s0, s1 and s2 respectively.

Note: 1, 2 and 4 to be run by the compilate of S1; 3 and 4 to be run by the compilate of S2

C

The representation of program source is a char**, each element a line of the source. The end is marked by NULL.

See under C++ for more details. (replace '.cpp' by '.c')

Fortran 90

The representation of program source is an array with the ASCII values of the characters.

All subroutines and definitions needed to create a self reproducing program are collected in module selfrepmodule.

The desired mode the program is running in is given in the environment variable HELLO_TODO

  1. "original": print S1 (see the recipe)
  2. "replicate": print S2 (see the recipe)
  3. file name: print S2, using file name as name of the file containing the source
  4. blank or non-existent: run the original functionality

Note: sources S0, S1 and S2 are represented as s0.f90, s1.f90 and s2.f90, the compilates are s0, s1 and s2 respectively.

Note: 1, 2 and 4 to be run by the compilate of s1.f90; 3 and 4 to be run by the compilate of s2.f90

Python

The representation of program source is an array of strings, one string per source-line.

All functions and definitions needed to create a self reproducing program are collected in class selfrep.

The desired mode the program is running in is given in the environment variable HELLO_TODO

  1. "original": print S1 (see the recipe)
  2. "replicate": print S2 (see the recipe)
  3. file name: print S2, using file name as name of the file containing the source
  4. blank or non-existent: run the original functionality

Note: sources S0, S1 and S2 are represented as s0.py, s1.py and s2.py

Note: 1, 2 and 4 to be run by s1.py; 3 and 4 to be run by s2.py

Matlab and Octave

The representation of program source is an array with the ASCII values of the characters.

Because of the peculiarities of Matlab and Octave, the self-replicating code is at the end of the original program.

The desired mode the program is running in is given as a parameter:

  1. 'original': print S1 (see the recipe)
  2. 'replicate': print S2 (see the recipe)
  3. 'file name': print S2, using file name as name of the file containing the source
  4. '': run the original functionality

Note: matlab produces a banner and a trailer on stdout, octave a superfluous empty line at the end of stdout. See the script 'doit' how to handle this.

Note: sources S0, S1 and S2 are represented as s0.m, s1.m and s2.m .

Note: 1, 2 and 4 to be run by s1.m; 3 and 4 to be run by s2.m