Program that prints itself 
Author Message
 Program that prints itself

Recently someone posted a qusetion about Oberon program that print itself.
The very old question. The easiest solution is in classic BASIC:

10 LIST

However, it makes no sense to create such a program in Oberon, becuase in
Oberon we do not execute programs, but commands. So, do we need to print the
procedure or the whole module?

Inseated, here is my solution in Modula-2. The only imported module is
TermIO with basic IO operations - replace it as you like with whatever you
like.

MODULE Autogen;

(* Program printing itself *)

IMPORT
  TermIO;

VAR
  Line : ARRAY [1..99], [0..79] OF CHAR;

PROCEDURE WriteLine(x, y: CARDINAL; full: BOOLEAN);
  VAR
    i: CARDINAL;
  BEGIN
    FOR i:=x TO y DO
      IF full THEN
        TermIO.WriteStr("  Line[");
        TermIO.WriteCard(i, 2);
        TermIO.WriteStr("]:=");
        TermIO.Write(47C);
      END;
      TermIO.WriteStr(Line[i]);
      IF full THEN
        TermIO.Write(47C);
        TermIO.WriteStr(";");
      END;
      TermIO.WriteLn;
    END;
  END WriteLin;

BEGIN
  Line[ 1]:='MODULE Autogen;';
  Line[ 2]:='';
  Line[ 3]:='(* Program printing itself *)';
  Line[ 4]:='';
  Line[ 5]:='IMPORT';
  Line[ 6]:='  TermIO;';
  Line[ 7]:='';
  Line[ 8]:='VAR';
  Line[ 9]:='  Line : ARRAY [1..99], [0..79] OF CHAR;';
  Line[10]:='';
  Line[11]:='PROCEDURE WriteLine(x, y: CARDINAL; full: BOOLEAN);';
  Line[12]:='  VAR';
  Line[13]:='    i: CARDINAL;';
  Line[14]:='  BEGIN';
  Line[15]:='    FOR i:=x TO y DO';
  Line[16]:='      IF full THEN';
  Line[17]:='        TermIO.WriteStr("  Line[");';
  Line[18]:='        TermIO.WriteCard(i, 2);';
  Line[19]:='        TermIO.WriteStr("]:=");';
  Line[20]:='        TermIO.Write(47C);';
  Line[21]:='      END;';
  Line[22]:='      TermIO.WriteStr(Line[i]);';
  Line[23]:='      IF full THEN';
  Line[24]:='        TermIO.Write(47C);';
  Line[25]:='        TermIO.WriteStr(";");';
  Line[26]:='      END;';
  Line[27]:='      TermIO.WriteLn;';
  Line[28]:='    END;';
  Line[29]:='  END WriteLine;';
  Line[30]:='';
  Line[31]:='BEGIN';
  Line[32]:='  WriteLine( 1, 29, FALSE);';
  Line[33]:='  WriteLine( 1, 33; TRUE);';
  Line[34]:='  WriteLine(30, 33, FALSE);';
  Line[35]:='END Autogen.';

  WriteLine( 1, 31, FALSE);
  WriteLine( 1, 35, TRUE);
  WriteLine(32, 35, FALSE);
END Autogen.



Mon, 22 Oct 2001 03:00:00 GMT  
 Program that prints itself

Quote:

> Recently someone posted a qusetion about Oberon program that print itself.
> The very old question. The easiest solution is in classic BASIC:

> 10 LIST

> However, it makes no sense to create such a program in Oberon, becuase in
> Oberon we do not execute programs, but commands. So, do we need to print the
> procedure or the whole module?

> Inseated, here is my solution in Modula-2. The only imported module is
> TermIO with basic IO operations - replace it as you like with whatever you
> like.

> MODULE Autogen;

> (* Program printing itself *)

> IMPORT
>   TermIO;

> VAR
>   Line : ARRAY [1..99], [0..79] OF CHAR;

> PROCEDURE WriteLine(x, y: CARDINAL; full: BOOLEAN);
>   VAR
>     i: CARDINAL;
>   BEGIN
>     FOR i:=x TO y DO
>       IF full THEN
>         TermIO.WriteStr("  Line[");
>         TermIO.WriteCard(i, 2);
>         TermIO.WriteStr("]:=");
>         TermIO.Write(47C);
>       END;
>       TermIO.WriteStr(Line[i]);
>       IF full THEN
>         TermIO.Write(47C);
>         TermIO.WriteStr(";");
>       END;
>       TermIO.WriteLn;
>     END;
>   END WriteLin;

> BEGIN
>   Line[ 1]:='MODULE Autogen;';
>   Line[ 2]:='';
>   Line[ 3]:='(* Program printing itself *)';
>   Line[ 4]:='';
>   Line[ 5]:='IMPORT';
>   Line[ 6]:='  TermIO;';
>   Line[ 7]:='';
>   Line[ 8]:='VAR';
>   Line[ 9]:='  Line : ARRAY [1..99], [0..79] OF CHAR;';
>   Line[10]:='';
>   Line[11]:='PROCEDURE WriteLine(x, y: CARDINAL; full: BOOLEAN);';
>   Line[12]:='  VAR';
>   Line[13]:='    i: CARDINAL;';
>   Line[14]:='  BEGIN';
>   Line[15]:='    FOR i:=x TO y DO';
>   Line[16]:='      IF full THEN';
>   Line[17]:='        TermIO.WriteStr("  Line[");';
>   Line[18]:='        TermIO.WriteCard(i, 2);';
>   Line[19]:='        TermIO.WriteStr("]:=");';
>   Line[20]:='        TermIO.Write(47C);';
>   Line[21]:='      END;';
>   Line[22]:='      TermIO.WriteStr(Line[i]);';
>   Line[23]:='      IF full THEN';
>   Line[24]:='        TermIO.Write(47C);';
>   Line[25]:='        TermIO.WriteStr(";");';
>   Line[26]:='      END;';
>   Line[27]:='      TermIO.WriteLn;';
>   Line[28]:='    END;';
>   Line[29]:='  END WriteLine;';
>   Line[30]:='';
>   Line[31]:='BEGIN';
>   Line[32]:='  WriteLine( 1, 29, FALSE);';
>   Line[33]:='  WriteLine( 1, 33; TRUE);';
>   Line[34]:='  WriteLine(30, 33, FALSE);';
>   Line[35]:='END Autogen.';

>   WriteLine( 1, 31, FALSE);
>   WriteLine( 1, 35, TRUE);
>   WriteLine(32, 35, FALSE);
> END Autogen.

Even here striving for simplicity pays off...

The following program works in Oberon System (S3 and V4) and prints
itself to the System.Log when its exported command is activated.

MODULE Print; (* Hans Klaver, 9 May 1999 *)
(** To execute middle-click Print.Me **)

  IMPORT In, Out;

  PROCEDURE Me*;
    VAR ch: CHAR;
  BEGIN
    In.Open;
    In.Char(ch);
    WHILE In.Done DO
      Out.Char(ch);
      In.Char(ch)
    END
  END Me;

END Print.Me*

Which is evidence that even in "demented" programming tasks Oberon
offers a perfectly simple solution!

It is interesting to note that the command Print.Me* also prints any
changes made in the program text, even before these are stored to disk.

In BlackBox this program doesn't work unchanged due to the different way
Component Pascal's implementation of Out.Char handles non-printing
characters such as 9X and 0DX. Also in BB the execution of commands and
the selection of input is a bit different:

MODULE Print; (* Hans Klaver, 9 May 1999 *)
(**
  To execute put a commander in front of Print.Me,
  then select the start of the program text and click the commander.
*)

  IMPORT In, Out;

  PROCEDURE Me*;
    VAR ch: CHAR;
  BEGIN
    In.Open;
    In.Char(ch);
    WHILE In.Done DO
      CASE ch OF
      | 09X: Out.String("   ");
      | 0DX: Out.Ln;
      | 02X:
      ELSE
        Out.Char(ch)
      END;
      In.Char(ch)
    END
  END Me;

END Print.

Hans Klaver

Just because today is my birthday...



Thu, 25 Oct 2001 03:00:00 GMT  
 Program that prints itself

Quote:

>Even here striving for simplicity pays off...

>The following program works in Oberon System (S3 and V4) and prints
>itself to the System.Log when its exported command is activated.

>MODULE Print; (* Hans Klaver, 9 May 1999 *)
>(** To execute middle-click Print.Me **)

>  IMPORT In, Out;

>  PROCEDURE Me*;
>    VAR ch: CHAR;
>  BEGIN
>    In.Open;
>    In.Char(ch);
>    WHILE In.Done DO
>      Out.Char(ch);
>      In.Char(ch)
>    END
>  END Me;

>END Print.Me*

>Which is evidence that even in "demented" programming tasks Oberon
>offers a perfectly simple solution!

>It is interesting to note that the command Print.Me* also prints any
>changes made in the program text, even before these are stored to disk.

>In BlackBox this program doesn't work unchanged due to the different way
>Component Pascal's implementation of Out.Char handles non-printing
>characters such as 9X and 0DX. Also in BB the execution of commands and
>the selection of input is a bit different:

>MODULE Print; (* Hans Klaver, 9 May 1999 *)
>(**
>  To execute put a commander in front of Print.Me,
>  then select the start of the program text and click the commander.
>*)

>  IMPORT In, Out;

>  PROCEDURE Me*;
>    VAR ch: CHAR;
>  BEGIN
>    In.Open;
>    In.Char(ch);
>    WHILE In.Done DO
>      CASE ch OF
>      | 09X: Out.String("   ");
>      | 0DX: Out.Ln;
>      | 02X:
>      ELSE
>        Out.Char(ch)
>      END;
>      In.Char(ch)
>    END
>  END Me;

>END Print.

>Hans Klaver

>Just because today is my birthday...

Happy Birthday, Hans!!!

I agree, your solution is much simpler, and it is really in the Oberon
spirit. My new "demented" programming task (appropriate for Oberon) would
be:

Write an Oberon program that writes its copy in another viewer (and with
another name), then compile this copy, then execute.... Stop, it becomes a
virus.

Anes Sadikovic



Thu, 25 Oct 2001 03:00:00 GMT  
 Program that prints itself

Quote:

> The following program works in Oberon System (S3 and V4) and prints
> itself to the System.Log when its exported command is activated.

Your program is elegant, but does not really comply to the
(demented) spirit of the problem :-)  You can write a similarly
elegant program in any programming language using this pattern:

  open file containing my source code
  for all characters in file, print character

-- Pieter

--
Pieter Muller, Institute for Computer Systems, ETH Zurich
Native Oberon OS: http://www.oberon.ethz.ch/native/



Mon, 05 Nov 2001 03:00:00 GMT  
 Program that prints itself

Quote:


> > The following program works in Oberon System (S3 and V4) and prints
> > itself to the System.Log when its exported command is activated.

> Your program is elegant, but does not really comply to the
> (demented) spirit of the problem :-)  You can write a similarly
> elegant program in any programming language using this pattern:

>   open file containing my source code
>   for all characters in file, print character

Yes, but the interesting thing is that Oberon (and BlackBox b.t.w.)
doesn't need a file to accomplish the same task: the source code is read
directly from the text in RAM. So compliance with the spirit of the
problem is better than one would think at first sight ;-)

Hans Klaver



Mon, 05 Nov 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Program that prints itself

2. ADVICE: Code that prints itself

3. Q: Making program both callable from TCL and to call itself with TCL

4. PRINT PREVIEWER FREEWARE - FormView - Views HP PCL Print Output / Prints PCL to ANY WINDOWS PRINTER

5. DolphinSure by itself...

6. listbox updating itself after an SQLquery

7. Doing math from input data itself

8. Can a form call itself when done?

9. Forcing an exe to delete itself (CFW4b - Legacy)

10. How to make a View remove itself?

11. ParcPlace Image knows how to start itself

12. App Resizing by itself, when opening a browse.

 

 
Powered by phpBB® Forum Software