Forth

Forth Command Line and Programming Language.

April 24, 2014
by Andras
Comments Off on What is in a Hello World Program

What is in a Hello World Program

The simplest Hello World program in Forth is probably this:

: HELLO-WORLD ( -- ) ." Hello World!" ;

And you run it by typing its name, like so:

HELLO-WORLD

But what does it do behind the scenes?
The word ." is really just a programmer convenience even though it has been around almost as long as Forth has. But could we write the a Hello World program without it?

Of course we can:

: HELLO-WORLD ( -- ) S" Hello World!" TYPE ;

In fact the Embeddable Forth Command Interpreter published on this website generates the same code for these two — but most systems actually don’t.
And to show how Forth words are being created by extending Forth, here is a definition of the word S" (the version defined in CORE words) using other words in the standard:

: S" ( -- ) ( R: -- caddr count ) 
    [CHAR] " PARSE POSTPONE SLITERAL ; IMMEDIATE

Needless to say that the Forth standard defines S" as part of the CORE words while SLITERAL is defined in the optional STRING words. Talk about cart before the horse

Using these, here is a usable definition for ." :

: ." ( -- )  
     POSTPONE S" POSTPONE TYPE ; IMMEDIATE

Note: This really only works with the version of S" in the CORE words, later on it is redefined in the FILE ACCESS words to have a different execution semantics and the standard declares it non-standard use to apply POSTPONE to S" thereby making everyone’s life a lot harder than it needs to be.