An esoteric #forthlang

I say Ulrich Hoffman’s talk on PreForth and seedForth the other day. Although I don’t understand much in the way of the details, the basic idea is to create a bootstrapping Forth.

This gave me the idea of creating a very very simple Forth. It is 123 lines of C, and is available here . I’ve called it fin. I originally intended it to be written in Nim (Forth In Nim, hence fin), but quickly switched to C.

Fin is a strange old beast. You are allowed 256 words. Each word is a character long. It has a normal stack, but doesn’t need a return stack. The dictionary is very simple: it’s just a lookup on a char. Here are the predefined words in Fin:

Primitives: ~ d - c k e
Inner words: ; x a
Outer words: : r h

So far, 12 words exist. There are no immediate words in Fin, and the only things you can do in the outer loop are:

: - begin the definition of a word
r - run the latest-defined word
h - halt the program

‘:’ calls ‘define()’. It simply writes out what is on stdin up to and including the ‘;’.

‘r’ calls ‘run()’, equivalent of ‘docolon’. It sets the IP (instruction pointer), executing each word that it finds, and exits when it finds a ‘;’. There are two other special instructions. ‘x’ exits the word if the top of the stack is non-zero. ‘a’ jumps back to the start of the word.

Primitives are defined via C. They are:

~ - the not operatord - dupe the top of the stack
- - arithmetic minusk - 'key' - read a char from stdin
e - 'emit' - write a char to stdout

This gives us just enough to write a hello world application:

:Ckdc.-~xea;rHELLO WORLD!.h

Groovy! We define the word C as out main loop. We read a key, duplicate its value on the stack, and compare it to the full stop. If we find one, we exit. Otherwise we branch to the beginning of the word.

Pretty cool. We may be asking “OK, so what’s the point?” Well, despite being a very very limited Forth, we might want to see if we can push it in a couple of interesting ways:

  • Can we add a couple of words to maybe produce a quine (i.e. a program that outputs itself)?
  • Can we bootstrap it so that we can produce an actually useful Forth?

We’re going to at least needs some words that manipulate the heap and the IP (Instruction Pointer). The question is: what’s the minimum we can get away with?

Maybe we could end up with “levels” of Forth, with each level adding features, and working faster, than the previous layer. The sorts of things we would want to implement would be conditionals, words of more than one character, spaces separating words, a proper look-up dictionary, and so on.

I think the first thing to work on would be a quine. That would require some heap manipulation, as we’d need to store the input. We’d also need to be able to have the concept of variables, maybe.

I shall develop this further.

 

About mcturra2000

Computer programmer living in Scotland.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s