Forth (programming language)

from Wikipedia, the free encyclopedia
Forth
Paradigms : stack-oriented
Publishing year: circa 1970
Designer: Charles H. Moore
Developer: Charles H. Moore
Typing : typeless
Dialects: FORTH-79, FORTH-83, ANS FORTH, Forth-2012
Influenced by: Burroughs Large Systems, Lisp , APL , Assembler
Affected: STOIC , Factor , PostScript , RPL , REBOL

Forth is an imperative , stack-based programming language . A Forth system contains an operating system to run and a development environment to create Forth programs. This is why an actually implemented Forth on a computer is called a Forth system . As in other programming languages, many versions today generate highly optimized machine code .

History of origin

Charles H. Moore developed Forth in the years before 1969 from a self-written mini interpreter . This interpreter already contained many features of the later programming language, but was dependent on an underlying operating system. Moore used it as a macro language for programs he wrote in Fortran , ALGOL , PL / I and assembler . Computer technology at that time was changing from large computers that worked in batch mode to mini-computers that also allowed interactive work. In 1968, in addition to his actual work for an IBM 1130 minicomputer, Moore finally rewrote a chess program that he had previously coded in Algol into his programming language. This made the chess program much easier to port and Moore called his language FORTH for the first time . Originally the language was supposed to be called FOURTH because it was actually intended for the fourth generation computers, which were expected to be sold as microcomputers . The operating system on which Moore programmed, however, only allowed file names with a length of five letters, which is why he decided on the homophone FORTH .

The peculiarity of a comprehensive solution consisting of programming language and operating system can be easily explained from the history of its development. To control the 11-meter radio telescope of the National Radio Astronomy Observatory (NRAO) on Kitt Peak in Arizona, Moore had two Honeywell computers without suitable software , a 16 kB  DDP-116 and a 32 kB  H316 . He had decided to program all the necessary components himself, which are necessary for comfortable programming and safe operation of the computers. This includes an operating system , a high-level language and a development environment . All of these components were implemented within a single program - the Forth system. Both computers were responsible for the control of the telescope and its scientific instruments, they monitored the positioning and tracking, the collection and recording of the data on magnetic tape , and supported an interactive graphics terminal (Tektronix 4002A) on which the astronomers analyzed and saved the recorded data Hard copy. The multitasking character of the system allowed these functions to run at the same time without disruptions or runtime conflicts. Overall, the system worked so well that astronomers from around the world got interested in it and wanted a copy. Its use quickly spread, and in 1976 Forth was adopted as the standard language by the International Astronomical Union .

Popular books on Forth from the ZX81 and Spectrum era

The approach of a so-called “all-in-one solution” has remained a special approach by Forth. Even programming languages ​​like Smalltalk or Self (usually) do not go so far as to replace the operating system. While this is possible, most of the time people want portability . The Oberon programming language , which was developed as part of the Ceres system, is also available independently of it. Only the programming language / operating system package EUMEL and ELAN had proceeded similarly to Forth . Usually the above components are designed and developed separately from each other.

Forth system

A complete Forth system can be implemented with just a few kilobytes of memory. This property was extremely important at the time of its creation, as the computers at that time had relatively little RAM available. In addition, the actual core of the Forth system is very small and the rest of the system itself is already defined in Forth. This makes porting the Forth system to other processors much easier. This positive characteristic made Forth relatively widespread in the late 1970s.

Today, this makes Forth a high-level language that is particularly suitable for programming a wide variety of microcontrollers . However, combined systems are used for development in which the intermediate code is precompiled on a host PC and only the result is stored in the client's microcontroller . This means that some interactive functions of the Forth system can be omitted and the source code can be commented on more comprehensively .

The essential elements of the internal Forth architecture are the two stacks ( data stack and return stack ), a set of registers and the dictionary , which are simulated independently of the hardware , and a jump table that links the token with the function to be called. The data word stored on the stack is called a cell , and it can be 16, 32 or 64 bits in size. In addition, the dictionary can be divided into several vocabularies as required. The Forth system itself is implemented as a virtual machine . All instructions and math expressions are written in Reverse Polish Notation ( UPN ). This results simply from the fact that the Forth system first checks for each input value whether it is in the dictionary. If this is the case, the corresponding function is called, which can then access all the entries made so far. If the value does not exist in the dictionary, it is interpreted as a numerical value or a character string . In both cases, the following entries have not yet been read in at this time and therefore have no effect on the program sequence. One can therefore, as usual at the time of creation, easily transfer the input data stream through a sequential medium, such as e.g. B. realize a punch card stack or a magnetic tape.

Forth has two main characteristics that set it apart from many other programming systems of its time:

  • Forth was public domain from the start
  • Forth is self-compiling and allows the user direct access to the compiler , whereby the command structure can be expanded as required

Programming in Forth

Programming in Forth is fundamentally different from other languages. In Forth there is no difference between integrated and programmed functions (so-called words ), but there is a functional core of words (primitives) that are implemented directly in machine code. There are also no immediate routines, instead the language is expanded to include additional words . In Forth, the start of a program corresponds to calling a word , which thus represents the main routine of the program. Since the compiler is also expanded by defining words that run in the development environment and thus directly manipulate the intermediate code, this leads to a flexibility that can only be achieved with a few other high-level languages.

Example of a UPN expression

The following sequence is entered in Forth to calculate the expression (5 + 3) * (7 + 2) :

5 3 + 7 2 + * .

The expression is processed while it is being read. The input string is divided into words by spaces . The word "5" is usually not in the dictionary , it is interpreted as a number and the corresponding value is placed on the value stack. The same applies to the word “3”. The word “+” that follows is now in the dictionary . The corresponding function is therefore called. This function takes the top two values ​​from the stack, adds them and puts the result back on the stack. The small "+" function is usually implemented in the respective machine language . The number 8 is now on the stack. The next word is "7", the number 7 is placed on the stack. This is followed by the word “2”, here the number 2 lands on the stack. Next, the word "+" again, which calls the addition function again. At the top of the stack is the number 9, below the number 8. Then the word “*” is read, which is linked to the multiplication function in the dictionary. This function takes the top two numbers from the stack and places your product on top. The multiplication function can, depending on the Forth system, be implemented as machine code or, in turn, already implemented in Forth. The next word read is now ".". This word takes the top value (now the number 72) from the stack and displays it on the display device.

Words for stack operations

command Stack description
DUP n1 n2 n1 n2 n2 duplicates the top stack element
SWAP n1 n2 n3 n1 n3 n2 swaps the top two stack elements
RED n1 n2 n3 n4 n1 n3 n4 n2 brings the third stack element up
OVER n1 n2 n3 n1 n2 n3 n2 copies the second stack element
PICK n1 n2 n3 2 n1 n2 n3 n1 copies the specified (here: 2 corresponding third) stack element
DROP n1 n2 n3 n4 n1 n2 n3 removes the topmost stack element

Forth is suitable for the interactive development of control systems that enable various processes, such as:

  1. Open valve
  2. Turn on the alarm
  3. Lock doors
  4. etc.

The earliest known applications were controls for radio telescopes and observatories for astronomy and space travel , other applications in the technical-scientific area came later. When Forth was deployed on the Galileo space probe in 1989, it was one of the first high-level languages in space; until then, assembler languages ​​were predominantly used for such systems.

Forth implementations and derived languages

The porting from Forth to the Apple II was carried out in the 1970s by the programmer John T. Draper , with which he developed the EasyWriter word processor . GraFORTH was a graphics-capable extension from Forth for the Apple II. ASYST was an extension of Forth for measuring , controlling and rules for PCs .

Factor is a programming language based on Forth, which, however, is more oriented towards application development than the machine-level functionality of Forth. A more recent application for Forth is the concept of Open Firmware (IEEE-1275).

A further development of Forth is Interpress from Xerox , from which the page description language Adobe PostScript developed. The PDF , which was later also developed by Adobe, is based heavily on PostScript (and could therefore be seen as the great-grandchild of Forth), but in contrast to PostScript and Forth, it is not a complete programming language.

See also

literature

  • Leo Brodie: Starting FORTH , 1981 ( updated online edition )
  • Leo Brodie: Thinking FORTH , Punchy Publishing, 2004, ISBN 978-0-976-45870-8 .
  • Ronald Zech: The programming language FORTH: a compact representation of the programming language with many examples , 2nd edition, Franzis, 1985, ISBN 3-7723-7262-7
  • Thom Hogan: FORTH - Quite simply , trans. v. Karl-Heinz Büchner, Vieweg & Sohn, Braunschweig / Wiesbaden, 1985, ISBN 978-3-528-04292-9 .
  • C. Kevin McCabe: Programming with FORTH , trans. v. Peter Monadjemi, Springer Vieweg, 1988, ISBN 978-3-322-90106-4 .
  • Edward K. Conklin, Elizabeth D. Rather: Forth Programmer's Handbook , Forth Inc., 1997, ISBN 0-9662156-0-5 .
  • Elizabeth D. Rather: Forth Application Techniques , 5th Edition, Forth Inc., Los Angeles, 2008, ISBN 1-4196-8576-7 .
  • Mitch Derick, Linda Baker: FORTH Encyclopedia - The Complete Forth Programmer's Manual , 2nd ed., Mountain View Press, Mountain View CA, USA, 1982, ISBN 978-9-993-16563-7 .
  • WP Salman, O. Tisserand, B. Toulout: FORTH , Editions EYROLLES, Paris 1983, engl. Translation by MJ Stewart, Macmillan Publishers Ltd. 1984, ISBN 978-0-333-36798-8 .

Web links

Individual evidence

  1. http://www.forth200x.org
  2. ^ Charles Moore: Forth - The Early Years. 1991, accessed May 5, 2016 .
  3. ^ History of the origins of Forth
  4. https://ub.fnwi.uva.nl/computermuseum//tek4002a.html
  5. Computers in Spaceflight: The NASA Experience - Chapter Six: Distributed Computing on Board Voyager and Galileo . history.nasa.gov, accessed August 23, 2019.
  6. ^ Forth in Space Applications . FORTH, Inc., accessed August 23, 2019.
  7. GraFORTH II Language Reference (PDF; 4.5 MB)
  8. Campbell et al., "Up and Running with Asyst 2.0," MacMillan Software Co., 1987
  9. Manfred Wenzel: Forth am Commodore 128. In: Commodore world. Volume 3, No. 11 (1986), p. 56 f.