factor/library/vocabularies.factor

95 lines
2.8 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: words
2005-08-21 23:35:50 -04:00
USING: hashtables errors kernel lists namespaces strings
sequences ;
SYMBOL: vocabularies
2005-07-23 01:03:46 -04:00
: word ( -- word ) "last-word" global hash ;
: set-word ( word -- ) "last-word" global set-hash ;
: 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 ;
: words ( vocab -- list )
#! Push a list of all words in a vocabulary.
#! Filter empty slots.
2004-12-25 18:08:20 -05:00
vocab dup [ hash-values [ ] subset word-sort ] 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-04-22 00:22:36 -04:00
all-words swap subset word-sort ; inline
2004-07-16 02:26:21 -04:00
: word-subset-with ( obj pred -- list | pred: obj word -- ? )
all-words swap subset-with word-sort ; inline
: recrossref ( -- )
#! Update word cross referencing information.
2005-05-15 21:17:56 -04:00
global [ <namespace> crossref set ] bind
[ add-crossref ] each-word ;
: lookup ( name vocab -- word ) vocab ?hash ;
2005-07-23 01:03:46 -04:00
: search ( name vocabs -- word )
[ lookup ] map-with [ ] find nip ;
2004-07-16 02:26:21 -04:00
2005-01-28 23:55:22 -05:00
: <props> ( name vocab -- plist )
[ "vocabulary" set "name" set ] make-hash ;
2004-07-16 02:26:21 -04:00
: (create) ( name vocab -- word )
2004-10-17 16:04:49 -04:00
#! Create an undefined word without adding to a vocabulary.
2005-01-28 23:55:22 -05:00
<props> <word> [ set-word-props ] keep ;
2004-07-16 02:26:21 -04:00
: 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
2005-08-21 23:35:50 -04:00
[ nip ] [ (create) dup reveal ] ?ifte ;
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
2004-11-26 22:23:57 -05:00
dup word-vocabulary vocab [ word-name off ] bind ;
: interned? ( word -- ? )
#! Test if the word is a member of its vocabulary.
2005-07-23 01:03:46 -04:00
dup word-name over word-vocabulary vocab ?hash eq? ;
: init-search-path ( -- )
"scratchpad" "in" set
[
2005-08-23 15:50:32 -04:00
"compiler" "errors" "gadgets" "generic" "hashtables"
"help" "inference" "inspector" "interpreter" "io"
2005-07-06 01:57:58 -04:00
"jedit" "kernel" "listener" "lists" "math" "matrices"
2005-08-23 15:50:32 -04:00
"memory" "namespaces" "parser" "prettyprint" "queues"
"scratchpad" "sequences" "strings" "styles" "syntax"
"test" "threads" "vectors" "words"
] "use" set ;