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
|
2008-09-10 21:07:00 -04:00
|
|
|
namespaces make 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 -- )
|
2008-12-15 22:20:32 -05:00
|
|
|
[ [ flatten-class keys ] keep ] 2dip [
|
2008-11-23 03:44:56 -05:00
|
|
|
[ spin ] dip push-method
|
2008-04-02 03:44:10 -04:00
|
|
|
] 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> ;
|
|
|
|
|
2009-02-23 21:27:05 -05:00
|
|
|
: mangle-method ( method -- quot )
|
|
|
|
1quotation generic get extra-values \ drop <repetition>
|
|
|
|
prepend [ ] like ;
|
|
|
|
|
2008-04-02 03:44:10 -04:00
|
|
|
: find-default ( methods -- quot )
|
|
|
|
#! Side-effects methods.
|
2008-04-02 19:50:21 -04:00
|
|
|
object bootstrap-word swap delete-at* [
|
2009-02-23 21:27:05 -05:00
|
|
|
drop generic get "default-method" word-prop mangle-method
|
2008-04-02 03:44:10 -04:00
|
|
|
] unless ;
|
|
|
|
|
2008-11-05 21:23:38 -05:00
|
|
|
: <standard-engine> ( word -- engine )
|
|
|
|
object bootstrap-word assumed set {
|
|
|
|
[ generic set ]
|
|
|
|
[ "engines" word-prop forget-all ]
|
|
|
|
[ V{ } clone "engines" set-word-prop ]
|
|
|
|
[
|
|
|
|
"methods" word-prop
|
2009-02-23 21:27:05 -05:00
|
|
|
[ mangle-method ] assoc-map
|
2008-11-05 21:23:38 -05:00
|
|
|
[ find-default default set ]
|
|
|
|
[ <big-dispatch-engine> ]
|
|
|
|
bi
|
|
|
|
]
|
|
|
|
} cleave ;
|
|
|
|
|
2008-04-02 19:50:21 -04:00
|
|
|
: single-combination ( word -- quot )
|
2008-11-05 21:23:38 -05:00
|
|
|
[ <standard-engine> engine>quot ] with-scope ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-05 21:07:30 -04:00
|
|
|
ERROR: inconsistent-next-method class generic ;
|
|
|
|
|
2008-11-28 02:08:16 -05:00
|
|
|
: single-next-method-quot ( class generic -- quot/f )
|
|
|
|
2dup next-method dup [
|
|
|
|
[
|
2008-11-28 01:56:54 -05:00
|
|
|
pick "predicate" word-prop %
|
|
|
|
1quotation ,
|
|
|
|
[ inconsistent-next-method ] 2curry ,
|
|
|
|
\ if ,
|
2008-11-28 02:08:16 -05:00
|
|
|
] [ ] make
|
|
|
|
] [ 3drop f ] if ;
|
2008-04-05 21:07:30 -04:00
|
|
|
|
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-11-23 03:44:56 -05:00
|
|
|
[ #>> (dispatch#) ] dip 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*
|
|
|
|
[
|
2008-11-28 02:08:16 -05:00
|
|
|
single-next-method-quot
|
|
|
|
dup [ picker prepend ] when
|
2008-04-05 21:07:30 -04:00
|
|
|
] 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-11-28 02:08:16 -05:00
|
|
|
[ hook-combination ] dip with-variable
|
2008-02-04 17:20:07 -05:00
|
|
|
] with-variable ; inline
|
|
|
|
|
2008-11-28 02:08:16 -05:00
|
|
|
: prepend-hook-var ( quot -- quot' )
|
|
|
|
hook-combination get var>> [ get ] curry prepend ;
|
|
|
|
|
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
|
2008-11-28 02:08:16 -05:00
|
|
|
[ error-method prepend-hook-var ] with-hook ;
|
2008-02-04 17:20:07 -05:00
|
|
|
|
|
|
|
M: hook-combination perform-combination
|
2008-11-28 02:08:16 -05:00
|
|
|
[ drop ] [
|
|
|
|
[ single-combination prepend-hook-var ] 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*
|
2008-11-28 02:08:16 -05:00
|
|
|
[
|
|
|
|
single-next-method-quot
|
|
|
|
dup [ prepend-hook-var ] when
|
|
|
|
] with-hook ;
|
2008-04-05 21:07:30 -04:00
|
|
|
|
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 ;
|