File:  [gforth] / gforth / Attic / search-order.fs
Revision 1.19: download - view: text, annotated - select for diffs
Mon May 13 16:37:02 1996 UTC (27 years, 11 months ago) by anton
Branches: MAIN
CVS tags: HEAD
eliminated state-smartness in ;code and sfnumber
immediate-flag is now $40 and restrict-flag $20
HEADER now stores the compilation wordlist in the header and
	REVEAL reveals into that wordlist
assorted cleanups

    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 ' 'initvoc >body !
   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 ( -- wordlist-map )
   85: ' (localsvocfind) A, ' (reveal) A,  ' drop A,
   86: 
   87: \ Only root                                            14may93py
   88: 
   89: wordlist \ the wordlist structure
   90: vocsearch over wordlist-map A! \ patch the map into it
   91: 
   92: Vocabulary Forth
   93: Vocabulary Root
   94: 
   95: : Only  vp off  also Root also definitions ;
   96: 
   97: \ set initial search order                             14may93py
   98: 
   99: Forth-wordlist @ ' Forth >body A!
  100: 
  101: vp off  also Root also definitions
  102: Only Forth also definitions
  103: 
  104: lookup A! \ our dictionary search order becomes the law
  105: 
  106: ' Forth >body constant forth-wordlist \ "forth definitions get-current" and "forth-wordlist" should produce the same wid
  107: 
  108: 
  109: \ get-order set-order                                  14may93py
  110: 
  111: : get-order  ( -- wid1 .. widn n )
  112:   vp @ 0 ?DO  vp cell+ I cells + @  LOOP  vp @ ;
  113: 
  114: : set-order  ( wid1 .. widn n / -1 -- )
  115:   dup -1 = IF  drop Only exit  THEN  dup vp !
  116:   ?dup IF  1- FOR  vp cell+ I cells + !  NEXT  THEN ;
  117: 
  118: : seal ( -- )  context @ 1 set-order ;
  119: 
  120: \ words visible in roots                               14may93py
  121: 
  122: : .name ( name -- ) \ gforth	dot-name
  123:     name>string type space ;
  124: 
  125: require termsize.fs
  126: 
  127: : words ( -- ) \ tools
  128:     cr 0 context @
  129:     BEGIN
  130: 	@ dup
  131:     WHILE
  132: 	2dup name>string nip 2 + dup >r +
  133: 	cols >=
  134: 	IF
  135: 	    cr nip 0 swap
  136: 	THEN
  137: 	dup .name space r> rot + swap
  138:     REPEAT
  139:     2drop ;
  140: 
  141: ' words alias vlist ( -- ) \ gforth
  142: \g Old (pre-Forth-83) name for @code{WORDS}.
  143: 
  144: : body> ( data -- cfa )  0 >body - ;
  145: 
  146: : .voc
  147:     body> >name .name ;
  148: : order ( -- )  \  search-ext
  149:     \g prints the search order and the @code{current} wordlist.  The
  150:     \g standard requires that the wordlists are printed in the order
  151:     \g in which they are searched. Therefore, the output is reversed
  152:     \g with respect to the conventional way of displaying stacks. The
  153:     \g @code{current} wordlist is displayed last.
  154:     get-order 0
  155:     ?DO
  156: 	.voc
  157:     LOOP
  158:     4 spaces get-current .voc ;
  159: : vocs ( -- ) \ gforth
  160:     \g prints vocabularies and wordlists defined in the system.
  161:     voclink
  162:     BEGIN
  163: 	@ dup @
  164:     WHILE
  165: 	dup 0 wordlist-link - .voc
  166:     REPEAT
  167:     drop ;
  168: 
  169: Root definitions
  170: 
  171: ' words Alias words
  172: ' Forth Alias Forth
  173: ' forth-wordlist alias forth-wordlist
  174: ' set-order alias set-order
  175: ' order alias order
  176: 
  177: Forth definitions
  178: 
  179: include hash.fs
  180: 
  181: \ table (case-sensitive wordlist)
  182: 
  183: : table-find ( addr len wordlist -- nfa / false )
  184:     >r 2dup r> bucket @ (tablefind) ;
  185: 
  186: Create tablesearch-map ( -- wordlist-map )
  187:     ' table-find A, ' hash-reveal A, ' (rehash) A,
  188: 
  189: : table ( -- wid )
  190:     \g create a case-sensitive wordlist
  191:     wordlist
  192:     tablesearch-map over wordlist-map ! ;
  193: 
  194: \ marker                                               18dec94py
  195: 
  196: \ Marker creates a mark that is removed (including everything 
  197: \ defined afterwards) when executing the mark.
  198: 
  199: : marker, ( -- mark )  here dup A,
  200:   voclink @ A, voclink
  201:   BEGIN  @ dup @  WHILE  dup 0 wordlist-link - @ A,  REPEAT  drop
  202:   udp @ , ;
  203: 
  204: : marker! ( mark -- )  dup @ swap cell+
  205:   dup @ voclink ! cell+
  206:   voclink
  207:   BEGIN  @ dup @  WHILE  over @ over 0 wordlist-link - !
  208: 	 swap cell+ swap
  209:   REPEAT  drop  voclink
  210:   BEGIN  @ dup @  WHILE  dup 0 wordlist-link - rehash  REPEAT  drop
  211:   @ udp !  dp ! ;
  212: 
  213: : marker ( "mark" -- )
  214:   marker, Create A,  DOES>  @ marker! ;

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