Forth/Structuring vs. words that actually do something

From Wikibooks, open books for an open world
< Forth
Jump to: navigation, search

We have described a process of creating definitions of words by listing sequences of other words previously in the dictionary which are to be executed in turn in the order given. One can imagine calling a "high-level" word which in turn calls other "high-level" words lower in the dictionary, which call other words lower still. Note that all this structuring is for the benefit of the programmer, maintenance, and many other good reasons to be there.

There obviously needs to be something else than a means to organize sequences of previously defined words, and so on ad infinitim. Eventually we must come to a different type of word that actually does something: adds two numbers, moves a value, or other operation normally thought of in connection with "machine instructions". These are called "machine code words". At the bottom of the dictionary, classic Forth systems include a set of words of this type that implement a more or less standard "Forth Virtual Machine". Often each machine code word is a short sequence of assembly language instructions that implement a particular standard function, for example a parameter stack manipulation named SWAP. Alternate implementations implement machine code words directly in hardware, as in the gates of an FPGA (Field Programmable Logic Array ~= Desktop hardware development environment).

The process of defining machine code words and "high level" words is formally analagous to "axioms" and "theorems" of such formally built up mathematical systems as Euclidean geometry or Bertrand Russell and Albert North Whitehead's Principia Mathematica [[1]]. Analagous questions of which axioms are essential, and whether it is necessary to add an axiom to allow a previously undefined operation to take place at all are considered in the design of the Forth dictionary. In this sense, adding a new machine code word that enables, for example, TCP/IP communications that could not be made to occur using a structure based on the previous set of machine code words is seen as adding a "TCP/IP communications axiom" to the dictionary.

Execution of a Forth program typically takes place when the user or another process requests the "execution" of a particular word. If this is a "high level" word, a mixture of tracing through the sequence of calls and sometimes reaching a machine code word and doing instructions on the "Forth Virtual Machine". Clearly, this technique can be used to great benefit when specialized hardware presents a unique set of machine code words necessary to send signals to exotic specialized processors or I/O devices. Adding the standard machine code and higher level words then provides an environment to develop additional "high level" words on top to bring out the functionality of the unique instrument.