factor/library/vocabularies.factor

90 lines
2.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: words
USING: errors hashtables kernel lists namespaces sequences
strings ;
! If true in current namespace, we are bootstrapping.
SYMBOL: bootstrapping?
SYMBOL: vocabularies
2005-08-25 15:27:38 -04:00
: word ( -- word ) \ word global hash ;
: set-word ( word -- ) \ word set-global ;
: vocabs ( -- list )
#! Push a list of vocabularies.
2005-08-24 10:19:09 -04:00
vocabularies get hash-keys string-sort ;
: vocab ( name -- vocab )
#! Get a vocabulary.
vocabularies get hash ;
: ensure-vocab ( name -- )
#! Create the vocabulary if it does not exist.
vocabularies get [ nest drop ] bind ;
: words ( vocab -- list )
#! Push a list of all words in a vocabulary.
#! Filter empty slots.
2005-10-03 19:53:32 -04:00
vocab dup [ hash-values ] when ;
2005-04-10 18:58:30 -04:00
: all-words ( -- list )
2005-07-23 01:03:46 -04:00
vocabs [ words ] map concat ;
2005-04-10 18:58:30 -04:00
: each-word ( quot -- )
#! Apply a quotation to each word in the image.
2005-04-10 18:58:30 -04:00
all-words swap each ; inline
: word-subset ( pred -- list | pred: word -- ? )
#! A list of words matching the predicate.
2005-08-25 15:27:38 -04:00
all-words swap subset ; inline
2004-07-16 02:26:21 -04:00
: word-subset-with ( obj pred -- list | pred: obj word -- ? )
2005-08-25 15:27:38 -04:00
all-words swap subset-with ; inline
: recrossref ( -- )
#! Update word cross referencing information.
crossref get clear-hash [ add-crossref ] each-word ;
: lookup ( name vocab -- word ) vocab ?hash ;
: reveal ( word -- )
#! Add a new word to its vocabulary.
vocabularies get [
2005-07-23 01:03:46 -04:00
dup word-name over word-vocabulary nest set-hash
] bind ;
2004-07-16 02:26:21 -04:00
2005-08-21 23:35:50 -04:00
: check-create ( name vocab -- )
string? [ "Vocabulary name is not a string" throw ] unless
string? [ "Word name is not a string" throw ] unless ;
2004-07-16 02:26:21 -04:00
: create ( name vocab -- word )
#! Create a new word in a vocabulary. If the vocabulary
#! already contains the word, the existing instance is
#! returned.
2dup check-create 2dup lookup dup
2005-11-27 17:45:48 -05:00
[ 2nip ] [ drop <word> dup init-word dup reveal ] if ;
2004-11-26 22:23:57 -05:00
: constructor-word ( string vocab -- word )
>r "<" swap ">" append3 r> create ;
2004-11-26 22:23:57 -05:00
: forget ( word -- )
#! Remove a word definition.
dup uncrossref
2005-10-04 03:16:50 -04:00
crossref get [ dupd remove-hash ] when*
2005-08-25 15:27:38 -04:00
dup word-name swap word-vocabulary vocab remove-hash ;
2005-11-27 17:45:48 -05:00
: target-word ( word -- word )
dup word-name swap word-vocabulary lookup ;
: interned? ( word -- ? )
#! Test if the word is a member of its vocabulary.
2005-11-27 17:45:48 -05:00
dup target-word eq? ;
2005-11-24 19:02:20 -05:00
: bootstrap-word ( word -- word )
dup word-name swap word-vocabulary
bootstrapping? get [
dup "syntax" = [ drop "!syntax" ] when
] when lookup ;