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-08-02 15:17:13 -04:00
|
|
|
USING: arrays definitions errors generic graphs hashtables
|
|
|
|
|
kernel kernel-internals math namespaces sequences strings
|
|
|
|
|
vectors ;
|
2005-03-26 20:12:14 -05:00
|
|
|
|
2006-08-10 16:47:10 -04:00
|
|
|
! Used by the compiler
|
|
|
|
|
SYMBOL: changed-words
|
|
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
: word-changed? ( word -- ? )
|
|
|
|
|
changed-words get [ hash-member? ] [ drop f ] if* ;
|
|
|
|
|
|
2006-08-11 16:55:43 -04:00
|
|
|
: changed-word ( word -- )
|
|
|
|
|
dup changed-words get [ set-hash ] [ 2drop ] if* ;
|
|
|
|
|
|
|
|
|
|
: unchanged-word ( word -- )
|
|
|
|
|
changed-words get [ remove-hash ] [ drop ] if* ;
|
2006-08-10 16:47:10 -04: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-08-15 21:23:05 -04:00
|
|
|
GENERIC: definer ( word -- definer )
|
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
|
|
|
|
2006-06-10 01:40:11 -04:00
|
|
|
SYMBOL: vocabularies
|
|
|
|
|
|
|
|
|
|
: vocab ( name -- vocab ) vocabularies get hash ;
|
|
|
|
|
|
|
|
|
|
: lookup ( name vocab -- word ) vocab ?hash ;
|
|
|
|
|
|
2006-08-17 23:08:04 -04:00
|
|
|
: target-word ( word -- target )
|
2006-06-10 01:40:11 -04:00
|
|
|
dup word-name swap word-vocabulary lookup ;
|
|
|
|
|
|
|
|
|
|
: interned? ( word -- ? ) dup target-word eq? ;
|
|
|
|
|
|
2006-08-15 21:23:05 -04:00
|
|
|
: uses ( word -- seq )
|
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-08-11 16:55:43 -04:00
|
|
|
: reset-props ( word seq -- ) [ remove-word-prop ] each-with ;
|
2005-04-10 18:58:30 -04:00
|
|
|
|
2006-11-13 00:16:22 -05:00
|
|
|
: custom-infer? ( word -- ? )
|
|
|
|
|
dup "infer" word-prop swap "infer-vars" word-prop or ;
|
|
|
|
|
|
2006-08-11 16:55:43 -04:00
|
|
|
: unxref-word* ( word -- )
|
|
|
|
|
{
|
|
|
|
|
{ [ dup compound? not ] [ drop ] }
|
2006-11-13 00:16:22 -05:00
|
|
|
{ [ dup custom-infer? ] [ drop ] }
|
2006-08-11 16:55:43 -04:00
|
|
|
{ [ t ] [
|
|
|
|
|
dup changed-word
|
2006-11-12 22:14:04 -05:00
|
|
|
{
|
|
|
|
|
"inferred-effect" "inferred-vars"
|
|
|
|
|
"base-case" "no-effect"
|
|
|
|
|
} reset-props
|
2006-08-11 16:55:43 -04:00
|
|
|
] }
|
|
|
|
|
} cond ;
|
2005-10-04 03:16:50 -04:00
|
|
|
|
2006-03-27 03:10:58 -05:00
|
|
|
: unxref-word ( word -- )
|
2006-07-25 01:37:54 -04:00
|
|
|
dup [ usage ] closure [ unxref-word* ] each
|
2006-03-27 03:10:58 -05:00
|
|
|
[ uses ] crossref get remove-vertex ;
|
2005-03-26 20:12:14 -05:00
|
|
|
|
2006-08-15 21:23:05 -04:00
|
|
|
: define ( word def primitive -- )
|
2006-08-11 16:55:43 -04:00
|
|
|
pick changed-word
|
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-09-29 17:07:24 -04:00
|
|
|
: define-symbol ( word -- )
|
|
|
|
|
dup symbol? [ drop ] [ dup 2 define ] if ;
|
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
|
|
|
|
2005-08-23 15:50:32 -04:00
|
|
|
: reset-word ( word -- )
|
2006-08-15 14:56:18 -04:00
|
|
|
{
|
|
|
|
|
"parsing" "inline" "foldable"
|
|
|
|
|
"predicating" "declared-effect"
|
|
|
|
|
} 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
|
|
|
|
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 ;
|
|
|
|
|
|
2006-08-15 16:29:35 -04:00
|
|
|
: words ( vocab -- seq ) vocab dup [ hash-values ] when ;
|
2006-03-27 03:10:58 -05:00
|
|
|
|
2006-12-02 23:34:25 -05:00
|
|
|
: all-words ( -- seq )
|
|
|
|
|
vocabularies get hash-values [ hash-values ] map concat ;
|
2006-03-27 03:10:58 -05:00
|
|
|
|
|
|
|
|
: 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
|
|
|
|
2006-09-13 00:48:42 -04:00
|
|
|
: create-vocab ( name -- vocab )
|
|
|
|
|
vocabularies get [ nest ] bind ;
|
|
|
|
|
|
2006-03-27 03:10:58 -05:00
|
|
|
: reveal ( word -- )
|
2006-09-13 00:48:42 -04:00
|
|
|
dup word-name over word-vocabulary create-vocab set-hash ;
|
2006-03-27 03:10:58 -05:00
|
|
|
|
2006-08-01 04:45:05 -04:00
|
|
|
TUPLE: check-create name vocab ;
|
|
|
|
|
: check-create ( name vocab -- name vocab )
|
|
|
|
|
dup string? [ <check-create> throw ] unless
|
|
|
|
|
over string? [ <check-create> throw ] unless ;
|
2006-03-27 03:10:58 -05:00
|
|
|
|
|
|
|
|
: create ( name vocab -- word )
|
2006-08-01 04:45:05 -04:00
|
|
|
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
|
|
|
|
2006-08-15 21:23:05 -04:00
|
|
|
: constructor-word ( name vocab -- word )
|
2006-12-10 14:59:32 -05:00
|
|
|
>r "<" swap ">" 3append r> create ;
|
2006-03-27 03:10:58 -05:00
|
|
|
|
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-08-16 21:55:53 -04:00
|
|
|
: bootstrap-word ( word -- target )
|
2006-03-27 03:10:58 -05:00
|
|
|
bootstrapping? get [
|
2006-07-26 00:38:19 -04:00
|
|
|
dup word-name swap word-vocabulary
|
2006-11-30 23:01:54 -05:00
|
|
|
dup "syntax" = [ [ CHAR: ! add* ] 2apply ] when lookup
|
2006-07-24 00:11:22 -04:00
|
|
|
] when ;
|
2006-08-02 15:17:13 -04:00
|
|
|
|
2006-08-24 00:45:58 -04:00
|
|
|
: words-named ( str -- seq )
|
|
|
|
|
all-words [ word-name = ] subset-with ;
|
|
|
|
|
|
2006-08-02 15:17:13 -04:00
|
|
|
! Definition protocol
|
2006-11-26 22:04:08 -05:00
|
|
|
M: word where "loc" word-prop ;
|
2006-08-02 15:17:13 -04:00
|
|
|
|
|
|
|
|
M: word subdefs drop f ;
|
|
|
|
|
|
|
|
|
|
: forget-word ( word -- )
|
|
|
|
|
dup unxref-word
|
|
|
|
|
dup remove-word-help
|
2006-08-11 16:55:43 -04:00
|
|
|
dup unchanged-word
|
2006-08-02 15:17:13 -04:00
|
|
|
crossref get [ dupd remove-hash ] when*
|
|
|
|
|
dup word-name swap word-vocabulary vocab remove-hash ;
|
|
|
|
|
|
|
|
|
|
M: word forget forget-word ;
|