2005-01-29 16:39:30 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2004-12-13 00:13:54 -05:00
|
|
|
IN: generic
|
2005-01-29 16:39:30 -05:00
|
|
|
USING: errors hashtables kernel kernel-internals lists
|
2005-04-25 19:54:21 -04:00
|
|
|
namespaces parser sequences strings words vectors math
|
|
|
|
math-internals ;
|
2004-12-13 00:13:54 -05:00
|
|
|
|
|
|
|
! A simple single-dispatch generic word system.
|
|
|
|
|
2005-05-16 01:15:48 -04:00
|
|
|
: predicate-word ( word -- word )
|
2005-07-28 18:20:31 -04:00
|
|
|
word-name "?" append create-in ;
|
|
|
|
|
|
|
|
: register-predicate ( class predicate -- )
|
|
|
|
2dup unit "predicate" set-word-prop
|
|
|
|
swap "predicating" set-word-prop ;
|
|
|
|
|
2005-06-12 20:55:30 -04:00
|
|
|
GENERIC: delegate
|
|
|
|
GENERIC: set-delegate
|
|
|
|
|
|
|
|
M: object delegate drop f ;
|
|
|
|
|
2004-12-18 23:18:32 -05:00
|
|
|
! Metaclasses have priority -- this induces an order in which
|
|
|
|
! methods are added to the vtable.
|
2004-12-13 00:13:54 -05:00
|
|
|
|
|
|
|
: metaclass ( class -- metaclass )
|
2005-03-05 14:45:23 -05:00
|
|
|
"metaclass" word-prop ;
|
2004-12-13 00:13:54 -05:00
|
|
|
|
|
|
|
: builtin-supertypes ( class -- list )
|
|
|
|
#! A list of builtin supertypes of the class.
|
2005-03-05 14:45:23 -05:00
|
|
|
dup metaclass "builtin-supertypes" word-prop call ;
|
2004-12-13 00:13:54 -05:00
|
|
|
|
2004-12-18 23:18:32 -05:00
|
|
|
: set-vtable ( definition class vtable -- )
|
2005-04-26 00:35:55 -04:00
|
|
|
>r "builtin-type" word-prop r> set-nth ;
|
2004-12-13 00:13:54 -05:00
|
|
|
|
2005-03-05 14:45:23 -05:00
|
|
|
: class-ord ( class -- n ) metaclass "priority" word-prop ;
|
2004-12-18 23:18:32 -05:00
|
|
|
|
|
|
|
: class< ( cls1 cls2 -- ? )
|
2005-01-13 14:41:08 -05:00
|
|
|
#! Test if class1 is a subclass of class2.
|
2004-12-29 18:01:23 -05:00
|
|
|
over metaclass over metaclass = [
|
2005-03-05 14:45:23 -05:00
|
|
|
dup metaclass "class<" word-prop call
|
2004-12-29 18:01:23 -05:00
|
|
|
] [
|
|
|
|
swap class-ord swap class-ord <
|
|
|
|
] ifte ;
|
2004-12-13 00:13:54 -05:00
|
|
|
|
2004-12-29 18:01:23 -05:00
|
|
|
: methods ( generic -- alist )
|
2005-03-05 14:45:23 -05:00
|
|
|
"methods" word-prop hash>alist [ 2car class< ] sort ;
|
2004-12-18 23:18:32 -05:00
|
|
|
|
2005-04-24 20:57:37 -04:00
|
|
|
: order ( generic -- list )
|
|
|
|
"methods" word-prop hash-keys [ class< ] sort ;
|
|
|
|
|
2004-12-23 23:55:22 -05:00
|
|
|
: add-method ( generic vtable definition class -- )
|
2004-12-18 23:18:32 -05:00
|
|
|
#! Add the method entry to the vtable. Unlike define-method,
|
|
|
|
#! this is called at vtable build time, and in the sorted
|
|
|
|
#! order.
|
2005-03-05 14:45:23 -05:00
|
|
|
dup metaclass "add-method" word-prop [
|
2005-01-23 21:31:32 -05:00
|
|
|
[ "Metaclass is missing add-method" throw ]
|
|
|
|
] unless* call ;
|
2004-12-18 23:18:32 -05:00
|
|
|
|
2005-05-14 21:15:50 -04:00
|
|
|
: picker% "picker" word-prop % ;
|
|
|
|
|
|
|
|
: dispatcher% "dispatcher" word-prop % ;
|
|
|
|
|
2005-05-23 00:25:52 -04:00
|
|
|
: error-method ( generic -- method )
|
2005-05-23 01:18:51 -04:00
|
|
|
[ dup picker% literal, \ no-method , ] make-list ;
|
2005-05-23 00:25:52 -04:00
|
|
|
|
2005-05-14 21:15:50 -04:00
|
|
|
: empty-method ( generic -- method )
|
2005-05-23 00:25:52 -04:00
|
|
|
dup "picker" word-prop [ dup ] = [
|
|
|
|
[
|
|
|
|
[ dup delegate ] %
|
|
|
|
[ dup , ] make-list ,
|
|
|
|
error-method ,
|
|
|
|
\ ?ifte ,
|
|
|
|
] make-list
|
|
|
|
] [
|
|
|
|
error-method
|
|
|
|
] ifte ;
|
2005-05-14 21:15:50 -04:00
|
|
|
|
2005-01-23 21:31:32 -05:00
|
|
|
: <empty-vtable> ( generic -- vtable )
|
2005-05-14 21:15:50 -04:00
|
|
|
empty-method num-types swap <repeated> >vector ;
|
2004-12-13 00:13:54 -05:00
|
|
|
|
2004-12-29 18:01:23 -05:00
|
|
|
: <vtable> ( generic -- vtable )
|
2005-01-23 21:31:32 -05:00
|
|
|
dup <empty-vtable> over methods [
|
2004-12-29 18:01:23 -05:00
|
|
|
( generic vtable method )
|
2004-12-23 23:55:22 -05:00
|
|
|
>r 2dup r> unswons add-method
|
|
|
|
] each nip ;
|
2004-12-13 00:13:54 -05:00
|
|
|
|
2005-05-14 21:15:50 -04:00
|
|
|
: (small-generic) ( word methods -- quot )
|
|
|
|
[
|
|
|
|
2dup cdr (small-generic) [
|
|
|
|
>r >r picker%
|
|
|
|
r> car unswons "predicate" word-prop %
|
|
|
|
, r> , \ ifte ,
|
|
|
|
] make-list
|
|
|
|
] [
|
|
|
|
empty-method
|
|
|
|
] ifte* ;
|
|
|
|
|
|
|
|
: small-generic ( word -- def )
|
|
|
|
dup methods reverse (small-generic) ;
|
|
|
|
|
|
|
|
: big-generic ( word -- def )
|
2005-05-14 17:18:45 -04:00
|
|
|
[
|
2005-05-14 21:15:50 -04:00
|
|
|
dup picker%
|
|
|
|
dup dispatcher%
|
|
|
|
<vtable> ,
|
2005-05-14 17:18:45 -04:00
|
|
|
\ dispatch ,
|
2005-05-14 21:15:50 -04:00
|
|
|
] make-list ;
|
|
|
|
|
|
|
|
: small-generic? ( word -- ? )
|
|
|
|
dup "methods" word-prop hash-size 3 <=
|
|
|
|
swap "dispatcher" word-prop [ type ] = and ;
|
|
|
|
|
|
|
|
: make-generic ( word -- )
|
|
|
|
dup dup small-generic? [
|
|
|
|
small-generic
|
|
|
|
] [
|
|
|
|
big-generic
|
|
|
|
] ifte (define-compound) ;
|
2004-12-18 23:18:32 -05:00
|
|
|
|
2005-01-29 16:39:30 -05:00
|
|
|
: define-method ( class generic definition -- )
|
|
|
|
-rot
|
2005-04-25 19:56:56 -04:00
|
|
|
over metaclass word? [
|
2005-05-06 19:49:07 -04:00
|
|
|
over word-name " is not a class" append throw
|
2005-04-25 19:56:56 -04:00
|
|
|
] unless
|
2005-05-03 23:50:04 -04:00
|
|
|
[ "methods" word-prop set-hash ] keep make-generic ;
|
2004-12-18 23:18:32 -05:00
|
|
|
|
2004-12-29 18:01:23 -05:00
|
|
|
: init-methods ( word -- )
|
2005-03-05 14:45:23 -05:00
|
|
|
dup "methods" word-prop [
|
2004-12-29 18:01:23 -05:00
|
|
|
drop
|
|
|
|
] [
|
2005-03-05 14:45:23 -05:00
|
|
|
<namespace> "methods" set-word-prop
|
2004-12-29 18:01:23 -05:00
|
|
|
] ifte ;
|
|
|
|
|
2004-12-13 00:13:54 -05:00
|
|
|
! Defining generic words
|
2005-05-14 17:18:45 -04:00
|
|
|
: define-generic* ( picker dispatcher word -- )
|
|
|
|
[ swap "dispatcher" set-word-prop ] keep
|
|
|
|
[ swap "picker" set-word-prop ] keep
|
2005-05-03 23:50:04 -04:00
|
|
|
dup init-methods make-generic ;
|
2004-12-18 23:18:32 -05:00
|
|
|
|
2005-05-14 17:18:45 -04:00
|
|
|
: define-generic ( word -- )
|
|
|
|
>r [ dup ] [ type ] r> define-generic* ;
|
2004-12-18 23:18:32 -05:00
|
|
|
|
2005-02-24 20:52:17 -05:00
|
|
|
PREDICATE: compound generic ( word -- ? )
|
2005-05-14 21:15:50 -04:00
|
|
|
"dispatcher" word-prop ;
|
2004-12-18 23:18:32 -05:00
|
|
|
|
2005-05-14 21:15:50 -04:00
|
|
|
M: generic definer drop \ G: ;
|
2005-02-24 20:52:17 -05:00
|
|
|
|
2004-12-23 02:14:40 -05:00
|
|
|
! Maps lists of builtin type numbers to class objects.
|
2005-04-10 18:58:30 -04:00
|
|
|
SYMBOL: typemap
|
2004-12-23 02:14:40 -05:00
|
|
|
|
|
|
|
SYMBOL: object
|
|
|
|
|
2004-12-27 22:58:43 -05:00
|
|
|
: lookup-union ( typelist -- class )
|
2005-04-10 18:58:30 -04:00
|
|
|
[ > ] sort typemap get hash [ object ] unless* ;
|
2004-12-27 22:58:43 -05:00
|
|
|
|
2004-12-23 16:37:16 -05:00
|
|
|
: class-or ( class class -- class )
|
2004-12-23 02:14:40 -05:00
|
|
|
#! Return a class that both classes are subclasses of.
|
|
|
|
swap builtin-supertypes
|
|
|
|
swap builtin-supertypes
|
2005-07-16 23:01:51 -04:00
|
|
|
seq-union lookup-union ;
|
2004-12-23 02:14:40 -05:00
|
|
|
|
2004-12-31 02:17:45 -05:00
|
|
|
: class-or-list ( list -- class )
|
|
|
|
#! Return a class that every class in the list is a
|
|
|
|
#! subclass of.
|
|
|
|
[
|
|
|
|
[ builtin-supertypes [ unique, ] each ] each
|
|
|
|
] make-list lookup-union ;
|
|
|
|
|
2004-12-23 16:37:16 -05:00
|
|
|
: class-and ( class class -- class )
|
2005-03-07 22:11:36 -05:00
|
|
|
#! Return a class that is a subclass of both, or null in
|
|
|
|
#! the degenerate case.
|
2005-01-16 17:58:28 -05:00
|
|
|
swap builtin-supertypes swap builtin-supertypes
|
2005-07-16 22:16:18 -04:00
|
|
|
seq-intersect lookup-union ;
|
2004-12-23 02:14:40 -05:00
|
|
|
|
|
|
|
: define-class ( class metaclass -- )
|
2005-03-05 14:45:23 -05:00
|
|
|
dupd "metaclass" set-word-prop
|
2004-12-23 02:14:40 -05:00
|
|
|
dup builtin-supertypes [ > ] sort
|
2005-04-10 18:58:30 -04:00
|
|
|
typemap get set-hash ;
|
2004-12-23 02:14:40 -05:00
|
|
|
|
2005-04-10 18:58:30 -04:00
|
|
|
typemap get [ <namespace> typemap set ] unless
|