The unusual Forth programming language was designed by Charles H. Moore in 1970. An astronomer, Moore was interested in developing a compact language for controlling motors to drive radio telescopes and other equipment.
Language Structure
Forth has a very simple structure. The Forth system con-sists of a collection of words. Each word is a sequence of operations (which can include other existing words). For example, the DUP word makes a copy of a data value. Data is held by a stack. For example, the arithmetic expression written as 2 + 3 in most languages would be written in Forth as + 2 3. When the + operator (which in Forth is a pre-defined word) executes, it adds the next two numbers it encounters (2 and 3) together, and puts the sum on the stack (where in turn it might be fetched for further process-ing by the next word in the program (see stack). This rep-resentation is also called postfix notation and is familiar to many users of scientific calculators.
The words in the dictionary are “threaded” or linked so that each word contains the starting address of the next one. The Forth interpreter runs a simple loop where it fetches the next token (one or more characters delimited by spaces) and scans the dictionary to see if it matches a defined word (including variables). If a word is found, the code in the word is executed. If no word is found, the interpreter inter-prets the token as a numeric constant, loads it on the stack, and proceeds to the next word.
A key feature of Forth is its extensibility. Once you have defined a word, the new word can be used in exactly the same way as the predefined words. The various forms of defining words allow for great control over what happens when a new word is created and when the word is later executed. (In many ways Forth anticipated the principles of object-oriented programming, with words as objects with implicit construc-tors and methods. A well-organized Forth program builds up from “primitive” operations to the higher-level words, with the program itself being the highest-level word.)
Forth has always attracted an enthusiastic following of programmers who appreciate a close communion with the flow of data in the machine and the ability to precisely tailor programs. The language is completely interactive, since any word can be typed at the keyboard to execute it and display the results. Forth was also attractive in the early days of microcomputing because the lack of need for a sophisticated interpreter or compiler meant that Forth systems could run comfortably on systems that had perhaps 16K or 64K of available RAM.
Forth never caught on with the mainstream of program-mers, however. Its very uniqueness and the unusual mindset it required probably limited the number of people willing to learn it. While Forth programs can be clearly organized, badly written Forth programs can be virtually impossible to read. However, Forth is sometimes found “under the hood” in surprising places (for example, the PostScript page description language is similar to Forth) and the language still has a considerable following in designing hardware control devices (see embedded systems).
No comments:
Post a Comment