| 
									
										
										
										
											2008-07-12 02:08:30 -04:00
										 |  |  | ! Copyright (C) 2008 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  | USING: parser words definitions kernel sequences assocs arrays | 
					
						
							|  |  |  | kernel.private fry combinators accessors vectors strings sbufs | 
					
						
							|  |  |  | byte-arrays byte-vectors io.binary io.streams.string splitting | 
					
						
							| 
									
										
										
										
											2008-12-17 15:57:24 -05:00
										 |  |  | math generic generic.standard generic.standard.engines classes | 
					
						
							|  |  |  | hashtables ;
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-29 13:03:56 -05:00
										 |  |  | M: class specializer-predicate "predicate" word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: object specializer-predicate '[ _ eq? ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: specializer-declaration ( spec -- class )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: class specializer-declaration ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: object specializer-declaration class ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : make-specializer ( specs -- quot )
 | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  |     dup length <reversed>
 | 
					
						
							|  |  |  |     [ (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
 | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  |         unclip [ swap [ f ] \ if 3array append [ ] like ] reduce
 | 
					
						
							| 
									
										
										
										
											2008-09-06 20:13:59 -04:00
										 |  |  |     ] if-empty ;
 | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : specializer-cases ( quot word -- default alist )
 | 
					
						
							|  |  |  |     dup [ array? ] all? [ 1array ] unless [ | 
					
						
							|  |  |  |         [ make-specializer ] keep
 | 
					
						
							| 
									
										
										
										
											2008-11-29 13:03:56 -05:00
										 |  |  |         [ specializer-declaration ] map '[ _ declare ] pick append
 | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  |     ] { } map>assoc ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : method-declaration ( method -- quot )
 | 
					
						
							|  |  |  |     [ "method-generic" word-prop dispatch# object <array> ] | 
					
						
							|  |  |  |     [ "method-class" word-prop ] | 
					
						
							|  |  |  |     bi prefix ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : specialize-method ( quot method -- quot' )
 | 
					
						
							| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  |     method-declaration '[ _ declare ] prepend ;
 | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : specialize-quot ( quot specializer -- quot' )
 | 
					
						
							|  |  |  |     specializer-cases alist>quot ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : standard-method? ( method -- ? )
 | 
					
						
							|  |  |  |     dup method-body? [ | 
					
						
							|  |  |  |         "method-generic" word-prop standard-generic? | 
					
						
							|  |  |  |     ] [ drop f ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : specialized-def ( word -- quot )
 | 
					
						
							| 
									
										
										
										
											2008-12-17 15:57:24 -05:00
										 |  |  |     [ def>> ] keep
 | 
					
						
							|  |  |  |     [ dup standard-method? [ specialize-method ] [ drop ] if ] | 
					
						
							|  |  |  |     [ "specializer" word-prop [ specialize-quot ] when* ] | 
					
						
							|  |  |  |     bi ;
 | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : specialized-length ( specializer -- n )
 | 
					
						
							|  |  |  |     dup [ array? ] all? [ first ] when length ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-12 02:08:30 -04:00
										 |  |  | : HINTS: | 
					
						
							| 
									
										
										
										
											2008-09-13 04:12:52 -04:00
										 |  |  |     scan-object | 
					
						
							|  |  |  |     dup method-spec? [ first2 method ] when
 | 
					
						
							| 
									
										
										
										
											2008-08-31 02:34:00 -04:00
										 |  |  |     [ redefined ] | 
					
						
							| 
									
										
										
										
											2008-07-12 02:08:30 -04:00
										 |  |  |     [ parse-definition "specializer" set-word-prop ] bi ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     parsing | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Default specializers | 
					
						
							|  |  |  | { first first2 first3 first4 } | 
					
						
							|  |  |  | [ { array } "specializer" set-word-prop ] each
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-06 01:02:44 -05:00
										 |  |  | { peek pop* pop } [ | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  |     { vector } "specializer" set-word-prop | 
					
						
							|  |  |  | ] each
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-06 01:02:44 -05:00
										 |  |  | \ push { { vector } { sbuf } } "specializer" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  | \ push-all | 
					
						
							|  |  |  | { { string sbuf } { array vector } { byte-array byte-vector } } | 
					
						
							|  |  |  | "specializer" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ append | 
					
						
							|  |  |  | { { string string } { array array } } | 
					
						
							|  |  |  | "specializer" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ subseq | 
					
						
							|  |  |  | { { fixnum fixnum string } { fixnum fixnum array } } | 
					
						
							|  |  |  | "specializer" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ reverse-here | 
					
						
							|  |  |  | { { string } { array } } | 
					
						
							|  |  |  | "specializer" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ mismatch | 
					
						
							|  |  |  | { string string } | 
					
						
							|  |  |  | "specializer" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ >string { sbuf } "specializer" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-12 09:18:44 -04:00
										 |  |  | \ >array { { vector } } "specializer" set-word-prop | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | \ >vector { { array } { vector } } "specializer" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ >sbuf { string } "specializer" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ split, { string string } "specializer" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ memq? { array } "specializer" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-12 09:18:44 -04:00
										 |  |  | \ member? { array } "specializer" set-word-prop | 
					
						
							| 
									
										
										
										
											2008-08-22 18:38:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | \ assoc-stack { vector } "specializer" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ >le { { fixnum fixnum } { bignum fixnum } } "specializer" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ >be { { bignum fixnum } { fixnum fixnum } } "specializer" set-word-prop | 
					
						
							| 
									
										
										
										
											2008-12-17 15:57:24 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | \ hashtable \ at* method { { fixnum hashtable } { word hashtable } } "specializer" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ hashtable \ set-at method { { object fixnum object } { object word object } } "specializer" set-word-prop |