home *** CD-ROM | disk | FTP | other *** search
/ Danny Amor's Online Library / Danny Amor's Online Library - Volume 1.iso / html / faqs / faq / forthfaq / whatisforth < prev   
Encoding:
Text File  |  1995-07-25  |  9.7 KB  |  233 lines

  1. Subject: Forth FAQ: What is Forth?  (l/m 30.Jan.93)
  2. Newsgroups: comp.lang.forth,comp.answers,news.answers
  3. From: ForthFAQ@willett.pgh.pa.us (FAQ account for comp.lang.forth)
  4. Date: 15 Dec 93 01:39:20 GMT
  5.  
  6. Archive-name: ForthFaq/WhatIsForth
  7. Last-modified: 30.Jan.93
  8. Version: 1.2
  9.  
  10.  
  11.  
  12. What is Forth?
  13.  
  14.  
  15.  ------------------------------------------------------------
  16.  
  17.  Philip J. Koopman Jr.
  18.  United Technologies Research Center, East Hartford, CT
  19.  
  20.  This description is copyright 1993 by ACM, and was developed
  21.  for the Second History of Programming Languages Conference
  22.  (HOPL-II), Boston MA.
  23.  
  24.  Permission to copy without fee all or part of this material
  25.  is granted, provided that the copies are not made or
  26.  distributed for direct commercial advantage, the ACM
  27.  copyright notice and the title of the publication and its
  28.  data appear, and notice is given that copying is by
  29.  permission of the Association for Computing Machinery.  To
  30.  copy otherwise, or to republish, requires a fee and/or
  31.  specific permission.
  32.  
  33.  ------------------------------------------------------------
  34.  
  35.  
  36.  
  37.  
  38.                  A BRIEF INTRODUCTION TO FORTH
  39.                  -----------------------------
  40.  
  41.       Forth is both an extensible language and an interactive
  42.  program development methodology.  Originally developed for
  43.  small embedded control mini- and micro-computers, Forth
  44.  seems to have been implemented on every major processor
  45.  manufactured.  It has been used in a wide variety of
  46.  applications, including spreadsheets, expert systems, and
  47.  multi-user databases.
  48.  
  49.  
  50.  TWO-STACK ABSTRACT MACHINE
  51.  
  52.       At the most superficial level, Forth is a directly
  53.  executable language for a stack-based abstract machine.  In
  54.  its essential form, the Forth abstract machine has a program
  55.  counter, memory, ALU, data evaluation pushdown stack, and
  56.  subroutine return address pushdown stack.
  57.  
  58.       Data evaluation in Forth is accomplished on the Data
  59.  Stack using Reverse Polish Notation (RPN), also called
  60.  postfix notation.  For example, the following sequence typed
  61.  from the keyboard:
  62.  
  63.       3 4 +  5 *  .  __35 ok__
  64.  
  65.  interactively pushes the value 3 on the stack, pushes the
  66.  value 4 on top of the 3, destructively adds 3 and 4 to get
  67.  7, then multiplies by 5.  The . operation displays the
  68.  single resultant top value on the stack, 35 (computer output
  69.  is underlined).  "ok" is the Forth command prompt.
  70.  Operations such as SWAP and DUP (duplicate) reorder and
  71.  replicate the top few Data Stack elements.
  72.  
  73.  
  74.  FACTORING
  75.  
  76.       At a deeper level, Forth programs use RPN not as an end
  77.  in itself, but rather as a means to achieve simple syntax
  78.  and flexible modularity.  Small, simple programs to perform
  79.  complex functions are written by reusing common code
  80.  sequences through a programming practice known as factoring.
  81.  
  82.       Subroutine calls and returns are an important part of
  83.  Forth programs and the factoring process.  As an example,
  84.  consider the following function (called a word in Forth)
  85.  which computes the sum of squares of two integers on top of
  86.  the Data Stack and returns the result on the Data Stack:
  87.  
  88.  : SUM-OF-SQUARES   ( a b -- c )   DUP *   SWAP   DUP *  +  ;
  89.  
  90.  The Data Stack inputs to the word at run-time are two
  91.  integers a and b.  The Data Stack output is a single integer
  92.  c.  The : denotes a function definition with the name SUM-
  93.  OF-SQUARES.  The ; terminates the definition.  Comments are
  94.  enclosed in parentheses.  This example follows the Forth
  95.  convention of including a stack-effect comment showing that
  96.  a (the second stack element) and b (the top stack element)
  97.  are consumed as stack inputs, with c produced as the stack
  98.  output.
  99.  
  100.       By the process of factoring, the example program would
  101.  be re-written in Forth using a new definition (a factor)
  102.  called SQUARED to allow sharing the common function of
  103.  duplicating and multiplying a number on the Data Stack.  The
  104.  separation of the Return Stack from the Data Stack in the
  105.  abstract machine allows the values on the Data Stack to be
  106.  cleanly passed down through multiple levels of subroutine
  107.  calls without run-time overhead.  In this new version, Data
  108.  Stack elements are implicitly passed as parameters from SUM-
  109.  OF-SQUARES to SQUARED:
  110.  
  111.  : SQUARED   ( n -- n**2 )     DUP *  ;
  112.  : SUM-OF-SQUARES   ( a b -- c )  SQUARED  SWAP SQUARED  +  ;
  113.  
  114.       Good Forth programmers strive to write programs
  115.  containing very short (often one-line), well-named word
  116.  definitions and reused factored code segments.  The ability
  117.  to pick just the right name for a word is a prized talent.
  118.  Factoring is so important that it is common for a Forth
  119.  program to have more subroutine calls than stack operations.
  120.  Factoring also simplifies speed optimization via replacing
  121.  commonly used factors with assembly language definitions.
  122.  In the preceding example, SQUARED could be re-written in
  123.  assembly language for speed while maintaining the same stack
  124.  effects.
  125.  
  126.       Writing a Forth program is equivalent to extending the
  127.  language to include all functions needed to implement an
  128.  application.  Therefore, programming in Forth may be thought
  129.  of as creating an application-specific language extension.
  130.  This paradigm, when coupled with a very quick
  131.  edit/compile/test cycle, seems to significantly increase
  132.  productivity.  As each Forth word is written, it can be
  133.  tested from the keyboard for immediate programmer feedback.
  134.  For example, the definitions above could be summarily tested
  135.  with:
  136.  
  137.  3 SQUARED .   __9 ok__
  138.  3 4 SUM-OF-SQUARES .   __25 ok__
  139.  
  140.  
  141.  INTERPRETATION, COMPILATION AND EXECUTION
  142.  
  143.       Forth systems use two levels of interpretation: a text
  144.  interpreter and an address interpreter.  When accepting
  145.  keyboard or file-based input, the text interpreter extracts
  146.  whitespace-separated character strings.  In interpretation
  147.  mode it attempts to execute the corresponding words (numeric
  148.  input is trapped and converted as a special case).  : is a
  149.  word like any other, but creates a new dictionary entry
  150.  containing the word name (symbol) and places the text
  151.  interpreter into compilation mode.  While in compilation
  152.  mode, most words extracted from the input stream are
  153.  compiled to a pointer to the word's definition in the
  154.  dictionary instead of being executed.
  155.  
  156.       A compiled Forth program is a collection of words, each
  157.  of which contains a statically allocated list of pointers to
  158.  other words.  Ultimately the pointers lead to assembly
  159.  language primitives, some of which are typically user-
  160.  written.  The Forth address interpreter is used to execute
  161.  compiled words, classically using threaded code techniques.
  162.  The Forth text interpreter, while not used in executing
  163.  compiled programs, is often included in applications as the
  164.  basis of a command-line user interface.
  165.  
  166.       Forth systems use one-pass compilation.  There is no
  167.  explicit Forth parser (and, for practical purposes, no
  168.  formal grammar).  Control flow words have a special
  169.  immediate attribute, and are executed immediately even when
  170.  the text interpreter is in compilation mode.  Immediate
  171.  words, when executed, typically cause compilation of special
  172.  structures.  For example, IF compiles a branch conditional
  173.  upon the top runtime Data Stack value, and the matching THEN
  174.  (the "endif" word) back-patches the branch target address.
  175.  Users can readily create their own immediate words, thus
  176.  extending the compiler by adding new control flow structures
  177.  or other language features.
  178.  
  179.       Data structures are created by another special class of
  180.  words: defining words.  Defining words have two parts: the
  181.  CREATE clause creates the dictionary entry for the data
  182.  structure instance, while the DOES> clause is a definition
  183.  shared by all data structures created by that defining word.
  184.  For example, an array defining word creates a named array
  185.  and reserves storage with its CREATE clause, and computes an
  186.  address (given indices) in its DOES> clause.  Defining words
  187.  are commonly used to hide data structure implementations and
  188.  to create families of similar words.
  189.  
  190.       Forth programmers traditionally value complete
  191.  understanding and control over the machine and their
  192.  programming environment.  Therefore, what Forth compilers
  193.  don't do reveals something about the language and its use.
  194.  Type checking, macro preprocessing, common subexpression
  195.  elimination, and other traditional compiler services are
  196.  feasible, but not included in production Forth compilers.
  197.  This simplicity allows Forth development systems to be small
  198.  enough to fit in the on-chip ROM of an 8-bit
  199.  microcontroller.  On the other hand, Forth's extensibility
  200.  allows "full-featured" systems to consume over 100K bytes
  201.  and provide comprehensive window-based programming
  202.  environments.  Forth also allows (and often encourages)
  203.  programmers to completely understand the entire compiler and
  204.  run-time system.  Forth supports extremely flexible and
  205.  productive application development while making ultimate
  206.  control of both the language and hardware easily attainable.
  207.  
  208.  
  209.  Philip J. Koopman Jr.
  210.  United Technologies Research Center, East Hartford, CT
  211.  
  212.  This description is copyright 1993 by ACM, and was developed
  213.  for the Second History of Programming Languages Conference
  214.  (HOPL-II), Boston MA.
  215.  
  216.  Permission to copy without fee all or part of this material
  217.  is granted, provided that the copies are not made or
  218.  distributed for direct commercial advantage, the ACM
  219.  copyright notice and the title of the publication and its
  220.  data appear, and notice is given that copying is by
  221.  permission of the Association for Computing Machinery.  To
  222.  copy otherwise, or to republish, requires a fee and/or
  223.  specific permission.
  224.  
  225. ---
  226. If you have any questions about ForthNet/comp.lang.forth or any information
  227. to add/delete or correct in this message or any suggestions on formatting or
  228. presentation, please contact Doug Philips at one of the following addresses:
  229.           Internet: dwp@willett.pgh.pa.us
  230.           Usenet:   ...!uunet!willett.pgh.pa.us!dwp
  231.           GEnie:    D.PHILIPS3
  232.  
  233.