factor/library/generic/predicate.factor

49 lines
1.3 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2005 Slava Pestov.
2005-02-18 20:37:01 -05:00
! See http://factor.sf.net/license.txt for BSD license.
IN: generic
2005-02-18 20:37:01 -05:00
USING: errors hashtables kernel lists namespaces parser strings
words vectors ;
! Predicate metaclass for generalized predicate dispatch.
SYMBOL: predicate
: predicate-dispatch ( existing definition class -- dispatch )
[
\ dup , "predicate" word-prop append, , , \ ifte ,
] make-list ;
2004-12-19 03:04:03 -05:00
: predicate-method ( vtable definition class type# -- )
>r rot r> swap [
vector-nth
( vtable definition class existing )
-rot predicate-dispatch
] 2keep set-vector-nth ;
predicate [
"superclass" word-prop builtin-supertypes
] "builtin-supertypes" set-word-prop
predicate [
( generic vtable definition class -- )
dup builtin-supertypes [
( vtable definition class type# )
2004-12-19 03:04:03 -05:00
>r 3dup r> predicate-method
] each 2drop 2drop
] "add-method" set-word-prop
predicate 25 "priority" set-word-prop
2004-12-29 18:01:23 -05:00
predicate [
2dup = [
2drop t
] [
>r "superclass" word-prop r> class<
2004-12-29 18:01:23 -05:00
] ifte
] "class<" set-word-prop
2004-12-29 18:01:23 -05:00
: define-predicate ( class predicate definition -- )
pick "superclass" word-prop "predicate" word-prop
2004-12-23 01:14:07 -05:00
[ \ dup , append, , [ drop f ] , \ ifte , ] make-list
define-compound
predicate "metaclass" set-word-prop ;