home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / love / chap10.doc < prev    next >
Text File  |  1993-04-11  |  7KB  |  167 lines

  1. Chapter 10              L.O.V.E. FORTH
  2.  
  3.  
  4.  
  5. 10.0 Stack Manipulation
  6.      ------------------
  7.  
  8.     Forth source code has been criticized by many as unreadable.  This
  9. is due to the traditional use of stack manipulation operators (such as SWAP
  10. DUP  ROT  PICK  ROLL etc... ) rather than the reverse polish syntax.
  11. Incorrect manipulation of the parameter stack in Forth is such a frequent
  12. source of logical bugs that experienced Forth programmers habitually insert
  13. 'stack diagrams' beside their phrases to document the net effect on the
  14. stack.  The problem of ordering data items on the stack is compounded by
  15. the use of double and quad length items to represent pointers of different
  16. sizes, large valued integers and floating point numbers.  In an attempt to
  17. remedy this, many versions of local variables and named parameters have
  18. been implemented by Forth programmers.  The implementation for L.O.V.E.
  19. Forth has a syntax modelled after that used in a Forth-83 system for the
  20. Apple Macintosh called MACH-2 by Palo Alto Shipping Company.
  21.  
  22.  
  23.  
  24. 10.1 Benefits
  25.      --------
  26.  
  27.     Local variables and named input parameters are an addition to the
  28. L.O.V.E. Forth compiler that improves the readability of source code and
  29. allows the programmer to spend less time thinking about the order and sizes
  30. of data items on Forth's parameter stack. An additional and significant
  31. benefit is increased execution speed and decreased object code size. Local
  32. variables and named input parameters insulate the implementation of a
  33. definition from changes to the order.  Data types of the input parameters
  34. are used at the programmer's discretion and are entirely optional.  This
  35. implementation of local variables allows the programmer to write completely
  36. re-entrant code and recursive definitions with ease.
  37.  
  38.  
  39.  
  40. 10.2 Usage
  41.      -----
  42.  
  43.     The syntax used replaces the traditional stack diagram after the
  44. name of the new definition.
  45.  
  46.     Local variables and named input parameters have a structure
  47. following this general form:
  48.  
  49.  : aname  { [type] n1 [type] n2 ... [type] n? | [type] L1  ...
  50.                                                 [type] L? -- comment }
  51.         ...  ;
  52.  
  53. where:
  54.  
  55. '{'     is an immediate word that precedes a local variable specification
  56.         list and parses this information up to the first '}' and must be used
  57.         immediately after the name of the new definition.
  58.  
  59.  
  60. [type]  is a built-in type specifier that tells the parser how to compile
  61.         object code for the local variable name following it in the local
  62.         variable specification.
  63.         Available types are:
  64.  
  65.            word:   -- a two byte (word length) integer
  66.            long:   -- a four byte (long word length) integer
  67.            quad:   -- an eight byte (quad word length) integer
  68.            float:  -- a four byte (single precision) floating point value
  69.                            in IEEE single precision format
  70.            double: -- an eight byte (double precision) floating point
  71.                           value in IEEE double precision format
  72.            ext:    -- a ten byte (extended precision) floating point
  73.                            value in IEEE extended precision format
  74.            fbcd:   -- a ten byte (binary coded decimal) floating point
  75.                            value in 80x87 BCD format
  76.  
  77.     ** NOTE : If a local variable name appears without a type specifier
  78.               the parser assumes that it is a 'word:' type. This allows
  79.               the programmer to write simple definitions, using mostly word
  80.               length items without always specifying the 'word:' type.
  81.  
  82.  
  83. n1 n2 n3 ...     are named input parameters from the stack where n1 is
  84.                  deepest in the stack and n? is on the top of the
  85.                  parameter stack.
  86.  
  87. | (bar)          is a separator character that terminates the input variable
  88.                  list and signals that a local variable list will begin.
  89.  
  90. L1 L2 L3 ...     is the list of local variable names used in this
  91.                  definition.
  92.  
  93. --               specifies that a comment begins, a comment string follows,
  94.                  usually used to document what the definition leaves on the
  95.                  stack. May not contain '}'  .
  96.  
  97.  
  98.  
  99. Notes:
  100.  
  101. (1)  Named parameters, temporary local variables and the comment are all
  102.      optional. A definition with no input parameters can have local
  103.      variables for temporary results.
  104.  
  105. (2)  In the V1.29 implementation a local variable stack frame can
  106.      contain up to 128 bytes of named input parameters and temporary
  107.      local variables (one local frame per definition).
  108.  
  109. (3)  Local names can be any space delimited string up to 31 characters
  110.      long but must not be named '|' (bar) or '--' (comment) or '}' (left
  111.      brace) or given a name that is identical to a type specifier .
  112.  
  113.  
  114.  
  115. 10.3 Examples
  116.      --------
  117.  
  118.  
  119.           : <a_word>   { arg1 arg2 arg3  | temp  -- result }
  120.                ...............
  121.                ...............   ;
  122.  
  123.  
  124.  
  125.     The named arguments 'arg1', 'arg2' and 'arg3' are initialized
  126. values on the parameter stack with 'arg3' on the top and 'arg1' deepest on
  127. the stack.  The local variable 'temp' is an uninitialized temporary value.
  128.  
  129.         : <b_word>   { word: arg1   long: arg2   | ext: temp  -- result }
  130.              ...............
  131.              ...............   ;
  132.  
  133.  
  134.  
  135.     The named arguments 'arg1', 'arg2' are initialized values on the
  136. parameter stack with long word length 'arg2' on the top and word length
  137. 'arg1' deepest on the stack. The local variable 'temp' is an uninitialized
  138. extended precision floating point temporary value.
  139.  
  140.  
  141. 10.4 Operations with Local Variables
  142.      -------------------------------
  143.  
  144.  
  145.     There are 4 basic operations with local variables and the
  146. possibility of adding more operations in the future.  These operations are:
  147.  
  148.    local_name    -- the default action of a local variable is to fetch
  149.                           the contents of the local variable to the top
  150.                           of the stack.
  151.  
  152.    -> local_name -- stores the top of stack into the location specified by
  153.                       local_name in the format specified by the type of
  154.                       local_name.
  155.  
  156.    +> local_name -- adds the top of stack to the contents of the location
  157.                       specified by local_name in the format specified by the
  158.                       type of local_name.
  159.  
  160.    & local_name  -- returns the segment and offset of the contents of
  161.                           local_name.
  162.  
  163.  
  164.  
  165.     All operations with local variables are immediate words that
  166. determine at compile time what type of data will be operated upon. Then,
  167. compile the correct object code.