2008-02-17 01:37:54 -05:00
|
|
|
! Copyright (C) 2005, 2008 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: arrays assocs kernel kernel.private slots.private math
|
|
|
|
namespaces sequences vectors words quotations definitions
|
2008-02-11 02:19:53 -05:00
|
|
|
hashtables layouts combinators sequences.private generic
|
2008-04-02 03:44:10 -04:00
|
|
|
classes classes.algebra classes.private generic.standard.engines
|
|
|
|
generic.standard.engines.tag generic.standard.engines.predicate
|
|
|
|
generic.standard.engines.tuple accessors ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: generic.standard
|
|
|
|
|
2008-04-02 22:27:49 -04:00
|
|
|
GENERIC: dispatch# ( word -- n )
|
|
|
|
|
2008-07-06 02:37:11 -04:00
|
|
|
M: generic dispatch#
|
|
|
|
"combination" word-prop dispatch# ;
|
|
|
|
|
|
|
|
GENERIC: method-declaration ( class generic -- quot )
|
|
|
|
|
|
|
|
M: generic method-declaration
|
|
|
|
"combination" word-prop method-declaration ;
|
|
|
|
|
|
|
|
M: quotation engine>quot
|
|
|
|
assumed get generic get method-declaration prepend ;
|
2008-04-02 22:27:49 -04:00
|
|
|
|
2008-03-20 16:00:49 -04:00
|
|
|
ERROR: no-method object generic ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 03:44:10 -04:00
|
|
|
: error-method ( word -- quot )
|
2007-09-20 18:09:08 -04:00
|
|
|
picker swap [ no-method ] curry append ;
|
|
|
|
|
2008-04-02 03:44:10 -04:00
|
|
|
: push-method ( method specializer atomic assoc -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
[
|
2008-04-02 03:44:10 -04:00
|
|
|
[ H{ } clone <predicate-dispatch-engine> ] unless*
|
|
|
|
[ methods>> set-at ] keep
|
|
|
|
] change-at ;
|
|
|
|
|
|
|
|
: flatten-method ( class method assoc -- )
|
|
|
|
>r >r dup flatten-class keys swap r> r> [
|
|
|
|
>r spin r> push-method
|
|
|
|
] 3curry each ;
|
|
|
|
|
|
|
|
: flatten-methods ( assoc -- assoc' )
|
|
|
|
H{ } clone [
|
|
|
|
[
|
|
|
|
flatten-method
|
|
|
|
] curry assoc-each
|
|
|
|
] keep ;
|
|
|
|
|
|
|
|
: <big-dispatch-engine> ( assoc -- engine )
|
|
|
|
flatten-methods
|
|
|
|
convert-tuple-methods
|
|
|
|
convert-hi-tag-methods
|
|
|
|
<lo-tag-dispatch-engine> ;
|
|
|
|
|
|
|
|
: find-default ( methods -- quot )
|
|
|
|
#! Side-effects methods.
|
2008-04-02 19:50:21 -04:00
|
|
|
object bootstrap-word swap delete-at* [
|
2008-04-02 03:44:10 -04:00
|
|
|
drop generic get "default-method" word-prop 1quotation
|
|
|
|
] unless ;
|
|
|
|
|
2008-04-05 21:07:30 -04:00
|
|
|
: mangle-method ( method generic -- quot )
|
|
|
|
[ 1quotation ] [ extra-values \ drop <repetition> ] bi*
|
|
|
|
prepend [ ] like ;
|
2008-04-02 03:44:10 -04:00
|
|
|
|
2008-04-02 19:50:21 -04:00
|
|
|
: single-combination ( word -- quot )
|
2008-04-02 03:44:10 -04:00
|
|
|
[
|
2008-04-02 19:50:21 -04:00
|
|
|
object bootstrap-word assumed set {
|
|
|
|
[ generic set ]
|
|
|
|
[ "engines" word-prop forget-all ]
|
|
|
|
[ V{ } clone "engines" set-word-prop ]
|
2008-04-02 03:44:10 -04:00
|
|
|
[
|
2008-04-02 19:50:21 -04:00
|
|
|
"methods" word-prop
|
|
|
|
[ generic get mangle-method ] assoc-map
|
|
|
|
[ find-default default set ]
|
2008-06-09 06:22:21 -04:00
|
|
|
[ <big-dispatch-engine> ]
|
|
|
|
bi engine>quot
|
2008-04-02 19:50:21 -04:00
|
|
|
]
|
|
|
|
} cleave
|
2008-04-02 03:44:10 -04:00
|
|
|
] with-scope ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-05 21:07:30 -04:00
|
|
|
ERROR: inconsistent-next-method class generic ;
|
|
|
|
|
|
|
|
ERROR: no-next-method class generic ;
|
|
|
|
|
|
|
|
: single-next-method-quot ( class generic -- quot )
|
|
|
|
[
|
2008-07-02 01:20:01 -04:00
|
|
|
[ drop "predicate" word-prop % ]
|
2008-04-05 21:07:30 -04:00
|
|
|
[
|
|
|
|
2dup next-method
|
|
|
|
[ 2nip 1quotation ]
|
2008-06-29 03:12:44 -04:00
|
|
|
[ [ no-next-method ] 2curry [ ] like ] if* ,
|
2008-04-05 21:07:30 -04:00
|
|
|
]
|
|
|
|
[ [ inconsistent-next-method ] 2curry , ]
|
|
|
|
2tri
|
|
|
|
\ if ,
|
|
|
|
] [ ] make ;
|
|
|
|
|
2008-04-11 09:35:07 -04:00
|
|
|
: single-effective-method ( obj word -- method )
|
2008-07-05 01:59:28 -04:00
|
|
|
[ [ order [ instance? ] with find-last nip ] keep method ]
|
|
|
|
[ "default-method" word-prop ]
|
|
|
|
bi or ;
|
2008-04-11 09:35:07 -04:00
|
|
|
|
2008-04-02 03:44:10 -04:00
|
|
|
TUPLE: standard-combination # ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 03:44:10 -04:00
|
|
|
C: <standard-combination> standard-combination
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 03:44:10 -04:00
|
|
|
PREDICATE: standard-generic < generic
|
|
|
|
"combination" word-prop standard-combination? ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 03:44:10 -04:00
|
|
|
PREDICATE: simple-generic < standard-generic
|
|
|
|
"combination" word-prop #>> zero? ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 03:44:10 -04:00
|
|
|
: define-simple-generic ( word -- )
|
|
|
|
T{ standard-combination f 0 } define-generic ;
|
|
|
|
|
|
|
|
: with-standard ( combination quot -- quot' )
|
2008-04-02 22:27:49 -04:00
|
|
|
>r #>> (dispatch#) r> with-variable ; inline
|
2008-04-02 03:44:10 -04:00
|
|
|
|
2008-04-05 21:07:30 -04:00
|
|
|
M: standard-generic extra-values drop 0 ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-02-04 17:20:07 -05:00
|
|
|
M: standard-combination make-default-method
|
2008-08-22 18:38:23 -04:00
|
|
|
[ error-method ] with-standard ;
|
2008-02-04 17:20:07 -05:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
M: standard-combination perform-combination
|
2008-04-02 19:50:21 -04:00
|
|
|
[ drop ] [ [ single-combination ] with-standard ] 2bi define ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 22:27:49 -04:00
|
|
|
M: standard-combination dispatch# #>> ;
|
|
|
|
|
2008-07-06 02:37:11 -04:00
|
|
|
M: standard-combination method-declaration
|
|
|
|
dispatch# object <array> swap prefix [ declare ] curry [ ] like ;
|
|
|
|
|
2008-04-05 21:07:30 -04:00
|
|
|
M: standard-combination next-method-quot*
|
|
|
|
[
|
|
|
|
single-next-method-quot picker prepend
|
|
|
|
] with-standard ;
|
|
|
|
|
2008-04-03 18:33:06 -04:00
|
|
|
M: standard-generic effective-method
|
2008-04-11 09:35:07 -04:00
|
|
|
[ dispatch# (picker) call ] keep single-effective-method ;
|
2008-04-03 18:33:06 -04:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
TUPLE: hook-combination var ;
|
|
|
|
|
|
|
|
C: <hook-combination> hook-combination
|
|
|
|
|
2008-04-02 03:44:10 -04:00
|
|
|
PREDICATE: hook-generic < generic
|
|
|
|
"combination" word-prop hook-combination? ;
|
|
|
|
|
2008-02-04 17:20:07 -05:00
|
|
|
: with-hook ( combination quot -- quot' )
|
2007-09-20 18:09:08 -04:00
|
|
|
0 (dispatch#) [
|
2008-04-02 03:44:10 -04:00
|
|
|
dip var>> [ get ] curry prepend
|
2008-02-04 17:20:07 -05:00
|
|
|
] with-variable ; inline
|
|
|
|
|
2008-04-02 22:27:49 -04:00
|
|
|
M: hook-combination dispatch# drop 0 ;
|
|
|
|
|
2008-07-06 02:37:11 -04:00
|
|
|
M: hook-combination method-declaration 2drop [ ] ;
|
|
|
|
|
2008-04-05 21:07:30 -04:00
|
|
|
M: hook-generic extra-values drop 1 ;
|
2008-04-02 03:44:10 -04:00
|
|
|
|
2008-04-11 09:35:07 -04:00
|
|
|
M: hook-generic effective-method
|
|
|
|
[ "combination" word-prop var>> get ] keep
|
|
|
|
single-effective-method ;
|
|
|
|
|
2008-02-04 17:20:07 -05:00
|
|
|
M: hook-combination make-default-method
|
|
|
|
[ error-method ] with-hook ;
|
|
|
|
|
|
|
|
M: hook-combination perform-combination
|
2008-04-02 19:50:21 -04:00
|
|
|
[ drop ] [ [ single-combination ] with-hook ] 2bi define ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-05 21:07:30 -04:00
|
|
|
M: hook-combination next-method-quot*
|
|
|
|
[ single-next-method-quot ] with-hook ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
M: simple-generic definer drop \ GENERIC: f ;
|
2008-01-06 11:13:44 -05:00
|
|
|
|
|
|
|
M: standard-generic definer drop \ GENERIC# f ;
|
|
|
|
|
|
|
|
M: hook-generic definer drop \ HOOK: f ;
|