home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!paladin.american.edu!howland.reston.ans.net!wupost!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!news.sei.cmu.edu!drycas.club.cc.cmu.edu!pitt.edu!pitt!willett!FAQ
- Newsgroups: comp.lang.forth
- Subject: Forth FAQ Answers: Part 01 (What is Forth?). (V1.0:01.Jan.93)
- Message-ID: <4215.UUL1.3#5129@willett.pgh.pa.us>
- From: FAQ@willett.pgh.pa.us (comp.lang.forth FAQ source)
- Date: 2 Jan 93 00:39:22 GMT
- Organization: EIEI-U
- Lines: 200
-
-
-
-
- What is Forth?
-
- -------------------
-
- (C) Copyright 1992 by Philip J. Koopman Jr.
- Use and distribution is permitted in accordance with ACM
- copyright policy.
-
- I have an informal ruling from the HOPL conference organizers
- that inclusion of this as FAQ material in comp.lang.forth is
- entirely appropriate.
-
- -------------------
-
- Original message header:
- From: koopman+@cs.cmu.edu (Philip Koopman)
- Subject: Description of Forth (long)
- Message-ID: <Buq5CL.D4n.2@cs.cmu.edu>
- Summary: HOPL-II Forth language description -- Draft 9/17/92
- Date: Thu, 17 Sep 1992 13:26:42 GMT
-
-
- The following is a draft description of the Forth
- programming language. It will eventually appear as
- a preprint for the History of Programming Languages
- Conference (HOPL-II) to be held in Boston in April 1993.
- That conference will feature an excellent paper on the
- history of Forth by Rather, Colburn & Moore.
- Constructive criticism appreciated.
-
-
-
- Forth Programming Language Description
- For HOPL-II
- DRAFT 9/17/92
-
- Philip J. Koopman Jr.
- United Technologies Research Center
- koopman@utrc.utc.com
-
-
- Forth is both an extensible language and an interactive program
- development methodology. While originally developed for small
- embedded control mini- and micro-computers, Forth seems to have
- been implemented on every major processor manufactured. It has
- been used in a wide variety of applications, such as spreadsheets,
- expert systems, and multi-user databases.
-
- At the most superficial level, Forth is an almost-directly executable
- high-level language for a stack-based abstract machine. In its
- essential form, the Forth abstract machine has a program counter,
- memory, ALU, data evaluation pushdown stack, and subroutine return
- address pushdown stack.
-
- Data evaluation in Forth is accomplished on the Data Stack using
- Reverse Polish Notation (RPN), otherwise known as postfix notation.
- For example, the following sequence typed from the keyboard:
-
- 3 4 + 5 * .
-
- interactively pushes the value 3 on the stack, pushes the value 4
- on top of the 3, destructively adds 3 and 4 to get 7, then multiplies
- by 5. The '.' operation displays the single resultant top value on
- the stack, outputting 35. Operations such as 'SWAP' and 'DUP' (for
- DUPlicate) are provided to reorder and replicate the top few Data
- Stack elements.
-
- At a deeper level, Forth programs use RPN not as an end in itself, but
- rather as a means to achieve simple syntax and flexible modularity.
- Small, simple programs to perform complex functions are written by
- reusing common code sequences through a programming practice known as
- "factoring."
-
- Subroutine calls and returns are an important part of Forth programs
- and the factoring process. As an example, consider the following
- function (called a "word" in Forth) which computes the sum of
- squares of two integers on top of the Data Stack and
- returns the result on the Data Stack.
-
- : SUM-OF-SQUARES ( a b -- c )
- DUP * SWAP DUP * + ;
-
- The Data Stack inputs to the word at run-time are two integers a and b.
- The Data Stack output is a single integer c. The ':' denotes a
- function definition with the name 'SUM-OF-SQUARES'. The definition
- terminates with the ';' . Comments are enclosed in parentheses.
- This example follows the Forth convention of including a stack effect
- comment showing that a and b are stack inputs, with c the stack output.
-
- By the process of factoring, the example program would be re-written
- in Forth using a new definition (a "factor") called 'SQUARED' to allow
- sharing the common function of duplicating and multiplying a number on
- the Data Stack. The separation of the Return Stack from the Data
- Stack in the abstract machine allows the values on the Data Stack to
- be cleanly passed down through multiple levels of subroutine calls
- without run-time overhead. In this new version, Data Stack elements
- are implicitly passed as parameters from 'SUM-OF-SQUARES' to 'SQUARED':
-
- : SQUARED ( n -- n**2 )
- DUP * ;
- : SUM-OF-SQUARES ( n m -- sum )
- SQUARED SWAP SQUARED + ;
-
- Good Forth programmers strive to achieve programs containing very short
- (often one-line), well-named word definitions and reused factored code
- segments. The ability to pick just the right name for a word is a
- prized talent. Factoring is so important that it is common for a
- Forth program to have more subroutine calls than stack operations.
- Factoring also allows speed optimization by replacing commonly used
- factors with assembly language definitions. In the preceding example,
- SQUARED could be re-written in assembly language for speed while
- maintaining the same stack effects.
-
- Writing a Forth program is equivalent to extending the language to
- include all functions needed to implement an application. Therefore,
- programming in Forth may be thought of as creating an application-
- specific language extension. This paradigm, when coupled with a very
- quick edit/compile/test cycle, seems to significantly increase
- productivity. As each Forth word is written, it can be tested from
- the keyboard for immediate programmer feedback. For example, the
- definitions above could be summarily tested with:
-
- 5 SQUARED .
- 3 4 SUM-OF-SQUARES .
-
- which would both print out 25.
-
- Forth systems use two levels of interpretation: a text interpreter
- and an address interpreter. When accepting keyboard or file-based
- input, the text interpreter extracts whitespace-separated character
- strings and attempts to execute them (numeric input is trapped and
- interpreted as a special case). ':' is a word like any other, but
- creates a new "dictionary" entry containing the word name (symbol)
- and sets a flag that places the text interpreter into compilation
- mode. While in compilation mode, each word is compiled to a pointer
- to the corresponding code in the dictionary instead of being executed.
-
- A compiled Forth program is a collection of words, each of which
- contains a statically allocated list of pointers to other words.
- Ultimately the pointers lead to assembly language primitives, some
- of which are typically user-written. The Forth address interpreter
- is used to execute compiled words, classically using threaded code
- techniques. The Forth text interpreter, while not used in executing
- compiled programs, is often included in applications as the basis
- of a command-line user interface.
-
- There is no explicit Forth parser (and, for practical purposes,
- no formal grammar). Control flow words (such as 'IF') have a
- special "immediate" attribute, and are executed immediately even
- when the text interpreter is in compilation mode. Immediate words,
- when executed, typically cause compilation of special structures
- (such as conditional branches based on the top runtime Data Stack
- value) or back-patching of branch targets. Users can readily
- create their own immediate words, thus extending the compiler by
- adding new control flow structures or other language features.
-
- Another class of words, "defining words" is used to create data
- structures. Defining words have two parts to their definitions:
- one part active during compilation (the 'CREATE' part), and an
- alternate part active during execution (the 'DOES>' part). For
- example, an array defining word reserves storage in its 'CREATE'
- part, but computes an address given indices in its 'DOES>' part.
- Defining words are commonly used to hide data structure implementations
- and to allow the creation of families of words with similar functions.
-
- Forth programmers traditionally value complete understanding and
- control over the machine and their programming environment. Therefore,
- what Forth compilers don't do reveals something about the language
- and its use. Type checking, macro preprocessing, common subexpression
- elimination, and other traditional compiler services are feasible,
- but not included in production Forth compilers. This simplicity
- allows Forth development systems to be small enough to fit in the
- on-chip ROM of an 8-bit microcontroller. On the other hand, Forth's
- extensibility allows "full-featured" systems to consume over 100K
- bytes and provide comprehensive window-based programming environments.
- Forth also allows (and often encourages) programmers to completely
- understand the entire compiler and run-time system. Forth supports
- extremely flexible and productive application development while
- making ultimate control of both the language and hardware easily
- attainable.
-
- -------------------
-
- (C) Copyright 1992 by Philip J. Koopman Jr.
- Use and distribution is permitted in accordance with ACM
- copyright policy.
-
- I have an informal ruling from the HOPL conference organizers
- that inclusion of this as FAQ material in comp.lang.forth is
- entirely appropriate.
-
- -------------------
-
- Phil Koopman koopman@utrcgw.utc.com (203) 727-1624
- United Technologies Research Center (UTRC)
- 411 Silver Lane M/S 48
- East Hartford, CT 06108 USA
-