factor/core/generic/standard/standard.factor

179 lines
4.8 KiB
Factor
Raw Normal View History

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 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
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-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 -- )
[ [ flatten-class keys ] keep ] 2dip [
[ 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> ;
: 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* [
drop generic get "default-method" word-prop mangle-method
2008-04-02 03:44:10 -04:00
] unless ;
: <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
[ mangle-method ] assoc-map
[ find-default default set ]
[ <big-dispatch-engine> ]
bi
]
} cleave ;
2008-04-02 19:50:21 -04:00
: single-combination ( word -- quot )
[ <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 [
[
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 )
[ [ 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' )
[ #>> (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
M: standard-combination make-default-method
2008-08-22 18:38:23 -04:00
[ error-method ] with-standard ;
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
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 ;
M: standard-generic effective-method
2008-04-11 09:35:07 -04:00
[ dispatch# (picker) call ] keep single-effective-method ;
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? ;
: 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
] with-variable ; inline
2008-11-28 02:08:16 -05:00
: prepend-hook-var ( quot -- quot' )
hook-combination get var>> [ get ] curry prepend ;
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 ;
M: hook-combination make-default-method
2008-11-28 02:08:16 -05:00
[ error-method prepend-hook-var ] with-hook ;
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 ;