This page (revision-4) was last changed on 03-Feb-2023 15:21 by Carsten Strotmann 

This page was created on 25-Apr-2010 20:23 by Carsten Strotmann

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Page revision history

Version Date Modified Size Author Changes ... Change note
4 03-Feb-2023 15:21 35 KB Carsten Strotmann to previous
3 25-Apr-2010 20:37 35 KB Carsten Strotmann to previous | to last
2 25-Apr-2010 20:28 36 KB Carsten Strotmann to previous | to last
1 25-Apr-2010 20:23 36 KB Carsten Strotmann to last

Page References

Incoming links Outgoing links

Version management

Difference between version and

At line 75 changed 9 lines
Depending on who you talk to " or believe " Forth is a lan-
guage, an environment, an operating system, and a philosophy
of programming. All these claims are true, in some sense,
but this semantic overload of the term Forth sometimes gives
"outsiders" the sensation of being smothered in cotton wool
when they try to understand what Forth is about. It doesn't
help matters that Forth programmers use a unique " and some-
what bizarre " terminology when discussing how the language
works and is structured.
Depending on who you talk to " or believe " Forth is a language, an environment, an operating system, and a philosophy of programming. All these claims are true, in some sense, but this semantic overload of the term Forth sometimes gives "outsiders" the sensation of being smothered in cotton wool when they try to understand what Forth is about. It doesn't help matters that Forth programmers use a unique " and somewhat bizarre " terminology when discussing how the language works and is structured.
At line 85 changed 9 lines
Every Forth language element that can be used interactively
or within a program is called a 'word'. (The term' word' is
also severely overloaded in Forth literature; in some con-
texts it can mean merely "a blank-delimited token from the
input stream," and there is also a parsing operator named
WORD). The source code for a particular word is called its
'definition'. Some words are 'primitives', coded in the
CPU's native machine language, the remainder are' high level
or secondary definitions', coded in Forth itself.
Every Forth language element that can be used interactively or within a program is called a 'word'. (The term' word' is also severely overloaded in Forth literature; in some contexts it can mean merely "a blank-delimited token from the input stream," and there is also a parsing operator named WORD). The source code for a particular word is called its 'definition'. Some words are 'primitives', coded in the
CPU's native machine language, the remainder are' high level or secondary definitions', coded in Forth itself.
At line 95 changed 8 lines
The Forth interpreter/compiler looks words up in the' dicti-
onary', which is essentially a symbol table that associates
each word's name with the address of the corresponding exe-
cutable code. The dictionary can be subdivided into' vocabu-
laries' that can be searched independently or in a tiered
fashion; this allows names to be overloaded and also allows
"dangerous" or infrequently used words to be "hidden" so
that they are not invoked accidentally.
The Forth interpreter/compiler looks words up in the' dictionary', which is essentially a symbol table that associates each word's name with the address of the corresponding executable code. The dictionary can be subdivided into' vocabularies' that can be searched independently or in a tiered fashion; this allows names to be overloaded and also allows "dangerous" or infrequently used words to be "hidden" so that they are not invoked accidentally.
At line 104 changed 13 lines
Why do we concern ourselves with this strange nomenclature
at all? For the purely pragmatic reason that to communicate
with a Forth programmer, you need to use his terminology,
because he probably won't understand yours. Terms like
'word', 'dictionary', 'vocabulary', and 'definition' have
the force of "tradition" behind them and pervade every Forth
book, article, and set of source code in existence. Further-
more, many Forth programmers are self-taught, learned Forth
only as a means to accomplish something else, and have never
used any other programming language. Backgrounds in electri-
cal or mechanical engineering, medicine, natural languages,
music, astronomy, and the like are common; formal educations
in computer science or mathematics are rare.
Why do we concern ourselves with this strange nomenclature at all? For the purely pragmatic reason that to communicate with a Forth programmer, you need to use his terminology, because he probably won't understand yours. Terms like 'word', 'dictionary', 'vocabulary', and 'definition' have the force of "tradition" behind them and pervade every Forth book, article, and set of source code in existence. Furthermore, many Forth programmers are self-taught, learned Forth only as a means to accomplish something else, and have never used any other programming language. Backgrounds in electrical or mechanical engineering, medicine, natural languages, music, astronomy, and the like are common; formal educations in computer science or mathematics are rare.
At line 118 changed one line
$ 3. 'The Forth Language'
!'The Forth Language'
At line 120 changed 19 lines
Viewed strictly as a language, Forth is clearly in the camp
of the modern, structured languages such as C, Pascal, and
Modula II. It has all the proper flow of control constructs
and, unlike C, does' no't have a GOTO. Like C, but unlike
Pascal and Modula II, Forth draws no distinction between
procedures and functions. A more important difference betwe-
en Forth and the other structured languages is that Forth
has no support for data typing either at compile time or at
runtime. The key difference between Forth and the other
structured languages is that the Forth compiler is 'extensi-
ble'. Forth allows the programmer to declare new data types
'and' allows him to define new compiler keywords (such as
control structures) and then use them immediately " even wi-
thin the very next procedure. Forth even has a compiler fa-
cility for creating new kinds of language' objects', if you
will: the programmer can separately define what happens at
compile time (when the object is instantiated) and what hap-
pens at runtime (when the object is invoked). The closest C
comes to being extensible is to allow you to build record
Viewed strictly as a language, Forth is clearly in the camp of the modern, structured languages such as C, Pascal, and Modula II. It has all the proper flow of control constructs and, unlike C, does not have a GOTO. Like C, but unlike Pascal and Modula II, Forth draws no distinction between procedures and functions. A more important difference between Forth and the other structured languages is that Forth
has no support for data typing either at compile time or at runtime. The key difference between Forth and the other structured languages is that the Forth compiler is 'extensible'. Forth allows the programmer to declare new data types 'and' allows him to define new compiler keywords (such as control structures) and then use them immediately " even within the very next procedure. Forth even has a compiler facility for creating new kinds of language' objects', if you will: the programmer can separately define what happens at compile time (when the object is instantiated) and what happens at runtime (when the object is invoked). The closest C comes to being extensible is to allow you to build record
At line 141 changed 19 lines
But Forth is not simply a set of keywords and syntax rules,
it is a description of a virtual machine. The most important
aspect of the virtual machine is that it has exactly two
stacks: a parameter stack and a return stack. Nearly all
Forth words use the parameter stack for both their arguments
and their results; the return stack is used for flow of con-
trol within a program and (occasionally) for temporary sto-
rage of working values. Forth lends itself naturally to re-
cursive algorithms because of this stack-based architecture;
in "classic" Forth programming, global variables are frowned
on and rarely used. The extant Forth standards specify the
names and actions of less than 200 words, which suffice to
define the Forth virtual machine and the behavior of the in-
terpreter/compiler. Commercial Forth development systems are
much more elaborate, and have at least 500 words resident
and available in the interpreter/compiler. Unfortunately, in
many Forth manuals the words are simply documented in ASCII
collating sequence, and are discussed as functional groups
only as an afterthought (if at all). This makes the prospect
But Forth is not simply a set of keywords and syntax rules, it is a description of a virtual machine. The most important aspect of the virtual machine is that it has exactly two stacks: a parameter stack and a return stack. Nearly all Forth words use the parameter stack for both their arguments and their results; the return stack is used for flow of control within a program and (occasionally) for temporary storage of working values. Forth lends itself naturally to recursive algorithms because of this stack-based architecture; in "classic" Forth programming, global variables are frowned on and rarely used. The extant Forth standards specify the names and actions of less than 200 words, which suffice to
define the Forth virtual machine and the behavior of the interpreter/compiler. Commercial Forth development systems are much more elaborate, and have at least 500 words resident and available in the interpreter/compiler. Unfortunately, in many Forth manuals the words are simply documented in ASCII collating sequence, and are discussed as functional groups only as an afterthought (if at all). This makes the prospect
At line 162 changed 27 lines
Actually, 500-600 language elements is almost exactly the
same as the combined number of operators, keywords, and run-
time library functions in Microsoft C or Borland C++, and
you can study Forth 'initially' with much the same approach
as you would use for C. As a first approximation, you can
view the relatively few "magic" words known to the Forth co-
mpiler as the "language," and treat the rest as a runtime
library divided into functional categories such as stack
operators, arithmetic/logical operators, memory access ope-
rators, and so on. As with any language, you will use 10% of
the library 90% of the time, so you can just learn the most
common functions first, and look up in the rest in the manu-
al when you need them. Once you've got a basic grasp of
what's available in the Forth language, it's better to think
about the system using a "layered" model, because this is
the way the system is actually built up. The lowest layer
consists of the primitives, which are written in the assem-
bly language of the host CPU and implement the Forth virtual
machine. The words coded as primitives tend to be the most
frequently used arithmetic/logical, comparison, stack, and
memory operators along with a few text-parsing building
blocks for the interpreter/compiler. Each additional layer
contains increasingly complex functions built out of the
words defined in the layers below: console I/O, mass stora-
ge, formatting and string management, the interpreter, the
compiler, and at the top, utilities such as the editor and
assembler.
Actually, 500-600 language elements is almost exactly the same as the combined number of operators, keywords, and runtime library functions in Microsoft C or Borland C++, and you can study Forth 'initially' with much the same approach as you would use for C. As a first approximation, you can view the relatively few "magic" words known to the Forth compiler as the "language," and treat the rest as a runtime library divided into functional categories such as stack operators, arithmetic/logical operators, memory access operators, and so on. As with any language, you will use 10% of the library 90% of the time, so you can just learn the most common functions first, and look up in the rest in the manual when you need them. Once you've got a basic grasp of what's available in the Forth language, it's better to think about the system using a "layered" model, because this is the way the system is actually built up. The lowest layer consists of the primitives, which are written in the assembly language of the host CPU and implement the Forth virtual machine. The words coded as primitives tend to be the most frequently used arithmetic/logical, comparison, stack, and memory operators along with a few text-parsing building blocks for the interpreter/compiler. Each additional layer contains increasingly complex functions built out of the words defined in the layers below: console I/O, mass storage, formatting and string management, the interpreter, the compiler, and at the top, utilities such as the editor and assembler.
At line 190 changed 17 lines
Two aspects of Forth that are frequently criticized are its
cryptic names and its postfix ('or reverse Polish') syntax.
To address the former: when the fundamental Forth names were
assigned, 10 character-per-second Teletypes were common and
so were minicomputer systems with 8 or 16 KB of RAM. Conse-
quently, short names were highly desirable to conserve memo-
ry and keystrokes, and we ended up with symbols such as @
for a memory fetch, ! for a memory store, and so on. I won't
make any attempt to defend the historical Forth namings;
each language has its conventions, and Forth's are no stran-
ger than some found in C, LISP, or APL. Readable, maintaina-
ble programs can be written in any language, as can write-
only, unmaintainable programs. The secret of obtaining the
former rather than the latter is good design, discipline,
and documentation, not use of a particular language. At le-
ast in Forth, since it is extensible, you can rename any
language element to anything you like!
Two aspects of Forth that are frequently criticized are its cryptic names and its postfix ('or reverse Polish') syntax. To address the former: when the fundamental Forth names were assigned, 10 character-per-second Teletypes were common and so were minicomputer systems with 8 or 16 KB of RAM. Consequently, short names were highly desirable to conserve memory and keystrokes, and we ended up with symbols such as @ for a memory fetch, ! for a memory store, and so on. I won't make any attempt to defend the historical Forth namings; each language has its conventions, and Forth's are no stranger than some found in C, LISP, or APL. Readable, maintainable programs can be written in any language, as can write-only, unmaintainable programs. The secret of obtaining the former rather than the latter is good design, discipline, and documentation, not use of a particular language. At least in Forth, since it is extensible, you can rename any language element to anything you like!
At line 208 changed 5 lines
The complaints about about Forth's postfix syntax are more
to the point, and deserve a more cogent response. In postfix
systems, the arguments precede the operator; for example, to
add 1 and 2 in Forth you would write:
The complaints about about Forth's postfix syntax are more to the point, and deserve a more cogent response. In postfix systems, the arguments precede the operator; for example, to add 1 and 2 in Forth you would write:
{{{
At line 214 changed one line
}}}
At line 216 changed one line
{{{
At line 218 changed one line
}}}
At line 220 changed one line
{{{
At line 109 added 2 lines
}}}
When recursive descent compilers for C and Pascal parse your pretty infix expressions, they transform the expressions into an intermediate postfix form before generating machine code. Forth is postfix to start with to shift the burden of expression evaluation onto the programmer, so that the compiler can be simpler, smaller, and faster. While postfix notation certainly takes some getting used to, the success of HP calculators indicates that postfix should not be considered a major barrier to learning Forth. It is interesting to note that (because the Forth compiler is extensible) an infix expression evaluator can easily be layered onto the compiler; several have been published, but Forth programmers have apparently not found them necessary or useful.
At line 223 changed 13 lines
When recursive descent compilers for C and Pascal parse your
pretty infix expressions, they transform the expressions in-
to an intermediate postfix form before generating machine
code. Forth is postfix to start with to shift the burden of
expression evaluation onto the programmer, so that the com-
piler can be simpler, smaller, and faster. While postfix no-
tation certainly takes some getting used to, the success of
HP calculators indicates that postfix should not be conside-
red a major barrier to learning Forth. It is interesting to
note that (because the Forth compiler is extensible) an in-
fix expression evaluator can easily be layered onto the com-
piler; several have been published, but Forth programmers
have apparently not found them necessary or useful.ul.
!'The Forth Environment'
At line 237 removed 2 lines
$ 4. 'The Forth Environment'