home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / internet / cld9z89.zip / Z0000089.TXT < prev    next >
Text File  |  1993-06-09  |  10KB  |  227 lines

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