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

  1. Chapter6                L_O_V_E FORTH
  2.  
  3.  
  4.  
  5. 6.0  Concept
  6.      -------
  7.  
  8.     In constructing Forth application programs, words are defined by
  9. using previously defined words as building blocks. This bottom-up process
  10. precludes the use of forward referencing or mutual recursion, but is
  11. normally sufficient to build quite complex software.  There are many
  12. occasions though, when the ability to reference a word before it has been
  13. defined or, for example, when the action of a low level word needs to be
  14. changed dynamically (ie. during program execution), that a portable and
  15. convenient method to do this becomes necessary.
  16.  
  17. 6.1  Definition of Terms
  18.      -------------------
  19.  
  20. Redefine:   A word is redefined when its action is changed after its
  21.             definition has been compiled. Redefinition can occur
  22.             dynamically  at run-time, but is generally used to
  23.             permanently change the the action of a word.
  24.  
  25. Revector:   Revectoring a word refers to the changing of the action
  26.             of a Forth word at run-time. This is the prefered method
  27.             for changing a word's action many times at run-time.
  28.  
  29. Vector:     A vector is the information necessary to execute a word
  30.             as if it had been compiled into a definition, or typed on
  31.             the command line. The vector information is system
  32.             specific, but should be hidden from the programmer so
  33.             that code which uses redefinitions, or allows run-time
  34.             revectoring, can be ported to other systems or computer
  35.             architectures. The vector is kept in a location called a
  36.             vector field, known to the dynamic word.
  37.  
  38.  
  39. 6.2 Reasons for Redefining and Revectoring
  40.     --------------------------------------
  41.  
  42. (1)  To change the action of a low-level word that is the foundation
  43.      for many other higher-level words when it is inconvenient to
  44.      recompile the entire system.
  45.  
  46. (2)  To allow mutual recursion or forward referencing. This is
  47.      accomplished by first defining a dummy word, and then later
  48.      redefining it to perform its correct action.
  49.  
  50. (3)  To dynamically redirect input or output within an application,
  51.      while allowing higher-level words to be device independant.
  52.  
  53. (4)  To develop and test an application interactively, and then change
  54.      the action of hardware specific words before committing the code
  55.      to   EPROM.
  56.  
  57. (5)  To chain abort handlers or other error handlers together, to
  58.      accomplish application specified actions on run-time execution
  59.      errors.
  60.  
  61.  
  62. 6.3  Word List
  63.      ---------
  64.  
  65.   REDEFINE  ( -- )
  66.      Used in the form:
  67.           Redefine <word1>  <word2>
  68.  
  69.      and can be read as "redefine <word1> do the same thing as
  70.      <word2>".
  71.  
  72. RESTORE  ( -- )
  73.      Used in the form:
  74.           RESTORE <word>
  75.  
  76.      where <word> has been previously  redefined, RESTORE will restore
  77.      the original function of <word>.
  78.  
  79. MAKE_DYNAMIC  ( -- )
  80.      Used in the form:
  81.           MAKE_DYNAMIC <word>
  82.  
  83.      to make <word> dynamically revectorable.
  84.      This allots space in the dictionary for a vector field, extracts the
  85.      vector from <word>, stores the vector into the vector field and
  86.      alters the code field address of <word> so that when <word>
  87.      executes it finds the vector in its vector field and executes it.
  88.  
  89.  
  90. REVECTOR  ( -- )
  91.      An immediate word used in the form:
  92.  
  93.           REVECTOR <word1> <word2>
  94.      where <word1> is a dynamically revectorable word. <word2> is any
  95.      other Forth word. Revector changes the vector field at <word1> by
  96.      storing the vector extracted from <word2>.
  97.  
  98.  
  99. VECTOR_FROM    ( -- )(compiling)
  100.                ( -- cfa, contents_of_cfa )(executing)
  101.           An immediate word used in the form:
  102.                VECTOR_FROM <word>
  103.  
  104.      From the next word in the input stream extract the cfa and the
  105.      contents of the cfa . If the state is compiling, then the vector
  106.      is compiled as a dliteral. This word operates on any Forth word.
  107.  
  108.  
  109. EXECUTE_VECTOR  ( cfa, contents_of_cfa -- )
  110.           Executes the word specified by the two-part vector on the
  111.           stack.
  112.  
  113.  
  114. GETVECTOR  ( -- )( compiling )
  115.            ( -- cfa, cfa_contents )( executing )
  116.  
  117.           An immediate word used in the form:
  118.                GETVECTOR <word>
  119.  
  120.      where <word> is a dynamically revectorable word. If the state
  121.      is compiling then   GETVECTOR   compiles the cfa of <word> as a
  122.      literal followed by   <GETVECTOR>.   This word is used to
  123.      retrieve the vector from the vector field of <word>, to be
  124.      stored in a variable, passed to   EXECUTE_VECTOR   or operated on
  125.      later with   SETVECTOR.
  126.  
  127. SETVECTOR ( cfa1, cfa1_contents -- )(executing)
  128.           ( -- )(compiling)
  129.           An immediate word used in the form:
  130.                SETVECTOR <word>
  131.  
  132.      where <word> is a dynamically revectorable word. When
  133.      compiling,   SETVECTOR   compiles the cfa of <word> as a literal
  134.      followed by   SETVECTOR>.   This word is used to store the
  135.      vector that is on the stack at run-time into the vector field
  136.      of <word>.
  137.  
  138.  
  139. 6.4  Examples
  140.      --------
  141.  
  142.      Forward Referencing:
  143.  
  144.           : second_word ;     ( make a dummy )
  145.  
  146.  
  147.           ( a word which executes another word that is define later )
  148.           : first_word  ( -- )
  149.                second_word ;
  150.  
  151.           ( the second word executes the previously defined word)
  152.           : <second_word> ( -- )
  153.                first_word  ;
  154.  
  155.  
  156.           ( now complete the circle by making second_word execute )
  157.           ( something useful  )
  158.           redefine second_word <second_word>
  159.  
  160.  
  161.  
  162.      Input and Output Redirection:
  163.  
  164.  
  165.      make_dynamic emit   ( make emit dynamically revectorable )
  166.  
  167.      ( output a character in textmode to the LCD Display )
  168.      : LCD_text_EMIT ( c -- )
  169.          dup bl <
  170.          if
  171.              case
  172.                  7   of beep     endof
  173.                  8   of do_backspace endof
  174.                  9   of do_tab   endof
  175.                  10  of do_linefeed  endof
  176.                  13  of do_return    endof
  177.              endcase
  178.          else
  179.              LCDwrite lcd!       ( write the char )
  180.              1 #out +!
  181.              1 cursor_addr +!
  182.              #out @ chars/line  =    ( is cursor past last column? )
  183.              if  do_return
  184.                  do_linefeed
  185.              then
  186.  
  187.          then
  188.        ;
  189.  
  190.  
  191. \     Later ...
  192.  
  193.      : app_word ( n1 n2 -- )
  194.           ...
  195.           revector emit  LCD_text_EMIT
  196.           ...  ;
  197.  
  198.  
  199.  
  200.      Chaining error handlers and abort handlers:
  201.  
  202.      ( a new abort that executes the old abort afterwards )
  203.      : <locals_abort>  ( -- )
  204.          LP!                  ( initialize the local stack )
  205.          <cleanup_locals>     ( restore all pointers and flags )
  206.          vector_from abort  execute_vector   ; ( execute previous abort)
  207.  
  208.      ( chain the special abort handler before abort )
  209.      redefine abort  <locals_abort>