home
table of contents
Comp Sci
September 2003
email

San sample code – a quining program

Here are some sample routines in the San language. The first one is the ever popular agent quine, the program that prints itself. There may well be a shorter version; however the quining mechanism is, I think, particularly clear in this version.

 
begin agent quine
   begin text q
      .print <"begin agent quine>
      .print <"   begin text q>
      .begin loop line from <split n q>[]
      .   print <"      {line}>
      .   end loop
      .print <"      end q>
      .put {q}
      .end quine
      end q
   print <"begin agent quine>
   print <"   begin text q>
   begin loop line from <split n q>[]
      print <"      .{line}>
      end loop
   print <"      end q>
   put {q}
   end quine
Code notes:

(1) Variables beginning with a back slash are reserved names; the contents of n is the newline character. Simlarly sp contains a single space.

(2) San has three distinct commands for sending data to an output port, emit, put, and print. These commands can have a port number appended to them, e.g., emit0, put1, and print2 write respectively to ports 0, 1, and 2. If there isn’t an appended port number then port 0 is used.

The emit command clears the variable or channel being emitted of the emitted value; the put and and print commands do not. The emit command uses variable mode, the put and print commands use text mode.

In variable mode the argument text is a list of variables; the contents of the variables are emitted. In text mode the argument text constitutes a string. For example:

        foo := "bageldorf
        print foo
        emit-word foo
sends two lines to channel 0 with only the first being terminated by a newline character. They are:
        foo
        bageldorf
If we had wanted to print the contents of foo we would have written
        print {foo} 
(3) In the expression, <split n q>[], the brackets are there to say that the split’s list is wanted. The split function takes a string (q in this case) and divides it into substrings. There are two arguments; the first argument contains the splitting characters; the second contains the string to be split. The split routine removes the splitting characters from the string, thereby breaking it into substrings.

(4) Note that the quote function uses text mode. In the command

        
        print <"      .{line}>
the contents of line replace {line}.


This page was last updated September 15, 2003.

home
table of contents
Comp Sci
September 2003
email