2005-01-29 14:18:28 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2004-07-16 02:26:21 -04:00
|
|
|
IN: words
|
2005-01-29 14:18:28 -05:00
|
|
|
USING: generic hashtables kernel kernel-internals lists math
|
2005-04-02 02:39:33 -05:00
|
|
|
namespaces sequences strings vectors ;
|
2005-03-26 20:12:14 -05:00
|
|
|
|
|
|
|
! Utility
|
|
|
|
GENERIC: (tree-each) ( quot obj -- ) inline
|
|
|
|
M: object (tree-each) swap call ;
|
|
|
|
M: cons (tree-each) [ car (tree-each) ] 2keep cdr (tree-each) ;
|
2005-04-03 16:55:56 -04:00
|
|
|
M: f (tree-each) swap call ;
|
2005-04-02 02:39:33 -05:00
|
|
|
M: sequence (tree-each) [ swap call ] seq-each-with ;
|
2005-03-26 20:12:14 -05:00
|
|
|
: tree-each swap (tree-each) ; inline
|
|
|
|
: tree-each-with ( obj vector quot -- )
|
|
|
|
swap [ with ] tree-each 2drop ; inline
|
|
|
|
|
|
|
|
! The basic word type. Words can be named and compared using
|
|
|
|
! identity. They hold a property map.
|
2005-01-14 19:51:38 -05:00
|
|
|
BUILTIN: word 17
|
2005-02-20 19:03:37 -05:00
|
|
|
[ 1 hashcode f ]
|
2005-03-05 14:45:23 -05:00
|
|
|
[ 4 "word-def" "set-word-def" ]
|
2005-02-20 19:03:37 -05:00
|
|
|
[ 5 "word-props" "set-word-props" ] ;
|
|
|
|
|
2005-03-26 20:12:14 -05:00
|
|
|
: word-prop ( word name -- value ) swap word-props hash ;
|
|
|
|
: set-word-prop ( word value name -- ) rot word-props set-hash ;
|
|
|
|
|
|
|
|
: word-name ( word -- str ) "name" word-prop ;
|
|
|
|
: word-vocabulary ( word -- str ) "vocabulary" word-prop ;
|
|
|
|
|
|
|
|
! Pointer to executable native code
|
2005-02-20 19:03:37 -05:00
|
|
|
GENERIC: word-xt
|
|
|
|
M: word word-xt ( w -- xt ) 2 integer-slot ;
|
|
|
|
GENERIC: set-word-xt
|
|
|
|
M: word set-word-xt ( xt w -- ) 2 set-integer-slot ;
|
|
|
|
|
2005-03-26 20:12:14 -05:00
|
|
|
! Primitive number; some are magic, see below.
|
2005-02-20 19:03:37 -05:00
|
|
|
GENERIC: word-primitive
|
|
|
|
M: word word-primitive ( w -- n ) 3 integer-slot ;
|
|
|
|
GENERIC: set-word-primitive
|
|
|
|
M: word set-word-primitive ( n w -- )
|
|
|
|
[ 3 set-integer-slot ] keep update-xt ;
|
|
|
|
|
2005-03-26 20:12:14 -05:00
|
|
|
! For the profiler
|
2005-02-20 19:03:37 -05:00
|
|
|
GENERIC: call-count
|
|
|
|
M: word call-count ( w -- n ) 6 integer-slot ;
|
|
|
|
GENERIC: set-call-count
|
|
|
|
M: word set-call-count ( n w -- ) 6 set-integer-slot ;
|
|
|
|
|
|
|
|
GENERIC: allot-count
|
|
|
|
M: word allot-count ( w -- n ) 7 integer-slot ;
|
|
|
|
GENERIC: set-allot-count
|
|
|
|
M: word set-allot-count ( n w -- ) 7 set-integer-slot ;
|
2004-12-18 23:18:32 -05:00
|
|
|
|
2005-03-26 20:12:14 -05:00
|
|
|
! The cross-referencer keeps track of word dependencies, so that
|
|
|
|
! words can be recompiled when redefined.
|
|
|
|
SYMBOL: crossref
|
2004-12-15 16:57:29 -05:00
|
|
|
|
2005-03-26 20:12:14 -05:00
|
|
|
global [ <namespace> crossref set ] bind
|
|
|
|
|
|
|
|
: (add-crossref)
|
|
|
|
dup word? [
|
|
|
|
crossref get [ dupd nest set-hash ] bind
|
|
|
|
] [
|
|
|
|
2drop
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: add-crossref ( word -- )
|
|
|
|
#! Marks each word in the quotation as being a dependency
|
|
|
|
#! of the word.
|
|
|
|
dup word-def [ (add-crossref) ] tree-each-with ;
|
|
|
|
|
|
|
|
: (remove-crossref)
|
|
|
|
dup word? [
|
|
|
|
crossref get [ nest remove-hash ] bind
|
|
|
|
] [
|
|
|
|
2drop
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: remove-crossref ( word -- )
|
|
|
|
#! Marks each word in the quotation as not being a
|
|
|
|
#! dependency of the word.
|
|
|
|
dup word-def [ (remove-crossref) ] tree-each-with ;
|
|
|
|
|
|
|
|
: usages ( word -- deps )
|
|
|
|
#! The transitive closure over the relation specified in
|
|
|
|
#! the crossref hash.
|
|
|
|
crossref get closure ;
|
|
|
|
|
|
|
|
GENERIC: (undefine) ( word -- )
|
|
|
|
M: word (undefine) drop ;
|
|
|
|
|
|
|
|
: undefine ( word -- )
|
2005-03-26 20:40:29 -05:00
|
|
|
dup (undefine) usages [ (undefine) ] each ;
|
2005-03-26 20:12:14 -05:00
|
|
|
|
|
|
|
! The word primitive combined with the word def specify what the
|
|
|
|
! word does when invoked.
|
|
|
|
|
|
|
|
: define ( word primitive parameter -- )
|
|
|
|
pick undefine
|
|
|
|
pick set-word-def
|
|
|
|
over set-word-primitive
|
|
|
|
f "parsing" set-word-prop ;
|
2004-11-25 21:51:47 -05:00
|
|
|
|
2005-03-23 22:49:40 -05:00
|
|
|
GENERIC: definer ( word -- word )
|
|
|
|
#! Return the parsing word that defined this word.
|
|
|
|
|
2005-03-26 20:12:14 -05:00
|
|
|
! Undefined words raise an error when invoked.
|
|
|
|
PREDICATE: word undefined ( obj -- ? ) word-primitive 0 = ;
|
|
|
|
M: undefined definer drop \ DEFER: ;
|
2005-03-23 22:49:40 -05:00
|
|
|
|
2005-03-26 20:12:14 -05:00
|
|
|
! Primitives are defined in the runtime.
|
2004-12-12 23:49:44 -05:00
|
|
|
PREDICATE: word primitive ( obj -- ? ) word-primitive 2 > ;
|
2005-03-23 22:49:40 -05:00
|
|
|
M: primitive definer drop \ PRIMITIVE: ;
|
|
|
|
|
2005-03-26 20:12:14 -05:00
|
|
|
! Symbols push themselves when executed.
|
2004-12-12 23:49:44 -05:00
|
|
|
PREDICATE: word symbol ( obj -- ? ) word-primitive 2 = ;
|
2005-03-23 22:49:40 -05:00
|
|
|
M: symbol definer drop \ SYMBOL: ;
|
|
|
|
|
2005-03-26 20:12:14 -05:00
|
|
|
: define-symbol ( word -- ) 2 over define ;
|
2004-11-25 21:51:47 -05:00
|
|
|
|
2005-03-26 20:12:14 -05:00
|
|
|
: intern-symbol ( word -- )
|
|
|
|
dup undefined? [ define-symbol ] [ drop ] ifte ;
|
2005-03-05 14:45:23 -05:00
|
|
|
|
2005-03-26 20:12:14 -05:00
|
|
|
! Compound words invoke a quotation when executed.
|
|
|
|
PREDICATE: word compound ( obj -- ? ) word-primitive 1 = ;
|
|
|
|
M: compound definer drop \ : ;
|
|
|
|
|
|
|
|
: (define-compound) ( word def -- )
|
|
|
|
>r dup dup remove-crossref r> 1 swap define add-crossref ;
|
2004-11-25 21:51:47 -05:00
|
|
|
|
2005-03-05 14:45:23 -05:00
|
|
|
: define-compound ( word def -- )
|
|
|
|
#! If the word is a generic word, clear the properties
|
|
|
|
#! involved so that 'see' can work properly.
|
|
|
|
over f "methods" set-word-prop
|
|
|
|
over f "combination" set-word-prop
|
|
|
|
(define-compound) ;
|