FIND

Extend the semantics of 6.1.1550 FIND to be: ( c-addr -- c-addr 0 | xt 1 | xt -1 )

Find the definition named in the counted string at c-addr. If the definition is not found after searching all the word lists in the search order, return c-addr and zero. If the definition is found, return xt. If the definition is immediate, also return one (1); otherwise also return minus-one (-1). For a given string, the values returned by FIND while compiling may differ from those returned while not compiling.

See:

Implementation:

Assuming #order and context are defined as per E.16.6.1.1647 GET-ORDER.

: FIND ( c-addr -- c-addr 0 | xt 1 | xt -1 )
   0                              ( c-addr 0 )
   #order @ 0 ?DO
      OVER COUNT                  ( c-addr 0 c-addr' u )
      I CELLS context + @         ( c-addr 0 c-addr' u wid )
      SEARCH-WORDLIST             ( c-addr 0; 0 | w 1 | q -1 )
      ?DUP IF                     ( c-addr 0; w 1 | w -1 )
             2SWAP 2DROP LEAVE    ( w 1 | w -1 )
         THEN                     ( c-addr 0 )
      LOOP                        ( c-addr 0 | w 1 | w -1 )
   ;

Testing:

: c"dup" C" DUP" ;
: c".(" C" .(" ;
: c"x" C" unknown word" ;

T{ c"dup" FIND -> xt  @ -1 }T
T{ c".("  FIND -> xti @  1 }T
T{ c"x"   FIND -> c"x"   0 }T

ContributeContributions