Forth programming language |
Forth is a Procedural programming, stack-oriented programming language, Reflection (computer science) programming language and programming environment. It was initially developed by Chuck Moore at the US National Radio Astronomy Observatory (NRAO) in the early 1970s, formalized as a programming language in 1977, and standardized by ANSI in 1994. It features both interactive execution of commands (making it suitable as a operating system shell for systems that lack a more formal operating system), as well as the ability to compile sequences of commands for later execution. Early Forth versions compiled threaded code, however many implementations today generate compiler optimization machine language like other language compilers.
Forth is so named because Moore considered it appropriate for fourth-generation computers (i.e. microcomputers), and the system on which he developed it was limited to five-letter filenames. Since the name is not an acronym, it is typically not spelled in all capital letters.
=Overview=
Forth offers a standalone programming environment consisting of a stack-oriented programming language, interactive, incremental interpreter (computer software) and Compiler. Programming is done by extending the language with words (the term used for Forth Subroutines), which become part of the language once defined.
Early versions of Forth were implemented with an inner interpreter tracing Threaded code Machine language, which yields compact and fast high-level code that can be compiled rapidly.
A character-oriented screen/block mechanism and standard Source code editor written in Forth provided a file mechanism for creating and storing Forth source code in the early systems, although today most Forths run under a host operating system and use conventional editors and text files.
A typical Forth package will consist of a pre-compiled kernel of the core words, which the programmer uses to define new words for the application. The application, once complete, can be saved as an image, with all new words already compiled. Generally, programmers will extend the initial core with words that are useful to the sorts of applications that they do and save this as their working foundation.
Forth has been popular for developing embedded systems and instrument controls because it is easy to add small machine code definitions to the language and use those in an interactive high-level programming environment.
The logical structure of Forth resembles a virtual machine. It has been implemented efficiently on modern RISC processors, and stack machine have been produced. The modular extensible nature of Forth permits many high-level applications such as CAD systems to be written in Forth.
Forth is used in the OpenFirmware booting used by Apple Computer, IBM, and Sun Microsystems. It is also used by the FreeBSD operating system as the first stage boot controller.
Forth is quick to implement and can be ported at low cost. A skilled programmer with good tools can port Forth to a new computer architecture in as little as two weeks. Porting a familiar computer architecture to a new computer is often much faster because only a few drivers are needed. Even programmers unfamiliar with Forth can use low-quality tools and write a Forth system from scratch in a few months of part-time effort.
One result has been a proliferation of nonstandard Forth systems by hobbyists, often of indifferent quality and with poor documentation. A newcomer to the language may download a free version from the net, have problems and dismiss the language without further consideration, mistaking a poor implementation for a fundamentally flawed language.
In contrast, professional implementations are rigorously tested (usually with proprietary test suites), conform to published standards, have manuals and telephone support, and often employ sophisticated optimization techniques to ensure excellent run-time performance. High-end professional Forth systems are usually delivered with all source code required to recompile themselves.
=Forth from a programmer s perspective=
Forth relies heavily on explicit use of the stack data structure and reverse Polish notation (or RPN, also used on advanced calculators from Hewlett-Packard). This notation is also called postfix notation because the operator is placed after its operands, as opposed to the more common infix notation where the operator is placed between its operands. The rationale for postfix notation is that it is closer to the machine language the computer will eventually use, and should therefore be faster to execute. For example, one could get the result of a mathematical expression this way:
25 10 * 50 + .
300
This command line first puts the numbers 25 and 10 on the implied stack; the * command multiplies the two numbers on the top of the stack and replaces them with their product; then the number 50 is placed on the stack, and the + command adds it to the previous product; finally, the . command prints the result to the user s terminal. Even the language s structural features are stack-based. For example:
: FLOOR5 ( n -- n ) DUP 6 < IF DROP 5 ELSE 1 - THEN ;
This code defines a new word (again, word is the term used for a subroutine) called FLOOR5 using the following commands: DUP simply duplicates the number on the stack;|
|
