Tuesday, September 11, 2007

Introducing... sectorForth!

sectorForth is extreme, and I mean extreme. I believe it qualifies as esoteric too.

Each sectorForth program consists of up to 128 lines each with at most 5 words (a word being a sequence of characters delimited by whitespace). This means that even the most complicated sectorForth program is, at most, 640 words long.

If a line starts with a word (no whitespace preceding it), then it's a definition. Otherwise, it's a continuation and is only allowed to be 4 words long. I'll give you a little example:
fact    0? drop 1
dup 1- fact *

That's a little factorial function. The word 0? checks to see if the top item (on the stack) is 0. If it is, it continues execution on that line. Otherwise it goes to the next line and executes that. In plain old forth, this means

: fact (n -- n!) recursive dup 0 = if drop 1 else dup 1- fact * then ;

Needless to say, I prefer sectorForth's way.

sectorForth has up to 128 predefined words. Among these you have:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 32 64 128 256 512 1024 4096 65536 -7 -6 -5 -4 -3 -2 -1
+ - * / 1- 1+ | & ^ >> << 8<<| neg
? 0? _? = != > < >= <= >? <?
dup swap over rot -rot drop { }
emit . open close in out :in :out
@ ! A B :A :B A@ A! B@ B! A@+ A!+ B@+ B!+ @@ !! A@@ A!! B@@ B!! A@@+ A!!+ B@@+ B!!+ word* new free
call cur+

Nifty, but... why?!

The point of sectorForth is to fit one program into 512 bytes (the size of a sector on most disks). This is done by restricting the number of words you can define (up to 128) and the length of each word (up to 4 calls per word). Since you can define 128 words, and you have 128 predefined words... you can have up to 256 distinct calls.

This means you only need one byte to represent a single call. Since a word is 4 calls, you need 32bits (an int by todays standards) for a word. And 128 * 4 = 512. :)

Actual practical uses? Very few. Maybe in an esoteric OS. Hmm...