home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / forth / 3729 < prev    next >
Encoding:
Internet Message Format  |  1993-01-01  |  9.4 KB

  1. 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
  2. Newsgroups: comp.lang.forth
  3. Subject: Forth FAQ Answers: Part 01 (What is Forth?).  (V1.0:01.Jan.93)
  4. Message-ID: <4215.UUL1.3#5129@willett.pgh.pa.us>
  5. From: FAQ@willett.pgh.pa.us (comp.lang.forth FAQ source)
  6. Date: 2 Jan 93 00:39:22 GMT
  7. Organization: EIEI-U
  8. Lines: 200
  9.  
  10.  
  11.  
  12.  
  13. What is Forth?
  14.  
  15.  -------------------
  16.  
  17.  (C) Copyright 1992 by Philip J. Koopman Jr.
  18.  Use and distribution is permitted in accordance with ACM
  19.  copyright policy.
  20.  
  21.  I have an informal ruling from the HOPL conference organizers
  22.  that inclusion of this as FAQ material in comp.lang.forth is 
  23.  entirely appropriate.
  24.  
  25.  -------------------
  26.  
  27.  Original message header:
  28.     From: koopman+@cs.cmu.edu (Philip Koopman)
  29.     Subject: Description of Forth (long)
  30.     Message-ID: <Buq5CL.D4n.2@cs.cmu.edu>
  31.     Summary: HOPL-II Forth language description -- Draft 9/17/92
  32.     Date: Thu, 17 Sep 1992 13:26:42 GMT
  33.  
  34.  
  35.  The following is a draft description of the Forth
  36.  programming language.  It will eventually appear as 
  37.  a preprint for the History of Programming Languages 
  38.  Conference (HOPL-II) to be held in Boston in April 1993.
  39.  That conference will feature an excellent paper on the 
  40.  history of Forth by Rather, Colburn & Moore.
  41.  Constructive criticism appreciated.
  42.  
  43.  
  44.  
  45.      Forth Programming Language Description
  46.      For HOPL-II
  47.      DRAFT  9/17/92
  48.  
  49.      Philip J. Koopman Jr.
  50.      United Technologies Research Center
  51.      koopman@utrc.utc.com
  52.     
  53.  
  54.  Forth is both an extensible language and an interactive program 
  55.  development methodology.  While originally developed for small 
  56.  embedded control mini- and micro-computers, Forth seems to have 
  57.  been implemented on every major processor manufactured.  It has 
  58.  been used in a wide variety of applications, such as spreadsheets, 
  59.  expert systems, and multi-user databases.
  60.  
  61.  At the most superficial level, Forth is an almost-directly executable 
  62.  high-level language for a stack-based abstract machine.  In its 
  63.  essential form, the Forth abstract machine has a program counter, 
  64.  memory, ALU, data evaluation pushdown stack, and subroutine return 
  65.  address pushdown stack.
  66.  
  67.  Data evaluation in Forth is accomplished on the Data Stack using 
  68.  Reverse Polish Notation (RPN), otherwise known as postfix notation.
  69.  For example, the following sequence typed from the keyboard:
  70.  
  71.  3 4 +  5 *  .
  72.  
  73.  interactively pushes the value 3 on the stack, pushes the value 4 
  74.  on top of the 3, destructively adds 3 and 4 to get 7, then multiplies 
  75.  by 5.  The '.' operation displays the single resultant top value on 
  76.  the stack, outputting 35.  Operations such as 'SWAP' and 'DUP' (for 
  77.  DUPlicate) are provided to reorder and replicate the top few Data 
  78.  Stack elements.
  79.  
  80.  At a deeper level, Forth programs use RPN not as an end in itself, but
  81.  rather as a means to achieve simple syntax and flexible modularity.  
  82.  Small, simple programs to perform complex functions are written by 
  83.  reusing common code sequences through a programming practice known as 
  84.  "factoring."
  85.  
  86.  Subroutine calls and returns are an important part of Forth programs 
  87.  and the factoring process.  As an example, consider the following 
  88.  function (called a "word" in Forth) which computes the sum of  
  89.  squares of two integers on top of the Data Stack and 
  90.  returns the result on the Data Stack.
  91.  
  92.  : SUM-OF-SQUARES   ( a b -- c )
  93.     DUP *   SWAP   DUP *     +  ;
  94.  
  95.  The Data Stack inputs to the word at run-time are two integers a and b.  
  96.  The Data Stack output is a single integer c.  The ':' denotes a 
  97.  function definition with the name 'SUM-OF-SQUARES'.  The definition 
  98.  terminates with the ';' .  Comments are enclosed in parentheses.  
  99.  This example follows the Forth convention of including a stack effect
  100.  comment showing that a and b are stack inputs, with c the stack output.
  101.  
  102.  By the process of factoring, the example program would be re-written 
  103.  in Forth using a new definition (a "factor") called 'SQUARED' to allow 
  104.  sharing the common function of duplicating and multiplying a number on 
  105.  the Data Stack.  The separation of the Return Stack from the Data 
  106.  Stack in the abstract machine allows the values on the Data Stack to 
  107.  be cleanly passed down through multiple levels of subroutine calls 
  108.  without run-time overhead.  In this new version, Data Stack elements 
  109.  are implicitly passed as parameters from 'SUM-OF-SQUARES' to 'SQUARED':
  110.  
  111.  : SQUARED   ( n -- n**2 )
  112.     DUP *  ;
  113.  : SUM-OF-SQUARES   ( n m -- sum )
  114.     SQUARED  SWAP   SQUARED  +  ;
  115.  
  116.  Good Forth programmers strive to achieve programs containing very short 
  117.  (often one-line), well-named word definitions and reused factored code 
  118.  segments.  The ability to pick just the right name for a word is a 
  119.  prized talent.  Factoring is so important that it is common for a 
  120.  Forth program to have more subroutine calls than stack operations.  
  121.  Factoring also allows speed optimization by replacing commonly used 
  122.  factors with assembly language definitions.  In the preceding example,
  123.  SQUARED could be re-written in assembly language for speed while 
  124.  maintaining the same stack effects.
  125.  
  126.  Writing a Forth program is equivalent to extending the language to 
  127.  include all functions needed to implement an application.  Therefore, 
  128.  programming in Forth may be thought of as creating an application-
  129.  specific language extension.  This paradigm, when coupled with a very
  130.  quick edit/compile/test cycle, seems to significantly increase 
  131.  productivity.  As each Forth word is written, it can be tested from 
  132.  the keyboard for immediate programmer feedback.  For example, the 
  133.  definitions above could be summarily tested with:
  134.  
  135.  5 SQUARED .
  136.  3 4 SUM-OF-SQUARES .
  137.  
  138.  which would both print out 25.
  139.  
  140.  Forth systems use two levels of interpretation: a text interpreter 
  141.  and an address interpreter.  When accepting keyboard or file-based 
  142.  input, the text interpreter extracts whitespace-separated character 
  143.  strings and attempts to execute them (numeric input is trapped and 
  144.  interpreted as a special case).  ':' is a word like any other, but
  145.  creates a new "dictionary" entry containing the word name (symbol) 
  146.  and sets a flag that places the text interpreter into compilation 
  147.  mode.  While in compilation mode, each word is compiled to a pointer 
  148.  to the corresponding code in the dictionary instead of being executed.
  149.  
  150.  A compiled Forth program is a collection of words, each of which 
  151.  contains a statically allocated list of pointers to other words.  
  152.  Ultimately the pointers lead to assembly language primitives, some 
  153.  of which are typically user-written.  The Forth address interpreter 
  154.  is used to execute compiled words, classically using threaded code 
  155.  techniques.  The Forth text interpreter, while not used in executing 
  156.  compiled programs, is often included in applications as the basis
  157.  of a command-line user interface.
  158.  
  159.  There is no explicit Forth parser (and, for practical purposes, 
  160.  no formal grammar).  Control flow words (such as 'IF') have a 
  161.  special "immediate" attribute, and are executed immediately even 
  162.  when the text interpreter is in compilation mode.  Immediate words, 
  163.  when executed, typically cause compilation of special structures 
  164.  (such as conditional branches based on the top runtime Data Stack 
  165.  value) or back-patching of branch targets.  Users can readily
  166.  create their own immediate words, thus extending the compiler by 
  167.  adding new control flow structures or other language features.
  168.  
  169.  Another class of words, "defining words" is used to create data
  170.  structures.  Defining words have two parts to their definitions: 
  171.  one part active during compilation (the 'CREATE' part), and an 
  172.  alternate part active during execution (the 'DOES>' part).  For 
  173.  example, an array defining word reserves storage in its 'CREATE'
  174.  part, but computes an address given indices in its 'DOES>' part.  
  175.  Defining words are commonly used to hide data structure implementations 
  176.  and to allow the creation of families of words with similar functions.
  177.  
  178.  Forth programmers traditionally value complete understanding and 
  179.  control over the machine and their programming environment.  Therefore, 
  180.  what Forth compilers don't do reveals something about the language 
  181.  and its use.  Type checking, macro preprocessing, common subexpression 
  182.  elimination, and other traditional compiler services are feasible, 
  183.  but not included in production Forth compilers.  This simplicity 
  184.  allows Forth development systems to be small enough to fit in the 
  185.  on-chip ROM of an 8-bit microcontroller.  On the other hand, Forth's 
  186.  extensibility allows "full-featured" systems to consume over 100K 
  187.  bytes and provide comprehensive window-based programming environments.  
  188.  Forth also allows (and often encourages) programmers to completely 
  189.  understand the entire compiler and run-time system.  Forth supports 
  190.  extremely flexible and productive application development while 
  191.  making ultimate control of both the language and hardware easily
  192.  attainable.
  193.  
  194.  -------------------
  195.  
  196.  (C) Copyright 1992 by Philip J. Koopman Jr.
  197.  Use and distribution is permitted in accordance with ACM
  198.  copyright policy.
  199.  
  200.  I have an informal ruling from the HOPL conference organizers
  201.  that inclusion of this as FAQ material in comp.lang.forth is 
  202.  entirely appropriate.
  203.  
  204.  -------------------
  205.  
  206.  Phil Koopman          koopman@utrcgw.utc.com   (203) 727-1624
  207.  United Technologies Research Center (UTRC)
  208.  411 Silver Lane      M/S 48
  209.  East Hartford, CT  06108   USA
  210.