2006-01-06 02:04:42 -05:00
|
|
|
! Copyright (C) 2004, 2006 Slava Pestov.
|
2006-03-27 03:10:58 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2006-06-22 22:36:56 -04:00
|
|
|
IN: help
|
|
|
|
|
DEFER: remove-word-help
|
|
|
|
|
|
2004-07-16 02:26:21 -04:00
|
|
|
IN: words
|
2006-06-04 15:35:00 -04:00
|
|
|
USING: arrays errors graphs hashtables kernel kernel-internals
|
2006-03-27 03:10:58 -05:00
|
|
|
math namespaces sequences strings vectors ;
|
2005-03-26 20:12:14 -05:00
|
|
|
|
2006-05-18 22:02:20 -04:00
|
|
|
M: word <=>
|
2006-06-04 15:35:00 -04:00
|
|
|
[ dup word-name swap word-vocabulary 2array ] 2apply <=> ;
|
2006-01-09 01:34:23 -05:00
|
|
|
|
2006-01-06 02:04:42 -05:00
|
|
|
GENERIC: definer ( word -- word )
|
2005-11-27 17:45:48 -05:00
|
|
|
|
2006-01-06 02:04:42 -05:00
|
|
|
PREDICATE: word undefined ( obj -- ? ) word-primitive 0 = ;
|
|
|
|
|
M: undefined definer drop \ DEFER: ;
|
2005-02-20 19:03:37 -05:00
|
|
|
|
2006-01-06 02:04:42 -05:00
|
|
|
PREDICATE: word compound ( obj -- ? ) word-primitive 1 = ;
|
|
|
|
|
M: compound definer drop \ : ;
|
|
|
|
|
|
|
|
|
|
PREDICATE: word primitive ( obj -- ? ) word-primitive 2 > ;
|
|
|
|
|
M: primitive definer drop \ PRIMITIVE: ;
|
|
|
|
|
|
|
|
|
|
PREDICATE: word symbol ( obj -- ? ) word-primitive 2 = ;
|
|
|
|
|
M: symbol definer drop \ SYMBOL: ;
|
|
|
|
|
|
|
|
|
|
: init-word ( word -- ) H{ } clone swap set-word-props ;
|
|
|
|
|
|
|
|
|
|
: word-prop ( word name -- value ) swap word-props hash ;
|
|
|
|
|
|
|
|
|
|
: remove-word-prop ( word name -- )
|
|
|
|
|
swap word-props remove-hash ;
|
2005-10-04 03:16:50 -04:00
|
|
|
|
2005-09-27 14:35:30 -04:00
|
|
|
: set-word-prop ( word value name -- )
|
2006-01-06 02:04:42 -05:00
|
|
|
over
|
|
|
|
|
[ rot word-props set-hash ]
|
|
|
|
|
[ nip remove-word-prop ] if ;
|
2005-03-26 20:12:14 -05:00
|
|
|
|
2005-02-20 19:03:37 -05:00
|
|
|
GENERIC: word-xt
|
2005-08-29 02:34:04 -04:00
|
|
|
M: word word-xt ( w -- xt ) 7 integer-slot ;
|
2005-10-04 03:16:50 -04:00
|
|
|
|
2005-02-20 19:03:37 -05:00
|
|
|
GENERIC: set-word-xt
|
2005-08-29 02:34:04 -04:00
|
|
|
M: word set-word-xt ( xt w -- ) 7 set-integer-slot ;
|
2005-02-20 19:03:37 -05:00
|
|
|
|
2006-06-10 01:40:11 -04:00
|
|
|
SYMBOL: vocabularies
|
|
|
|
|
|
|
|
|
|
: vocab ( name -- vocab ) vocabularies get hash ;
|
|
|
|
|
|
|
|
|
|
: lookup ( name vocab -- word ) vocab ?hash ;
|
|
|
|
|
|
|
|
|
|
: target-word ( word -- word )
|
|
|
|
|
dup word-name swap word-vocabulary lookup ;
|
|
|
|
|
|
|
|
|
|
: interned? ( word -- ? ) dup target-word eq? ;
|
|
|
|
|
|
2006-03-21 00:44:19 -05:00
|
|
|
: uses ( word -- uses )
|
2006-06-10 00:53:29 -04:00
|
|
|
word-def flatten
|
|
|
|
|
[ word? ] subset
|
2006-06-10 01:30:16 -04:00
|
|
|
[ global [ interned? ] bind ] subset
|
2006-06-10 00:53:29 -04:00
|
|
|
prune ;
|
2005-08-31 21:06:13 -04:00
|
|
|
|
2005-03-26 20:12:14 -05:00
|
|
|
SYMBOL: crossref
|
2004-12-15 16:57:29 -05:00
|
|
|
|
2006-03-27 03:10:58 -05:00
|
|
|
: xref-word ( word -- )
|
2006-05-25 01:29:45 -04:00
|
|
|
dup word-vocabulary [
|
2006-06-10 00:53:29 -04:00
|
|
|
[ uses ] crossref get add-vertex
|
2006-05-25 01:29:45 -04:00
|
|
|
] [
|
|
|
|
|
drop
|
|
|
|
|
] if ;
|
2005-03-26 20:12:14 -05:00
|
|
|
|
2006-03-27 03:10:58 -05:00
|
|
|
: usage ( word -- seq ) crossref get in-edges ;
|
2005-03-26 20:12:14 -05:00
|
|
|
|
2006-03-27 03:10:58 -05:00
|
|
|
: usages ( word -- deps ) crossref get closure ;
|
2006-01-06 02:04:42 -05:00
|
|
|
|
2006-03-27 03:10:58 -05:00
|
|
|
GENERIC: unxref-word* ( word -- )
|
2005-04-10 18:58:30 -04:00
|
|
|
|
2006-03-27 03:10:58 -05:00
|
|
|
M: word unxref-word* drop ;
|
2005-10-04 03:16:50 -04:00
|
|
|
|
2006-03-27 03:10:58 -05:00
|
|
|
: unxref-word ( word -- )
|
|
|
|
|
dup unxref-word* dup usages [ unxref-word* ] each
|
|
|
|
|
[ uses ] crossref get remove-vertex ;
|
2005-03-26 20:12:14 -05:00
|
|
|
|
2006-01-06 02:04:42 -05:00
|
|
|
: define ( word parameter primitive -- )
|
2006-03-27 03:10:58 -05:00
|
|
|
pick unxref-word
|
2006-01-06 02:04:42 -05:00
|
|
|
pick set-word-primitive
|
|
|
|
|
over set-word-def
|
|
|
|
|
dup update-xt
|
2006-03-27 03:10:58 -05:00
|
|
|
xref-word ;
|
2005-03-23 22:49:40 -05:00
|
|
|
|
2006-01-06 02:04:42 -05:00
|
|
|
: define-symbol ( word -- ) dup 2 define ;
|
2004-11-25 21:51:47 -05:00
|
|
|
|
2005-03-26 20:12:14 -05:00
|
|
|
: intern-symbol ( word -- )
|
2005-09-24 15:21:17 -04:00
|
|
|
dup undefined? [ define-symbol ] [ drop ] if ;
|
2005-03-05 14:45:23 -05:00
|
|
|
|
2006-01-06 02:04:42 -05:00
|
|
|
: define-compound ( word def -- ) 1 define ;
|
2004-11-25 21:51:47 -05:00
|
|
|
|
2006-01-06 02:04:42 -05:00
|
|
|
: reset-props ( word seq -- ) [ remove-word-prop ] each-with ;
|
2005-08-21 23:35:50 -04:00
|
|
|
|
2005-08-23 15:50:32 -04:00
|
|
|
: reset-word ( word -- )
|
2006-06-05 23:26:44 -04:00
|
|
|
{ "parsing" "inline" "foldable" "predicating" } reset-props ;
|
2005-08-21 23:35:50 -04:00
|
|
|
|
2005-08-23 15:50:32 -04:00
|
|
|
: reset-generic ( word -- )
|
2005-10-29 23:25:38 -04:00
|
|
|
dup reset-word { "methods" "combination" } reset-props ;
|
2005-07-13 22:51:43 -04:00
|
|
|
|
2006-05-29 04:27:30 -04:00
|
|
|
: <word> ( name vocab -- word ) (word) dup init-word ;
|
|
|
|
|
|
2005-08-31 21:06:13 -04:00
|
|
|
: gensym ( -- word )
|
2006-06-10 00:53:29 -04:00
|
|
|
"G:" \ gensym counter number>string append f <word> ;
|
2006-05-29 04:27:30 -04:00
|
|
|
|
|
|
|
|
: define-temp ( quot -- word )
|
|
|
|
|
gensym [ swap define-compound ] keep ;
|
2006-03-22 02:07:21 -05:00
|
|
|
|
|
|
|
|
: completions ( substring words -- seq )
|
|
|
|
|
[ word-name subseq? ] subset-with ;
|
2006-03-27 03:10:58 -05:00
|
|
|
|
|
|
|
|
SYMBOL: bootstrapping?
|
|
|
|
|
|
|
|
|
|
: word ( -- word ) \ word get-global ;
|
|
|
|
|
|
|
|
|
|
: set-word ( word -- ) \ word set-global ;
|
|
|
|
|
|
|
|
|
|
: vocabs ( -- seq ) vocabularies get hash-keys natural-sort ;
|
|
|
|
|
|
|
|
|
|
: ensure-vocab ( name -- ) vocabularies get [ nest drop ] bind ;
|
|
|
|
|
|
|
|
|
|
: words ( vocab -- list ) vocab dup [ hash-values ] when ;
|
|
|
|
|
|
|
|
|
|
: all-words ( -- list ) vocabs [ words ] map concat ;
|
|
|
|
|
|
|
|
|
|
: word-subset ( pred -- list )
|
|
|
|
|
all-words swap subset ; inline
|
|
|
|
|
|
|
|
|
|
: word-subset-with ( obj pred -- list | pred: obj word -- ? )
|
|
|
|
|
all-words swap subset-with ; inline
|
|
|
|
|
|
|
|
|
|
: xref-words ( -- )
|
2006-06-04 02:00:59 -04:00
|
|
|
all-words [ uses ] crossref get build-graph ;
|
2006-03-27 03:10:58 -05:00
|
|
|
|
|
|
|
|
: reveal ( word -- )
|
|
|
|
|
vocabularies get [
|
|
|
|
|
dup word-name over word-vocabulary nest set-hash
|
|
|
|
|
] bind ;
|
|
|
|
|
|
|
|
|
|
: check-create ( name vocab -- )
|
|
|
|
|
string? [ "Vocabulary name is not a string" throw ] unless
|
|
|
|
|
string? [ "Word name is not a string" throw ] unless ;
|
|
|
|
|
|
|
|
|
|
: create ( name vocab -- word )
|
|
|
|
|
2dup check-create 2dup lookup dup
|
2006-05-29 04:27:30 -04:00
|
|
|
[ 2nip ] [ drop <word> dup reveal ] if ;
|
2006-03-27 03:10:58 -05:00
|
|
|
|
|
|
|
|
: constructor-word ( string vocab -- word )
|
|
|
|
|
>r "<" swap ">" append3 r> create ;
|
|
|
|
|
|
|
|
|
|
: forget ( word -- )
|
|
|
|
|
dup unxref-word
|
2006-06-22 22:36:56 -04:00
|
|
|
dup remove-word-help
|
2006-06-10 00:53:29 -04:00
|
|
|
dup "forget-hook" word-prop call
|
2006-05-25 01:29:45 -04:00
|
|
|
crossref get [ dupd remove-hash ] when*
|
2006-03-27 03:10:58 -05:00
|
|
|
dup word-name swap word-vocabulary vocab remove-hash ;
|
|
|
|
|
|
2006-04-03 01:33:52 -04:00
|
|
|
: forget-vocab ( vocab -- )
|
2006-06-10 00:53:29 -04:00
|
|
|
words [ forget ] each ;
|
2006-04-03 01:33:52 -04:00
|
|
|
|
2006-03-27 03:10:58 -05:00
|
|
|
: bootstrap-word ( word -- word )
|
|
|
|
|
bootstrapping? get [
|
2006-07-24 00:11:22 -04:00
|
|
|
dup word-name swap word-vocabulary
|
2006-06-19 03:41:42 -04:00
|
|
|
dup "syntax" = [
|
|
|
|
|
drop "!syntax" >r "!" swap append r>
|
2006-07-24 00:11:22 -04:00
|
|
|
] when lookup
|
|
|
|
|
] when ;
|