Jul
15
2009
Implementing my own Forth interpreter is taking a little longer than anticipated. Each Forth word is almost like a puzzle. What’s the smallest number of words each can be written in? What’s the most efficient implementation? For an example, take a look at Implementing MIN in Forth without Conditional Code. The smallest version of MIN is 2 words shorter than eForth’s MIN.
Even words with a trivial implementation can pose an interesting problem. For example which of the following is most efficient:
Jones Forth TUCK (Corrected)
: TUCK DUP -ROT ;
Alternative TUCK
: TUCK SWAP OVER ;
If DUP, -ROT, SWAP and OVER are all primative, the alternative implementation will execute two fewer instructions on an 80×86 Forth. However, -ROT is often implemented in Forth which would cause the Jones Forth TUCK to be substantially slower. Here’s a typical implementation of -ROT:
: -ROT SWAP >R SWAP R> ;
Note: there’s an error in Jones Forth. The implementation of ROT and -ROT are reversed.
If you can think of an alternative two word implementation of TUCK or four word implementation of -ROT, please let me know.
Mar
25
2009
I’ve been curious about Forth ever since college. Now I’ve got some time on my hands, it seems like the ideal opportunity to investigate further.
Unfortunately the only Forth book I have is Advanced Spectrum Forth by Don Thomasson. The book covers the basics of Forth before moving on to more complex topics. The problem is it describes an ancient Forth standard, fig-Forth.
Modern Forth
After a quick seach, I turned up the ANS Forth Standard 1994 and the draft Forth 200x Standard. There are substantial differences to fig-Forth, which more or less makes the book useless.
I discovered a Forth IRC channel, #forth on irc.freenode.net where a community of Forth programmers gather. They gave me some pointers to some minimal Forth implementations:
However, the most useful pointers have been a couple of articles about implementing and moving Forth:
After reading through these, I’ve made a number of implementation decisions.
I’m planning to create a Forth interpreter in 8086 assembly, and later translate it to the AVR microcontroller on the Arduino board. I’ll use the processor stack for Forth’s data stack and use a register for the return stack. The top of stack element will be kept in a register.
However, there’s one small problem. At the end of the month I’ll be away from the Internet, spending a week on the beach. Can anyone recommend a good Forth book I can take with me?