Page 132 - The Unofficial Guide to Lego Mindstorms Robots
P. 132
121
There are other options, as well. Appendix B, A pbFORTH Downloader, contains source code for a Java-based program
downloader. Furthermore, the pbFORTH web site has pbFORTH tools written in Tcl/Tk.
About Forth
In this section, I'll briefly describe the Forth language itself. Then I'll detail pbFORTH's RCX-specific words and present some
short examples of robotic programming with pbFORTH.
The Stack
Forth is a simple but powerful language. It is based around the idea of a stack. A stack is just what it sounds like, like a stack of
trays in one of those spring-loaded carts at a cafeteria. You can add something to the stack or remo ve the top item. Adding an
item to th e stack is a push. Retrieving the top item is a pop. A pop always returns the last item that was pushed.
Assuming you've al ready gotten pbFORTH running on your RCX, let's see how this works. Let's begin by pushing a number
on the stack. Type a number into your terminal emulator and hit return. pbFORTH resp onds with a friendly "ok":
77
ok
N ow type a single period and press return. The period pops a value off the stack and prints it out to the console, along with that
ubiquitous "ok":
.
77 ok
Yo u can push multiple values on the stack by separating them with spaces. Similarly, you can pop more than one value off the
stack with more than one period, like this:
77 12 55
ok
. .
55 12 ok
.
77 ok
If you understand the stack, you know almost everything there is to know about Forth.
Words and the Dictionary
Th e other important concept in Forth is the dictionary. This is simply a list of words and what they mean. A word is a simply a
program w ith a name. To run it,