umouse

umouse.git
git clone git://git.lenczewski.org/umouse.git
Log | Files | Refs | Submodules | README

DPANSA16.HTM (7969B)


      1 <HTML><HEAD>
      2 <TITLE>DPANS94</TITLE>
      3 <link disabled rel="stylesheet" href="mpexc6.css">
      4 <style>@import url(mpexc6.css);</style>
      5 </head>
      6 
      7 <BODY>
      8 <table width=100%>
      9 <tr>
     10 <td align=left>
     11 <a href=dpansa15.htm><img src=left.gif
     12  width=26 height=26 align=ALIGN border=0></a>
     13 <a href=dpansa17.htm><img src=right.gif
     14  width=26 height=26 align=ALIGN border=0></a>
     15 </td>
     16 <td align=right>
     17 <a href=dpans.htm#toc><img src=up.gif 
     18  width=26 height=26 align=ALIGN border=0></a>
     19 <a name=A.16>Table of Contents</a>
     20 </td>
     21 </tr>
     22 </table>
     23 <p>
     24 <hr size=4>
     25 
     26 
     27 <H2>A.16 The optional Search-Order word set</H2>
     28 
     29 Search-order specification and control mechanisms vary widely.  The
     30 FIG-Forth, Forth-79, polyFORTH, and Forth-83 vocabulary and search order
     31 mechanisms are all mutually incompatible.  The complete list of
     32 incompatible mechanisms, in use or proposed, is much longer.  The
     33 <a href=dpans16.htm#16.6.2.0715>ALSO</a>/<a href=dpans16.htm#16.6.2.1965>ONLY</a> 
     34 scheme described in a Forth-83 Experimental Proposal has
     35 substantial community support.  However, many consider it to be
     36 fundamentally flawed, and oppose it vigorously.
     37 
     38 <P>
     39 
     40 Recognizing this variation, this Standard specifies a new
     41 <B>primitive</B> set of tools from which various schemes may be
     42 constructed.  This primitive search-order word set is intended to be a
     43 portable <B>construction set</B> from which search-order words may be
     44 built, rather than a user interface.  ALSO/ONLY or the various
     45 <B>vocabulary</B> schemes supported by the major Forth vendors can be
     46 defined in terms of the primitive search-order word set.
     47 
     48 <P>
     49 
     50 The encoding for word list identifiers wid might be a small-integer
     51 index into an array of word-list definition records, the data-space
     52 address of such a record, a user-area offset, the execution token of a
     53 Forth-83 style sealed vocabulary, the link-field address of the first
     54 definition in a word list, or anything else.  It is entirely up to the
     55 system implementor.
     56 
     57 <P>
     58 
     59 In some systems the interpretation of numeric literals is controlled by
     60 including <B>pseudo word lists</B> that recognize numbers at the end of
     61 the search order.  This technique is accommodated by the <B>default
     62 search order</B> behavior of 
     63 <a href=dpans16.htm#16.6.1.2197>SET-ORDER</a> 
     64 when given an argument of -1.  In
     65 a system using the traditional implementation of ALSO/ONLY , the minimum
     66 search order would be equivalent to the word ONLY.
     67 
     68 <P>
     69 
     70 There has never been a portable way to restore a saved search order.
     71 F83 (not Forth-83) introduced the word 
     72 <a href=dpans16.htm#16.6.2.2037>PREVIOUS</a>, which almost made it
     73 possible to <B>unload</B> the search order by repeatedly executing the
     74 phrase <b><code>CONTEXT @ PREVIOUS</code></b>.  
     75 The search order could be <B>reloaded</B> by
     76 repeating 
     77 <b><code>ALSO CONTEXT !</code></b>.  
     78 Unfortunately there was no portable way to
     79 determine how many word lists were in the search order.
     80 
     81 <P>
     82 
     83 ANS Forth has removed the word CONTEXT because in many systems its
     84 contents refer to more than one word list, compounding portability
     85 problems.
     86 
     87 <P>
     88 
     89 Note that : 
     90 (<a href=dpans6.htm#6.1.0450>colon</a>) 
     91 no longer affects the search order.  The previous
     92 behavior, where the compilation word list replaces the first word list
     93 of the search order, can be emulated with the following redefinition of
     94 : (colon).
     95 
     96 <PRE>
     97 : :  GET-ORDER SWAP DROP  GET-CURRENT  SWAP SET-ORDER  : ;
     98 </PRE>
     99 <P>
    100 
    101 
    102 
    103 <hr>
    104 <A name=A.16.2>
    105 <H3>A.16.2 Additional terms</H3>
    106 </a>
    107 
    108 <DL>
    109 <DT><b>search order</b>
    110 <DD>Note that the use of the term <B>list</B> does not necessarily imply
    111 implementation as a linked list.</DL>
    112 
    113 <P>
    114 
    115 
    116 <hr>
    117 <A name=A.16.3.3>
    118 <H4>A.16.3.3 Finding definition names</H4>
    119 </a>
    120 
    121 In other words, the following is not guaranteed to work:
    122 
    123 <PRE>
    124 : FOO  ... [ ... SET-CURRENT ] ... RECURSE ... ;  IMMEDIATE
    125 </PRE>
    126 
    127 <a href=dpans6.htm#6.1.2120>RECURSE</a>, 
    128 ; (<a href=dpans6.htm#6.1.0460>semicolon</a>), and 
    129 <a href=dpans6.htm#6.1.1710>IMMEDIATE</a> 
    130 may or may not need information stored
    131 in the compilation word list.
    132 
    133 <P>
    134 
    135 
    136 <hr>
    137 <A name=A.16.6>
    138 <H3>A.16.6 Glossary</H3>
    139 </a>
    140 
    141 
    142 
    143 <hr>
    144 <A name=A.16.6.1.2192>A.16.6.1.2192 SEARCH-WORDLIST</A>
    145 <P>
    146 
    147 The string argument to SEARCH-WORDLIST is represented by c-addr u,
    148 rather than by just c-addr as with 
    149 <a href=dpans6.htm#6.1.1550>FIND</a>.  
    150 The committee wishes to
    151 establish c-addr u as the preferred representation of a string on the
    152 stack, and has adopted that representation for all new functions that
    153 accept string arguments.  While this decision may cause the
    154 implementation of SEARCH-WORDLIST to be somewhat more difficult in
    155 existing systems, the committee feels that the additional difficulty is
    156 minor.
    157 
    158 <P>
    159 
    160 When SEARCH-WORDLIST fails to find the word, it does not return the
    161 string, as does FIND.  This is in accordance with the general principle
    162 that Forth words consume their arguments.
    163 
    164 <P>
    165 
    166 <hr>
    167 <A name=A.16.6.2.0715>A.16.6.2.0715 ALSO</A>
    168 <P>
    169 
    170 Here is an implementation of 
    171 ALSO/<a href=dpans16.htm#16.6.2.1965>ONLY</a> 
    172 in terms of the primitive
    173 search-order word set.
    174 
    175 
    176 <PRE>
    177 WORDLIST CONSTANT ROOT   ROOT SET-CURRENT
    178 
    179 : DO-VOCABULARY  ( -- ) \ Implementation factor
    180     DOES>  @ >R           (  ) ( R: widnew )
    181      GET-ORDER  SWAP DROP ( wid1 ... widn-1 n )
    182      R> SWAP SET-ORDER
    183 ;
    184 
    185 : DISCARD  ( x1 .. xu u - ) \ Implementation factor
    186    0 ?DO DROP LOOP          \ DROP u+1 stack items
    187 ;
    188 
    189 CREATE FORTH  FORTH-WORDLIST , DO-VOCABULARY
    190 
    191 : VOCABULARY  ( name -- )  WORDLIST CREATE ,  DO-VOCABULARY ;
    192 
    193 : ALSO  ( -- )  GET-ORDER  OVER SWAP 1+ SET-ORDER ;
    194 
    195 : PREVIOUS  ( --  )  GET-ORDER  SWAP DROP 1- SET-ORDER ;
    196 
    197 : DEFINITIONS  ( -- )  GET-ORDER  OVER SET-CURRENT DISCARD ;
    198 
    199 : ONLY ( -- )  ROOT ROOT  2 SET-ORDER ;
    200 
    201 \ Forth-83 version; just removes ONLY
    202 : SEAL  ( -- )  GET-ORDER 1- SET-ORDER DROP ;
    203 
    204 \ F83 and F-PC version; leaves only CONTEXT
    205 : SEAL  ( -- )  GET-ORDER OVER 1 SET-ORDER DISCARD ;
    206 </PRE>
    207 
    208 <P>
    209 
    210 The preceding definition of ONLY in terms of a <B>ROOT</B> word list
    211 follows F83 usage, and assumes that the default search order just
    212 includes ROOT and 
    213 <a href=dpans16.htm#16.6.2.1590>FORTH</a>.  
    214 A more portable definition of FORTH and ONLY,
    215 without the assumptions, is:
    216 
    217 <PRE>
    218 &lt;omit the  ... WORDLIST CONSTANT ROOT ... line&gt;
    219 
    220 CREATE FORTH GET-ORDER OVER , DISCARD DO-VOCABULARY
    221 
    222 : ONLY  ( -- )  -1 SET-ORDER ;
    223 </PRE>
    224 
    225 <P>
    226 
    227 Here is a simple implementation of 
    228 <a href=dpans16.htm#16.6.1.1647>GET-ORDER</a> and 
    229 <a href=dpans16.htm#16.6.1.2197>SET-ORDER</a>, including a
    230 corresponding definition of 
    231 <a href=dpans6.htm#6.1.1550>FIND</a>.  The implementations of 
    232 <a href=dpans16.htm#16.6.1.2460>WORDLIST</a>,
    233 <a href=dpans16.htm#16.6.1.2192>SEARCH-WORDLIST</a>, 
    234 <a href=dpans16.htm#16.6.1.1643>GET-CURRENT</a> and 
    235 <a href=dpans16.htm#16.6.1.2195>SET-CURRENT</a> 
    236 depend on system details
    237 and are not given here.
    238 
    239 <PRE>
    240 16 CONSTANT #VOCS
    241 
    242 VARIABLE #ORDER
    243 
    244 CREATE CONTEXT  #VOCS CELLS ALLOT
    245 
    246 : GET-ORDER  ( -- wid1 .. widn n )
    247     #ORDER @ 0 ?DO
    248       #ORDER @  I - 1- CELLS CONTEXT + @
    249     LOOP
    250     #ORDER @
    251 ;
    252 
    253 : SET-ORDER  ( wid1 .. widn n -- )
    254     DUP -1 = IF
    255       DROP  &lt;push system default word lists and n&gt;
    256     THEN
    257     DUP #ORDER !
    258     0 ?DO  I CELLS CONTEXT + ! LOOP
    259 ;
    260 
    261 : FIND  ( c-addr -- c-addr 0 | w 1 | w -1 )
    262     0                     ( c-addr 0 )
    263     #ORDER @ 0 ?DO
    264       OVER COUNT          ( c-addr 0 c-addr' u )
    265       I CELLS CONTEXT + @ ( c-addr 0 c-addr' u wid)
    266       SEARCH-WORDLIST     ( c-addr 0; 0 | w 1 | w -1 )
    267       ?DUP IF             ( c-addr 0; w 1 | w -1 )
    268         2SWAP 2DROP LEAVE ( w 1 | w -1 )
    269       THEN                ( c-addr 0 )
    270     LOOP                  ( c-addr 0 | w 1 | w -1 )
    271 ;
    272 </PRE>
    273 
    274 <P>
    275 
    276 In an implementation where the dictionary search mechanism uses a hash
    277 table or lookup cache to reduce the search time, SET-ORDER might need to
    278 reconstruct the hash table or flush the cache.
    279 
    280 <P>
    281 
    282 <hr>
    283 <A href=dpans.htm#toc><IMG   src="up.gif" ></A>    Table of Contents 
    284 <BR>
    285 <A href=dpansa17.htm><IMG   src="right.gif" ></A>
    286 Next Section
    287 <P>
    288 </BODY>
    289 </HTML>