forth language
play

FORTH LANGUAGE Charles Moore created Forth in the 1960s and 1970s to - PDF document

ARINDAM ADHIKARY, PID: 904567450 , ROLL NO: MIT/01/02 aadhikar@vt.edu, aadhikary.mit1@spjimr.ernet.in MOHMMAD BIN QASIM FAISAL, PID: 904585976 ROLL NO: MIT/01/33 mfaisal@vt.edu, faisal.mit1@spjimr.ernet.in FORTH LANGUAGE Charles Moore created


  1. ARINDAM ADHIKARY, PID: 904567450 , ROLL NO: MIT/01/02 aadhikar@vt.edu, aadhikary.mit1@spjimr.ernet.in MOHMMAD BIN QASIM FAISAL, PID: 904585976 ROLL NO: MIT/01/33 mfaisal@vt.edu, faisal.mit1@spjimr.ernet.in FORTH LANGUAGE Charles Moore created Forth in the 1960s and 1970s to give computers real-time control over astronomical equipment. . The object in developing this new "programming tool" was to overcome the need for an engineer to learn a large number of methods for controlling compilers (linkers, assemblers and directives plus high-level languages). A number of Forth's features (such as its interactive style) make it a useful language for AI programming, and devoted adherents have developed Forth-based expert systems and neural networks. The first program to be called Forth was written in about 1970. The first complete implementation was used in 1971 on a DEC PDP-11 for the National Radio Astronomy Observatory's 11-meter radio telescope in Arizona. This system was responsible for pointing and tracking the telescope, collecting data and recording it on magnetic tape, supporting an interactive graphics terminal on which an astronomer could analyze previously recorded data. The system was so useful that astronomers from all over the world began asking for copies. The success of this application enabled Moore and Elizabeth ("Bess") Rather in 1973 to form "FORTH, Inc.", to explore commercial uses of the language. A version was developed, in 1977, for the newly introduced 8 Bit microprocessors called "micro FORTH". Their “MiniFORTH” product for minicomputers complemented this. Dictionary: The dictionary contains all the executable routines (or words) that make up a Forth system. System routines are entries predefined in the dictionary that become available when the system is booted. The basic form of the most common type of word definition is: <name> <words to be executed> ; The dictionary is a linked list of variable-length entries, each of which is a Forth word and its definition. In most implementations, the dictionary grows toward high memory; the discussion in this section will assume it does. Each dictionary entry points to the entry that logically precedes it The address of the next available cell at the end of the dictionary is put on the stack by the word HERE .

  2. The ANS Forth term for one of these chains is word list. A word list is a subset of the dictionary containing words for some special purpose. There usually are several word lists present in a system and these are normally available to all users on a re-entrant basis. The essential structure of dictionary entries is the same for all words, and is diagrammed in the Figure . The link cell contains the location of the preceding entry. This speeds up searches, which start at the recent end of the dictionary and work backwards to the older end. Some systems are case sensitive and others are not; see your product documentation for details. To avoid problems and to maximize the transportability of code, the names of the words provided in a standard system are defined in all upper-case letters and should always be referred to in all upper-case letters when using them in subsequent definitions.

  3. Although the order of the fields in a dictionary entry is arranged in each implementation to optimize each machine's dictionary search, Figure 3 shows a general model. In addition, usually there are several control bits to control the type and use of the definition. Since the longest name field in most systems has 31 characters, requiring only five bits to express a count, the control bits are often found in the byte containing the count. The most important control bit is called the precedence bit. A word whose precedence bit is set executes at compile time. The precedence bit is set by the word IMMEDIATE. The precedence bit is used for a few special words, such as compiler directives, but it is zero for most words. The cells (if any) after the code field address are called the parameter field, which is of variable length. CONSTANTs and VARIABLEs keep their data in the first cell of the parameter field. Other definitions may keep several values. Glossary HERE ( -- addr ) Push the address of the next available dictionary location onto the stack. ALLOT ( n -- ) Increment the dictionary address pointer by n number of bytes. References CODE definitions, Section 4.1 Code field addresses, Section 2.7.4 Creating dictionary entries, Section 2.7.1 Word lists, Section 3.6 Control Structure: Data Stack: Every Forth system contains at least one data stack. In a multitasked system, each task may have its own data stack. The stack is a cell-wide, push-down LIFO (last-in, first-out) list; its purpose is to contain numeric operands for Forth commands. Commands commonly expect their input parameters on this stack and leave their output results there. The stack's size is indefinite. 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 5 < 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; "<" compares the two numbers on the stack and replaces them with a true-or-false value; "IF" takes a true-or-false value and chooses to execute commands immediately after it or to skip to the " ELSE "; " DROP " discards the value on the stack; and " THEN "

  4. ends the conditional. The text in parentheses is a comment, advising that this word expects a number on the stack and will return a possibly changed number. The net result is a function that performs similarly to this function (written in the C programming language): int floor5(int v) { if (v < 5) return 5; else return v - 1; A terser Forth definition of FLOOR5 that gives the same result: : FLOOR5 ( n -- n' ) 1 - 5 MAX ; The standard Forth dictionary provides words for simple manipulation of single- and double-length operands on the stack: SWAP , DUP , DROP , 2SWAP , etc. Structure of the Compiler: The compiler itself consists of Forth words. This gives the programmer considerable control of the compiler, and a programmer can change the compiler's words for special purposes.The " compile time " flag in the name field is set for words with "compile time" behavior. Most simple words execute the same code whether they are typed on a command line, or embedded in code. When compiling these, the compiler simply places code or a threaded pointer to the word. Compile-time words are actually executed by the compiler. The classic examples of compile time words are the control-structures such as IF and WHILE . All of Forth's control structures, and almost all of its compiler are implemented as compile-time words. The assembler (see above) is a special dialect of the compiler. Structure of Code: In most Forth systems, the body of a code definition consists of either machine language, or some form of threaded code. Traditionally, indirect-threaded code was used, but direct-threaded and subroutine threaded Forths have also been popular. The fastest modern Forths use subroutine threading, insert simple words as macros, and perform peephole optimization or other optimizing strategies to make the code smaller and faster. Data Objects:

Recommend


More recommend