2010-01-14 10:10:13 -05:00
|
|
|
! Copyright (C) 2008, 2010 Slava Pestov.
|
2008-07-12 02:08:30 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-05-25 16:35:50 -04:00
|
|
|
USING: accessors arrays assocs byte-arrays byte-vectors classes
|
2009-09-01 15:39:22 -04:00
|
|
|
combinators definitions effects fry generic generic.single
|
2010-04-19 02:13:21 -04:00
|
|
|
generic.standard hashtables io.binary io.encodings
|
2010-11-16 06:13:15 -05:00
|
|
|
io.streams.string kernel kernel.private math math.parser
|
|
|
|
namespaces parser sbufs sequences splitting splitting.private
|
|
|
|
strings vectors words ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: hints
|
|
|
|
|
2008-11-29 13:03:56 -05:00
|
|
|
GENERIC: specializer-predicate ( spec -- quot )
|
2008-08-22 18:38:23 -04:00
|
|
|
|
2011-11-22 02:00:52 -05:00
|
|
|
M: class specializer-predicate predicate-def ;
|
2008-11-29 13:03:56 -05:00
|
|
|
|
|
|
|
M: object specializer-predicate '[ _ eq? ] ;
|
|
|
|
|
|
|
|
GENERIC: specializer-declaration ( spec -- class )
|
|
|
|
|
|
|
|
M: class specializer-declaration ;
|
|
|
|
|
2011-10-24 07:47:42 -04:00
|
|
|
M: object specializer-declaration class-of ;
|
2008-11-29 13:03:56 -05:00
|
|
|
|
2009-09-01 15:39:22 -04:00
|
|
|
: specializer ( word -- specializer )
|
2009-09-02 00:13:08 -04:00
|
|
|
"specializer" word-prop ;
|
2009-09-01 15:39:22 -04:00
|
|
|
|
2008-11-29 13:03:56 -05:00
|
|
|
: make-specializer ( specs -- quot )
|
2010-01-14 10:10:13 -05:00
|
|
|
dup length iota <reversed>
|
2008-08-22 18:38:23 -04:00
|
|
|
[ (picker) 2array ] 2map
|
|
|
|
[ drop object eq? not ] assoc-filter
|
2008-09-06 20:13:59 -04:00
|
|
|
[ [ t ] ] [
|
2008-11-29 13:03:56 -05:00
|
|
|
[ swap specializer-predicate append ] { } assoc>map
|
2009-04-06 18:50:54 -04:00
|
|
|
[ ] [ swap [ f ] \ if 3array append [ ] like ] map-reduce
|
2008-09-06 20:13:59 -04:00
|
|
|
] if-empty ;
|
2008-08-22 18:38:23 -04:00
|
|
|
|
2009-09-01 16:49:08 -04:00
|
|
|
: specializer-cases ( quot specializer -- alist )
|
2008-08-22 18:38:23 -04:00
|
|
|
dup [ array? ] all? [ 1array ] unless [
|
2009-09-01 16:49:08 -04:00
|
|
|
[ nip make-specializer ]
|
|
|
|
[ [ specializer-declaration ] map swap '[ _ declare @ ] ] 2bi
|
|
|
|
] with { } map>assoc ;
|
2008-08-22 18:38:23 -04:00
|
|
|
|
2009-09-25 19:50:08 -04:00
|
|
|
: specialize-quot ( quot specializer -- quot' )
|
|
|
|
[ drop ] [ specializer-cases ] 2bi alist>quot ;
|
2009-03-26 22:25:21 -04:00
|
|
|
|
2009-04-24 17:53:30 -04:00
|
|
|
: method-declaration ( method -- quot )
|
|
|
|
[ "method-generic" word-prop dispatch# object <array> ]
|
|
|
|
[ "method-class" word-prop ]
|
|
|
|
bi prefix [ declare ] curry [ ] like ;
|
|
|
|
|
2008-08-22 18:38:23 -04:00
|
|
|
: specialize-method ( quot method -- quot' )
|
2010-01-20 18:06:28 -05:00
|
|
|
[ method-declaration prepend ]
|
2009-09-25 19:50:08 -04:00
|
|
|
[ "method-generic" word-prop ] bi
|
|
|
|
specializer [ specialize-quot ] when* ;
|
2008-08-22 18:38:23 -04:00
|
|
|
|
|
|
|
: standard-method? ( method -- ? )
|
2010-02-01 02:08:24 -05:00
|
|
|
dup method? [
|
2008-08-22 18:38:23 -04:00
|
|
|
"method-generic" word-prop standard-generic?
|
|
|
|
] [ drop f ] if ;
|
|
|
|
|
2009-09-02 12:45:30 -04:00
|
|
|
: specialized-def ( word -- quot )
|
2008-12-17 15:57:24 -05:00
|
|
|
[ def>> ] keep
|
2009-03-26 22:25:21 -04:00
|
|
|
dup generic? [ drop ] [
|
|
|
|
[ dup standard-method? [ specialize-method ] [ drop ] if ]
|
2009-09-25 19:50:08 -04:00
|
|
|
[ specializer [ specialize-quot ] when* ]
|
2009-03-26 22:25:21 -04:00
|
|
|
bi
|
|
|
|
] if ;
|
2008-08-22 18:38:23 -04:00
|
|
|
|
|
|
|
: specialized-length ( specializer -- n )
|
|
|
|
dup [ array? ] all? [ first ] when length ;
|
|
|
|
|
2013-03-04 20:34:20 -05:00
|
|
|
ERROR: cannot-specialize word specializer ;
|
|
|
|
|
|
|
|
: set-specializer ( word specializer -- )
|
|
|
|
over inline-recursive? [ cannot-specialize ] when
|
|
|
|
"specializer" set-word-prop ;
|
|
|
|
|
2009-03-21 02:27:50 -04:00
|
|
|
SYNTAX: HINTS:
|
2009-08-09 04:07:33 -04:00
|
|
|
scan-object dup wrapper? [ wrapped>> ] when
|
2009-04-20 19:44:45 -04:00
|
|
|
[ changed-definition ]
|
2009-08-18 20:40:54 -04:00
|
|
|
[ subwords [ changed-definition ] each ]
|
2013-03-04 20:34:20 -05:00
|
|
|
[ parse-definition { } like set-specializer ] tri ;
|
2008-08-22 18:38:23 -04:00
|
|
|
|
|
|
|
! Default specializers
|
2013-03-04 20:34:20 -05:00
|
|
|
{ pop* pop push last } [
|
|
|
|
{ vector } set-specializer
|
2008-08-22 18:38:23 -04:00
|
|
|
] each
|
|
|
|
|
2013-03-04 20:34:20 -05:00
|
|
|
\ set-last { { object vector } } set-specializer
|
2009-07-28 12:51:47 -04:00
|
|
|
|
2008-08-22 18:38:23 -04:00
|
|
|
\ push-all
|
|
|
|
{ { string sbuf } { array vector } { byte-array byte-vector } }
|
2013-03-04 20:34:20 -05:00
|
|
|
set-specializer
|
2008-08-22 18:38:23 -04:00
|
|
|
|
2012-09-11 13:34:50 -04:00
|
|
|
{ append prepend } [
|
|
|
|
{ { string string } { array array } }
|
2013-03-04 20:34:20 -05:00
|
|
|
set-specializer
|
2012-09-11 13:34:50 -04:00
|
|
|
] each
|
2012-07-16 14:07:13 -04:00
|
|
|
|
2008-08-22 18:38:23 -04:00
|
|
|
\ subseq
|
|
|
|
{ { fixnum fixnum string } { fixnum fixnum array } }
|
2013-03-04 20:34:20 -05:00
|
|
|
set-specializer
|
2008-08-22 18:38:23 -04:00
|
|
|
|
2009-10-28 15:40:15 -04:00
|
|
|
\ reverse!
|
2008-08-22 18:38:23 -04:00
|
|
|
{ { string } { array } }
|
2013-03-04 20:34:20 -05:00
|
|
|
set-specializer
|
2008-08-22 18:38:23 -04:00
|
|
|
|
|
|
|
\ mismatch
|
|
|
|
{ string string }
|
2013-03-04 20:34:20 -05:00
|
|
|
set-specializer
|
2008-08-22 18:38:23 -04:00
|
|
|
|
2013-03-04 20:34:20 -05:00
|
|
|
\ >string { sbuf } set-specializer
|
2008-08-22 18:38:23 -04:00
|
|
|
|
2013-03-04 20:34:20 -05:00
|
|
|
\ >array { { vector } } set-specializer
|
2008-08-22 18:38:23 -04:00
|
|
|
|
2013-03-04 20:34:20 -05:00
|
|
|
\ >vector { { array } { vector } } set-specializer
|
2008-08-22 18:38:23 -04:00
|
|
|
|
2013-03-04 20:34:20 -05:00
|
|
|
\ >sbuf { string } set-specializer
|
2008-08-22 18:38:23 -04:00
|
|
|
|
2013-03-27 19:21:32 -04:00
|
|
|
\ split { string string } set-specializer
|
2008-08-22 18:38:23 -04:00
|
|
|
|
2013-03-18 16:35:22 -04:00
|
|
|
\ member? { { array } { string } } set-specializer
|
|
|
|
|
|
|
|
\ member-eq? { array } set-specializer
|
2008-08-22 18:38:23 -04:00
|
|
|
|
2013-03-04 20:34:20 -05:00
|
|
|
\ assoc-stack { vector } set-specializer
|
2008-08-22 18:38:23 -04:00
|
|
|
|
2012-09-11 13:34:50 -04:00
|
|
|
{ >le >be } [
|
|
|
|
{ { fixnum fixnum } { bignum fixnum } }
|
2013-03-04 20:34:20 -05:00
|
|
|
set-specializer
|
2012-09-11 13:34:50 -04:00
|
|
|
] each
|
2008-12-17 15:57:24 -05:00
|
|
|
|
2013-03-04 20:34:20 -05:00
|
|
|
\ base> { string fixnum } set-specializer
|
|
|
|
|
|
|
|
M\ hashtable at*
|
|
|
|
{ { fixnum object } { word object } }
|
|
|
|
set-specializer
|
2009-04-11 21:30:51 -04:00
|
|
|
|
2013-03-04 20:34:20 -05:00
|
|
|
M\ hashtable set-at
|
|
|
|
{ { object fixnum object } { object word object } }
|
|
|
|
set-specializer
|
2008-12-17 15:57:24 -05:00
|
|
|
|
2013-03-04 20:34:20 -05:00
|
|
|
\ encode-string { string object object } set-specializer
|