2005-08-15 03:25:39 -04:00
|
|
|
IN: generic
|
|
|
|
USING: errors hashtables kernel kernel-internals lists math
|
|
|
|
namespaces sequences vectors words ;
|
|
|
|
|
2005-08-22 14:29:43 -04:00
|
|
|
: error-method ( picker word -- method )
|
|
|
|
[ swap % literalize , \ no-method , ] make-list ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
2005-08-22 14:29:43 -04:00
|
|
|
: empty-method ( picker word -- method )
|
|
|
|
over [ dup ] = [
|
2005-08-15 03:25:39 -04:00
|
|
|
[
|
2005-08-22 14:29:43 -04:00
|
|
|
[ dup delegate ] % dup unit , error-method , \ ?ifte ,
|
2005-08-15 03:25:39 -04:00
|
|
|
] make-list
|
|
|
|
] [
|
|
|
|
error-method
|
|
|
|
] ifte ;
|
|
|
|
|
2005-08-22 14:29:43 -04:00
|
|
|
: class-predicates ( picker assoc -- assoc )
|
|
|
|
[ uncons >r "predicate" word-prop append r> cons ] map-with ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
2005-08-16 15:53:30 -04:00
|
|
|
: alist>quot ( default alist -- quot )
|
|
|
|
[ unswons [ % , , \ ifte , ] make-list ] each ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
2005-08-16 15:53:30 -04:00
|
|
|
: sort-methods ( assoc -- vtable )
|
|
|
|
#! Input is a predicate -> method association.
|
|
|
|
num-types [
|
|
|
|
type>class dup
|
|
|
|
[ swap [ car classes-intersect? ] subset-with ]
|
|
|
|
[ 2drop f ] ifte
|
|
|
|
] map-with ;
|
|
|
|
|
2005-08-22 14:29:43 -04:00
|
|
|
: <vtable> ( picker word -- vtable )
|
|
|
|
2dup methods sort-methods [ class-predicates ] map-with
|
2005-08-16 15:53:30 -04:00
|
|
|
>r empty-method r> [ alist>quot ] map-with ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
2005-08-22 14:29:43 -04:00
|
|
|
: small-generic ( picker word -- def )
|
|
|
|
2dup methods class-predicates >r empty-method r> alist>quot ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
2005-08-22 14:29:43 -04:00
|
|
|
: big-generic ( picker word -- def )
|
|
|
|
[ over % \ type , <vtable> , \ dispatch , ] make-list ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
|
|
|
: small-generic? ( word -- ? )
|
|
|
|
"methods" word-prop hash-size 3 <= ;
|
|
|
|
|
2005-08-22 14:29:43 -04:00
|
|
|
: standard-combination ( word picker -- quot )
|
|
|
|
swap dup small-generic?
|
|
|
|
[ small-generic ] [ big-generic ] ifte ;
|
|
|
|
|
|
|
|
: simple-combination ( word -- quot )
|
|
|
|
[ dup ] standard-combination ;
|
2005-08-15 03:25:39 -04:00
|
|
|
|
|
|
|
: define-generic ( word -- )
|
2005-08-22 14:29:43 -04:00
|
|
|
[ simple-combination ] define-generic* ;
|
|
|
|
|
|
|
|
PREDICATE: generic simple-generic ( word -- ? )
|
|
|
|
"combination" word-prop [ simple-combination ] = ;
|