factor/core/optimizer/specializers/specializers.factor

65 lines
1.9 KiB
Factor
Raw Normal View History

2008-02-05 21:11:35 -05:00
! Copyright (C) 2006, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays generic hashtables kernel kernel.private math
namespaces sequences vectors words strings layouts combinators
2008-04-02 03:44:10 -04:00
sequences.private classes generic.standard
generic.standard.engines assocs ;
2008-02-05 21:11:35 -05:00
IN: optimizer.specializers
: (make-specializer) ( class picker -- quot )
swap "predicate" word-prop append ;
: make-specializer ( classes -- quot )
dup length <reversed>
[ (picker) 2array ] 2map
[ drop object eq? not ] assoc-subset
dup empty? [ drop [ t ] ] [
[ (make-specializer) ] { } assoc>map
unclip [ swap [ f ] \ if 3array append [ ] like ] reduce
] if ;
: tag-specializer ( quot -- newquot )
[
[ dup tag ] %
num-tags get swap <array> ,
\ dispatch ,
] [ ] make ;
2008-03-16 03:43:00 -04:00
: specializer-cases ( quot word -- default alist )
2008-02-12 16:52:32 -05:00
dup [ array? ] all? [ 1array ] unless [
[ make-specializer ] keep
[ declare ] curry pick append
] { } map>assoc ;
2008-03-16 03:43:00 -04:00
: method-declaration ( method -- quot )
dup "method-generic" word-prop dispatch# object <array>
swap "method-class" word-prop prefix ;
2008-03-16 03:43:00 -04:00
: specialize-method ( quot method -- quot' )
2008-03-19 20:15:43 -04:00
method-declaration [ declare ] curry prepend ;
2008-03-16 03:43:00 -04:00
: specialize-quot ( quot specializer -- quot' )
dup { number } = [
drop tag-specializer
] [
specializer-cases alist>quot
] if ;
: standard-method? ( method -- ? )
dup method-body? [
"method-generic" word-prop standard-generic?
] [ drop f ] if ;
2008-02-05 21:11:35 -05:00
: specialized-def ( word -- quot )
2008-03-16 03:43:00 -04:00
dup word-def swap {
{ [ dup standard-method? ] [ specialize-method ] }
{
[ dup "specializer" word-prop ]
[ "specializer" word-prop specialize-quot ]
}
2008-04-11 13:53:22 -04:00
[ drop ]
2008-03-16 03:43:00 -04:00
} cond ;
2008-02-05 21:11:35 -05:00
: specialized-length ( specializer -- n )
dup [ array? ] all? [ first ] when length ;