|
| |
| Navigation |
Synopsis A self-reproducing program.
Description A quine
At The Quine Page Learning about quines, is about learning how to quote and escape symbols in strings.
Examples
module demo::basic::Quine
import IO;
import String;
public void quine(){
println(program); If you look at the source code, is a remarkable point: the string variable program has as value
the text of the module Quine upto . Note that the definition of program ends at .
Also observe that this string has a mesmerizing amount of escapes to which we will come back in a moment.
The function quine prints the string program twice:
") and backslash (\) in strings.
Let's do a simple experiment: And indeed, the two quotes are now properly escaped. This is exactly what happens at in the definition of quine:
println("\"" + escape(program, ("\"" : "\\\"", "\\" : "\\\\")) + "\";");
We escape program and replace " by \", and \ by \\.
The mesmerizing amount of \ characters can be explained due to escaping " and \.
Now let's put quine to the test.
rascal>import demo::basic::Quine; ok rascal>quine(); module demo::basic::Quine import IO; import String; public void quine(){ println(program); println("\"" + escape(program, ("\"" : "\\\"", "\\" : "\\\\")) + "\";"); } str program = "module demo::basic::Quine import IO; import String; public void quine(){ println(program); println(\"\\\"\" + escape(program, (\"\\\"\" : \"\\\\\\\"\", \"\\\\\" : \"\\\\\\\\\")) + \"\\\";\"); } str program ="; okIf you follow this output line-by-line you will see that it is identical to the original source code of module Quine.
|