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



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

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


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Expanding Shortened URL's

A Simple Parser for a Small Command Line Interface

A Review of Kevin Mitnick's Book Ghost in the Wires

FIF Isn't Forth

Tracing XSLT with a Tiny Java Web Server

Setting Text Color in a Batch File

Play MP3 Files with Python on Windows

BPL: Batch Programming Language Interpreter

CP/M Days

Understanding TRS-80 CMD Files


Full List of Posts

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


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service