Tikalon Blog is now in archive mode.
An easily printed and saved version of this article, and a link
to a directory of all articles, can be found below: |
This article |
Directory of all articles |
Forth
November 10, 2010
The availability of inexpensive computer memory and phenomenal computation speed has enabled a plethora of computer languages. In the beginning there was just
machine language, followed closely by its enabler,
assembly language. When you just have a few bytes of program memory to work with, you need to have compact programs, often implemented with a few
programmer's tricks, to do anything useful. When memory finally allowed
compilation of
high level languages, such as
COBOL and
Fortran, the compilers were optimized to produced compact code. More importantly, the compilers were
compilers.
Interpreted languages were not useful, since the computers were too slow.
After a time,
computer engineering caught up with
computer science, which enabled some very useful interpreted languages, such as
APL and
BASIC. Memory became inexpensive, and soon we had other interpreted languages, like
PHP, that contain all the
bells and whistles you would ever need; so many, in fact, you don't know
half of them until you need them. In my computer history, I've programmed in many languages. I've even flipped switches to put machine language programs into
primitive microprocessors, but for general purposes, the evolution of my programming has mirrored the evolution of programming in general. In chronological order, there was Fortran, APL,
Pascal,
Visual Basic,
C, and PHP, with short skirmishes in
Lisp and
Perl. So many languages were coming into existence in the early days of personal computing that there was even a magazine, Computer Language, devoted to the topic (see figure).
Cover of the August, 1988, issue of Computer Language (vol. 5, no. 8). (Click for larger image)
Although I haven't programmed in it for twenty years, my favorite language is
Forth. Forth was one of the first interpreted languages that could run on a primitive microprocessor. It was very close to the machine, so it was an excellent control language, and I used it to automate an
epitaxial crystal growth laboratory. It may have been my early interest in
radio astronomy that led me into Forth, since its inventor,
Charles H. Moore, developed it at the
US National Radio Astronomy Observatory to control radio telescopes. During the 1970s, as co-founder of FORTH, Inc., he ported the language to many different computer architectures.
Rockwell International introduced the
AIM-65 microcomputer in 1976 with a Forth operating system, and I used this in my early automation system which evolved into an
STD-Bus system programmed in Forth.
Although developers have played with its implementation over the years, Forth began as a threaded interpretive language. What this means, in effect, is that each
function or subroutine in a program (called a "word" in Forth) has a
pointer associated with it that tells the interpreter the next place to go to execute code. This makes for a fast interpreter. Forth is also a
stack language in which the arguments for each function are found on a stack, and the output of a function is left on a stack. In this way, its operation is somewhat like
Reverse Polish Notation, so adding two numbers would occur by the code
1 2 + which puts
1 and
2 on the stack and leaves a
3 there for the next operation. This sequence as it appears on a Linux terminal running Gforth appears below. As you can see, Forth is a very reassuring language. It tells you "ok" after a carriage return if what you've typed makes sense. The primitive word,
.s, let's you look at the stack without disturbing it.
1 2 + ok
.s <1> 3 ok
One interesting feature of Forth is that Forth is written in Forth. After writing a basic machine language
kernel, Moore was able to write the language in its own syntax, since every "word" that's defined automatically extends the language. This is my favorite Forth feature. In my epitaxy automation system, an operator could just type
cool on a terminal, and the furnace would cool down. How fast? If you typed
50 set_rate prior to that, the cool-down would be at 50 degrees an hour. It wasn't a
case statement in some program that figured out what
cool or
set_rate meant. These words were built into the language after I had defined them.
Some predefined words in Forth (Gforth on Ubuntu).
In parting, a few words of advice, depending on your language. Don't forget your colons (Pascal), semicolons (C and many others), and dollar signs (PHP).
References:
- Gforth documentation.
- Linux Goodies - Gforth Review.
- Forth Stuff, by Don Hopkins.
Permanent Link to this article
Linked Keywords: Machine language; assembly language; reentrant subroutine; compiler; high level languages; COBOL; Fortran; Interpreted languages; computer engineering; computer science; APL; BASIC; PHP; bells and whistles; Levenshtein distance; arxiv.org/abs/1004.4940; Intel 8080; Pascal; Visual Basic; C; Lisp; Perl; Computer Language Magazine (August 1988) Cover; Forth; epitaxial crystal growth; radio astronomy; Charles H. Moore; US National Radio Astronomy Observatory; Rockwell International; AIM-65; STD-Bus; function or subroutine; pointer; stack language; Reverse Polish Notation; kernel; case statement; Gforth; Ubuntu.