Welcome to Forth
Welcome to Forth, a mid-level, interactive, word-based language
(see below for a note about what "word-based" means.) In the language
abstraction spectrum, Forth lies somewhere between assembler and
system languages like C. Forth can be used as a "portable assembler",
but it also supports the simple construction of arbitrary higher-level
abstractions such as object-orientation. Forth's middle-of-the-road
abstraction level and interactive nature addresses the following
specific perceived shortcomings of other mainstream languages. Note
that while there are many languages with one or more of Forth's
features, Forth is unique in that it combines them all in one tool:
- Unlike many popular system languages, Forth is fully-interactive; no
edit/compile/test cycle is required. Instead, you can edit and run
code directly from the Forth command line. This means that
exploratory programming is very easy in Forth. (However, the fact
that control structures usually can't be used outside procedure
definitions somewhat limits one's ability to run ad-hoc code at
the command prompt.)
- Unlike many popular scripting languages, Forth can compile
directly to native code, which means you can have native-code
performance in a fully-interactive environment.
- Unlike many popular languages, Forth does not impose any
particular structure to your code (no "everything is an X"); you
are free to use procedural, structured, object-oriented, and
limited functional techniques as circumstances warrant. (However,
you may need to build your own toolbox.)
- Unlike virtually all popular languages, a complete Forth
development environment can run in a surprisingly small memory
footprint (a few 10's of K for a really large, highly-functional
implementation), and without an underlying general-purpose
operating system. Furthermore, the Forth compiler typically
produces very compact object code. This makes Forth ideal for
exploratory programming in embedded and resource-constrained
environments.
- Unlike most popular languages, Forth makes it possible and natural
to directly manipulate the hardware on which the Forth system
runs. Such direct manipulation can be done from the Forth command
line -- another advantage when doing exploratory development in
embedded environments.
- Unlike most popular languages, Forth is extensible - it allows the
programmer to add to the language facilities (such as control
structures) that are completely indistinguishable from the
built-in facilities. Forth takes you beyond the ability to design
libraries, and makes you a partner in the design of the language
itself; building a high-level language atop the Forth environment
is therefore fairly simple. Further, any such high-level language
can be made immediately available to the user as well as the
programmer. (Note: do not use this power in the service of evil!)
- Unlike nearly all popular languages, the internal structure of a
Forth development environment is extremely simple and easy to
understand, even for folks with little programming experience.
While to an experienced C programmer, for example, Forth may seem
to be full of strange inconsistencies and edge cases, a full
understanding of the nature of Forth makes it clear that all of
its features are natural consequences of its simple and elegant
implementation; and such a full understanding is easy to achieve.
- Because Forth permits the programmer both to manipulate
the low-level hardware interafaces of the system, and to build
arbitrary abstractions, it makes a very good language with which
to learn programming.
- The flip side of that is that in order to use Forth most
effectively -- and sometimes merely to use it correctly --
the programmer must throughly understand both the low-level
details and the high-level abstractions. That can be a good thing:
a wise man once said that a programmer working at a particular level
of abstraction should thoroughly understand both the level below
(so as to make proper use of the underlying facilities) and the level
above (so as to build genuinely useful tools). Forth strongly
encourages this.
Notes
Some time ago I was involved in a discussion of Forth evangelism
on comp.lang.forth. My position was that a lot of Forth
propaganda seems to be of the "Drink the Kool-Aid" variety --
cult members preaching to the converted. In an attempt to
develop a fair and unbiased description (to the extent such a
thing is possible), I came up with the text above. It is my
personal view of the Forth language and philosophy; if you
don't like it, that's your prerogative. If you do like it,
excellent :-)
What does "word-based" mean?
Forth is pretty much a context-free language. I consider a language
"Forth-like" if, when looking at a program in that language, all you
need to know to decide what to do next is, "what is the current
token?" That's a very important property, because means the language
is trivial to parse and nearly as trivial to interpret. C, for
example, does not have that property: if you're interpreting (not that
anyone does that) a C program, and you see a "(" token --- well, is it
the beginning of an expression delimiter? Is it a function-call
argument list delimiter? Is it a function definition argument-list
delimiter? You just cannot know without examining the surrounding
context, which may require a lot of lookahead or look-behind. In
Forth, the current word tells you what to do, period. It may make use
of some contextual data built by words previously executed, but there
is no doubt whatsoever about exactly what code should execute when a
word is encountered (leaving aside the infamous
"state-smartness"). Another way to say this is that in Forth, there
are no syntactic relationships between tokens; there are only
behavioral relationships between executed words; and therefore there
is no need for syntactic analysis, only behavioral analysis (aka
"execution"). This, I think, is what is meant when it's said that
Forth is a "word-based" language.