Originally published on: Tue, 28 Apr 2009 00:11:12 +0000
A quine is a program that produces its own source as its output.
I'd never written one before, so on Pi Day of this year ( 3/14 ) I gave it a go. Here's the result of my effort. You'll need to scroll waaaay over to the right ...
The issue that I found myself dealing with was the string metacharacters the slash and the double-quote. If I used any of the metacharacters in the source string, I would need to find a way to regenerate the character in its escaped form as well as the raw form.
My solution was to loop through the string s and use my own metacharacter @ to indicate that the next two digits will represent the ASCII character that is to be written. In checking for the metacharacter, I use the ASCII value of @ (64), so if you're trying this out on a machine that uses EBCDIC or something other than ASCII, the code won't work.
The appearance of @10 in the string will cause the program to output a character with an ASCII value of ten ( a newline. ) The appearance of @34 in the string will yield a double-quotation mark. @59 will yield a semicolon.
The next metacharacter my routine uses is the tilde ~ character to denote the end of the prologue of the output. The quine is really three sections wide.
I abbreviated putchar() as E() with a macro so that the source wouldn't be any lengthier than it needed to be.
To generate the quine, the code first outputs the prologue, then a double-quote, then the complete source string, then another double-quote, then a semicolon, then a preserved section of the source-string that appears after the tilde ( the main() function. )
Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless
Save to del.icio.us
Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks
Subscribe!
Auto Save Clipboard Images Redux
Extending SpiderMonkey JavaScript on Windows
Rhino JavaScript to EXE with launch4j
Compiling Rhino JavaScript to Java
Directory Traversal in Rhino JavaScript
A Command-Line MP3 Player for Windows
Auto Save Images from the Clipboard
Java in a Windows EXE with launch4j
An Interview with Tom Zimmer: Forth System Developer
Setting Windows Console Text Colors in C
Speeding up JRuby with NailGun
A TCP Command Line Interface in Rhino JavaScript
Changing the C64 Text Color in C
An Interview with Game Developer James Hague
Preserving my Favorite HN Links
MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer