Jim Lawless' Blog


A Quine in Forth

Originally published on: Tue, 04 Aug 2009 02:47:56 +0000

I needed a little mental exercise tonight, so I chose to write a quine ( a program that produces its own source as its output ) in the Forth programming language?

Why Forth? Because I'm a novice Forth programmer at best. I don't think very well in Forth, so this took some effort. Here's the quine: ( the source for this quine is in the public domain )


s" swap dup 1 type 34 emit 32 emit over over swap type 34 emit 32 emit swap type" swap dup 1 type 34 emit 32 emit over over swap type 34 emit 32 emit swap type

I tested the above in immediate mode in Win32Forth.

The first portion:

s" ... to the ending " leaves the address and length of a counted string on the stack. By following that with swap dup 1, I was able to make a counted version of the string with a length of 1. A call to the type word then displays the letter "s" from the beginning of the string "swap".

emit 34 emit 32 causes a double-quote and a space to appear. So, the only thing I have left to do is to display the string, a dquote and the string again and I'm done.

I do that by using the remaining code over over swap type 34 emit 32 emit swap type.

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless

del_icio_us Save to del.icio.us
stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks



Previous post: A Command-Line MP3 Player for Windows
Next post:BSave and BLoad for the Commodore 64


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

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

Taking Shape

We've Moved!


Popular Posts

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


Random Posts

Switching a Console Window to Full Screen or Windowed Mode

Internet Protocols and Rhino JavaScript

BSave and BLoad for the Commodore 64

A Lightweight Alternative to Windows Shortcuts

WSH2EXE part 1

A Simple Associative Array Library in C

An Interview with Game Developer James Hague

An Interview with Brad Templeton

Directory Traversal in Rhino JavaScript

Changing the C64 Text Color in C


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer