File:  [gforth] / gforth / Attic / search-order.fs
Revision 1.17: download - view: text, annotated - select for diffs
Mon May 6 12:39:03 1996 UTC (27 years, 11 months ago) by anton
Branches: MAIN
CVS tags: HEAD
added tables (case-sensitive wordlists)
cleaned up hash.fs a bit
added "interpretation:" for adding special interpretation semantics

    1: \ search order wordset                                 14may93py
    2: 
    3: \ Copyright (C) 1995 Free Software Foundation, Inc.
    4: 
    5: \ This file is part of Gforth.
    6: 
    7: \ Gforth is free software; you can redistribute it and/or
    8: \ modify it under the terms of the GNU General Public License
    9: \ as published by the Free Software Foundation; either version 2
   10: \ of the License, or (at your option) any later version.
   11: 
   12: \ This program is distributed in the hope that it will be useful,
   13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15: \ GNU General Public License for more details.
   16: 
   17: \ You should have received a copy of the GNU General Public License
   18: \ along with this program; if not, write to the Free Software
   19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   20: 
   21: $10 constant maxvp
   22: Variable vp
   23:   0 A, 0 A,  0 A, 0 A,   0 A, 0 A,   0 A, 0 A, 
   24:   0 A, 0 A,  0 A, 0 A,   0 A, 0 A,   0 A, 0 A, 
   25: 
   26: : get-current  ( -- wid )  current @ ;
   27: : set-current  ( wid -- )  current ! ;
   28: 
   29: : context ( -- addr )  vp dup @ cells + ;
   30: : definitions  ( -- )  context @ current ! ;
   31: 
   32: \ wordlist Vocabulary also previous                    14may93py
   33: 
   34: AVariable voclink
   35: 
   36: Defer 'initvoc
   37: ' drop IS 'initvoc
   38: 
   39: Variable slowvoc   slowvoc off
   40: 
   41: : wordlist  ( -- wid )
   42:   here  0 A, Forth-wordlist wordlist-map @ A, voclink @ A, slowvoc @ A,
   43:   dup wordlist-link dup voclink ! 'initvoc ;
   44: 
   45: : Vocabulary ( -- ) Create wordlist drop  DOES> context ! ;
   46: 
   47: : also  ( -- )
   48:   context @ vp @ 1+ dup maxvp > abort" Vocstack full"
   49:   vp ! context ! ;
   50: 
   51: : previous ( -- )  vp @ 1- dup 0= abort" Vocstack empty" vp ! ;
   52: 
   53: \ vocabulary find                                      14may93py
   54: 
   55: : (vocfind)  ( addr count wid -- nfa|false )
   56:     \ !! generalize this to be independent of vp
   57:     drop vp dup @ 1- cells over +
   58:     DO  2dup I 2@ over <>
   59:         IF  (search-wordlist) dup
   60: 	    IF  nip nip  UNLOOP EXIT
   61: 	    THEN  drop
   62:         ELSE  drop 2drop  THEN
   63:     [ -1 cells ] Literal +LOOP
   64:     2drop false ;
   65: 
   66: 0 value locals-wordlist
   67: 
   68: : (localsvocfind)  ( addr count wid -- nfa|false )
   69:     \ !! use generalized (vocfind)
   70:     drop locals-wordlist
   71:     IF 2dup locals-wordlist (search-wordlist) dup
   72: 	IF nip nip
   73: 	    EXIT
   74: 	THEN drop
   75:     THEN
   76:     0 (vocfind) ;
   77: 
   78: \ In the kernal the dictionary search works on only one wordlist.
   79: \ The following stuff builds a thing that looks to the kernal like one
   80: \ wordlist, but when searched it searches the whole search order
   81: \  (including locals)
   82: 
   83: \ this is the wordlist-map of the dictionary
   84: Create vocsearch       ' (localsvocfind) A, ' (reveal) A,  ' drop A,
   85: 
   86: \ Only root                                            14may93py
   87: 
   88: wordlist \ the wordlist structure
   89: vocsearch over wordlist-map A! \ patch the map into it
   90: 
   91: Vocabulary Forth
   92: Vocabulary Root
   93: 
   94: : Only  vp off  also Root also definitions ;
   95: 
   96: \ set initial search order                             14may93py
   97: 
   98: Forth-wordlist @ ' Forth >body A!
   99: 
  100: vp off  also Root also definitions
  101: Only Forth also definitions
  102: 
  103: lookup A! \ our dictionary search order becomes the law
  104: 
  105: ' Forth >body constant forth-wordlist \ "forth definitions get-current" and "forth-wordlist" should produce the same wid
  106: 
  107: 
  108: \ get-order set-order                                  14may93py
  109: 
  110: : get-order  ( -- wid1 .. widn n )
  111:   vp @ 0 ?DO  vp cell+ I cells + @  LOOP  vp @ ;
  112: 
  113: : set-order  ( wid1 .. widn n / -1 -- )
  114:   dup -1 = IF  drop Only exit  THEN  dup vp !
  115:   ?dup IF  1- FOR  vp cell+ I cells + !  NEXT  THEN ;
  116: 
  117: : seal ( -- )  context @ 1 set-order ;
  118: 
  119: \ words visible in roots                               14may93py
  120: 
  121: : .name ( name -- ) \ gforth	dot-name
  122:     name>string type space ;
  123: 
  124: require termsize.fs
  125: 
  126: : words ( -- ) \ tools
  127:     cr 0 context @
  128:     BEGIN
  129: 	@ dup
  130:     WHILE
  131: 	2dup name>string nip 2 + dup >r +
  132: 	cols >=
  133: 	IF
  134: 	    cr nip 0 swap
  135: 	THEN
  136: 	dup .name space r> rot + swap
  137:     REPEAT
  138:     2drop ;
  139: 
  140: ' words alias vlist ( -- ) \ gforth
  141: \g Old (pre-Forth-83) name for @code{WORDS}.
  142: 
  143: : body> ( data -- cfa )  0 >body - ;
  144: 
  145: : .voc
  146:     body> >name .name ;
  147: : order ( -- )  \  search-ext
  148:     \g prints the search order and the @code{current} wordlist.  The
  149:     \g standard requires that the wordlists are printed in the order
  150:     \g in which they are searched. Therefore, the output is reversed
  151:     \g with respect to the conventional way of displaying stacks. The
  152:     \g @code{current} wordlist is displayed last.
  153:     get-order 0
  154:     ?DO
  155: 	.voc
  156:     LOOP
  157:     4 spaces get-current .voc ;
  158: : vocs ( -- ) \ gforth
  159:     \g prints vocabularies and wordlists defined in the system.
  160:     voclink
  161:     BEGIN
  162: 	@ dup @
  163:     WHILE
  164: 	dup 0 wordlist-link - .voc
  165:     REPEAT
  166:     drop ;
  167: 
  168: Root definitions
  169: 
  170: ' words Alias words
  171: ' Forth Alias Forth
  172: ' forth-wordlist alias forth-wordlist
  173: ' set-order alias set-order
  174: ' order alias order
  175: 
  176: Forth definitions
  177: 
  178: include hash.fs
  179: 
  180: \ table (case-sensitive wordlist)
  181: 
  182: : table-find ( addr len wordlist -- nfa / false )
  183:     >r 2dup r> bucket @ (tablefind) ;
  184: 
  185: Create tablesearch-map ( -- wordlist-map )
  186:     ' table-find A, ' hash-reveal A, ' (rehash) A,
  187: 
  188: : table ( -- wid )
  189:     \g create a case-sensitive wordlist
  190:     wordlist
  191:     tablesearch-map over wordlist-map ! ;
  192: 
  193: \ marker                                               18dec94py
  194: 
  195: \ Marker creates a mark that is removed (including everything 
  196: \ defined afterwards) when executing the mark.
  197: 
  198: : marker, ( -- mark )  here dup A,
  199:   voclink @ A, voclink
  200:   BEGIN  @ dup @  WHILE  dup 0 wordlist-link - @ A,  REPEAT  drop
  201:   udp @ , ;
  202: 
  203: : marker! ( mark -- )  dup @ swap cell+
  204:   dup @ voclink ! cell+
  205:   voclink
  206:   BEGIN  @ dup @  WHILE  over @ over 0 wordlist-link - !
  207: 	 swap cell+ swap
  208:   REPEAT  drop  voclink
  209:   BEGIN  @ dup @  WHILE  dup 0 wordlist-link - rehash  REPEAT  drop
  210:   @ udp !  dp ! ;
  211: 
  212: : marker ( "mark" -- )
  213:   marker, Create A,  DOES>  @ marker! ;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>