Collection policy was Re: No space for vinatge computers in
> > : GREETING ." Hello World !" CR ;
> Aargh! Another FORTH coder :-)
Of course... Here's a _very_ brief introduction to Forth.
Forth consists of 'words' sepeared by whitespace. When the Forth system
reads your command line, it looks up each word in its 'dictionary'. If it
can find it, it executes it. If not, it attempts to translate the
sequence of characters as a number in the current base. If that fails
too, it gives an error message.
Forth arithmetic is done using reverse polish notation (it virtually has
to be using that method of esecution), and is often integer only.
So, when you get your Forth system running, type something like
2 3 +
(the spaces are, of course important, although you can type more than 1
if you like). This pushs 2 on the stack (the first thing was not found in
the dictionary, so was taken as a numbner [1]), then pushes 3, then
executes the word '+' when pops 2 numbers off the stack ans pushes the
sum. The stack now contains 5.
[1] OK, experienced Forth hackers will realise I've simplified things
here. It's common to have small numbers (generally -1, 0, 1, 2, 3 and
maybe others) defined as words in the dictionary so as to speed up
execution. These words just push the obvious value onto the stack when
executed. But there's no reason to have these defined -- the system can
run without them.
But you can't see it. The forth word . (that's a single period, and is
pronounced 'dot') pops the top of stack and displays it as a number in
the currnet base. Try it. You should get '5' displayed somewhere. The
stack is now empty.
So far it's just an integer-only calculator. What makes Forth interesting
is that you can add your own words to the dictionary (in fact that's how
you generally program in Forth, you just keep on defining new words).
There are sevveral ways to do this, the simplest is the 'colon
definition'. This defines a new word (immediately following a colon) in
terms of existing words. The definition is ended by a semicolon.
OK, let's try that
: SQ DUP * ;
This defines a new word called 'SQ' (for 'square') as being the same as
DUP *. That means duplicate the number on top of stack, then multiply the
2 numbers on the stack. This, of course, multiplies the top of stack by
itself (a reasonable way to square it :-)).
OK, yuou can now try out the new definition
4 SQ .
(don't forget the 'dot') should display 16
Or 4 SQ 3 SQ + .
should display 25
Note you can use 'home made' words just like the ones in the system. You
can even use them in new definitions, e.g.
; 4TH-POWER SQ SQ ;
Obviously there's a lot more to the language than that (loops,
conditionals, etc), but that should be a start.
-tony
Received on Mon May 26 2003 - 17:31:29 BST
This archive was generated by hypermail 2.3.0
: Fri Oct 10 2014 - 23:36:16 BST