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
|
|
|
|
namespaces parser strings words vectors math math-internals ;
|
2004-12-13 00:13:54 -05:00
|
|
|
|
|
|
|
! A simple single-dispatch generic word system.
|
|
|
|
|
|
|
|
: predicate-word ( word -- word )
|
|
|
|
word-name "?" cat2 "in" get create ;
|
|
|
|
|
|
|
|
! Terminology:
|
|
|
|
! - type: a datatype built in to the runtime, eg fixnum, word
|
|
|
|
! cons. All objects have exactly one type, new types cannot be
|
|
|
|
! defined, and types are disjoint.
|
|
|
|
! - class: a user defined way of differentiating objects, either
|
|
|
|
! based on type, or some combination of type, predicate, or
|
|
|
|
! method map.
|
|
|
|
! - metaclass: a metaclass is a symbol with a handful of word
|
2005-01-29 16:39:30 -05:00
|
|
|
! properties: "builtin-types" "priority"
|
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 )
|
|
|
|
"metaclass" word-property ;
|
|
|
|
|
|
|
|
: builtin-supertypes ( class -- list )
|
|
|
|
#! A list of builtin supertypes of the class.
|
|
|
|
dup metaclass "builtin-supertypes" word-property call ;
|
|
|
|
|
2004-12-18 23:18:32 -05:00
|
|
|
: set-vtable ( definition class vtable -- )
|
2004-12-13 00:13:54 -05:00
|
|
|
>r "builtin-type" word-property r> set-vector-nth ;
|
|
|
|
|
2004-12-18 23:18:32 -05:00
|
|
|
: class-ord ( class -- n ) metaclass "priority" word-property ;
|
|
|
|
|
|
|
|
: 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 = [
|
|
|
|
dup metaclass "class<" word-property call
|
|
|
|
] [
|
|
|
|
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 )
|
|
|
|
"methods" word-property hash>alist [ 2car class< ] sort ;
|
2004-12-18 23:18:32 -05:00
|
|
|
|
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-01-23 21:31:32 -05:00
|
|
|
dup metaclass "add-method" word-property [
|
|
|
|
[ "Metaclass is missing add-method" throw ]
|
|
|
|
] unless* call ;
|
2004-12-18 23:18:32 -05:00
|
|
|
|
2005-01-23 21:31:32 -05:00
|
|
|
: <empty-vtable> ( generic -- vtable )
|
|
|
|
unit num-types
|
|
|
|
[ drop dup [ car undefined-method ] cons ] vector-project
|
|
|
|
nip ;
|
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-01-29 16:39:30 -05:00
|
|
|
: make-generic ( word vtable -- )
|
2004-12-18 23:18:32 -05:00
|
|
|
over "combination" word-property cons define-compound ;
|
|
|
|
|
2005-01-29 16:39:30 -05:00
|
|
|
: define-method ( class generic definition -- )
|
|
|
|
-rot
|
2004-12-29 18:01:23 -05:00
|
|
|
[ "methods" word-property set-hash ] keep dup <vtable>
|
2005-01-29 16:39:30 -05:00
|
|
|
make-generic ;
|
2004-12-18 23:18:32 -05:00
|
|
|
|
2004-12-29 18:01:23 -05:00
|
|
|
: init-methods ( word -- )
|
|
|
|
dup "methods" word-property [
|
|
|
|
drop
|
|
|
|
] [
|
|
|
|
<namespace> "methods" set-word-property
|
|
|
|
] ifte ;
|
|
|
|
|
2004-12-13 00:13:54 -05:00
|
|
|
! Defining generic words
|
2005-01-29 16:39:30 -05:00
|
|
|
: define-generic ( combination definer word -- )
|
2004-12-18 23:18:32 -05:00
|
|
|
#! Takes a combination parameter. A combination is a
|
|
|
|
#! quotation that takes some objects and a vtable from the
|
|
|
|
#! stack, and calls the appropriate row of the vtable.
|
2004-12-29 18:01:23 -05:00
|
|
|
[ swap "definer" set-word-property ] keep
|
|
|
|
[ swap "combination" set-word-property ] keep
|
|
|
|
dup init-methods
|
2005-01-29 16:39:30 -05:00
|
|
|
dup <vtable> make-generic ;
|
2004-12-18 23:18:32 -05:00
|
|
|
|
|
|
|
: single-combination ( obj vtable -- )
|
|
|
|
>r dup type r> dispatch ; inline
|
|
|
|
|
2004-12-13 00:13:54 -05:00
|
|
|
: GENERIC:
|
2004-12-18 23:18:32 -05:00
|
|
|
#! GENERIC: bar creates a generic word bar. Add methods to
|
|
|
|
#! the generic word using M:.
|
2005-01-29 16:39:30 -05:00
|
|
|
[ single-combination ]
|
|
|
|
\ GENERIC: CREATE define-generic ; parsing
|
2004-12-18 23:18:32 -05:00
|
|
|
|
|
|
|
: arithmetic-combination ( n n vtable -- )
|
|
|
|
#! Note that the numbers remain on the stack, possibly after
|
|
|
|
#! being coerced to a maximal type.
|
|
|
|
>r arithmetic-type r> dispatch ; inline
|
|
|
|
|
|
|
|
: 2GENERIC:
|
|
|
|
#! 2GENERIC: bar creates a generic word bar. Add methods to
|
|
|
|
#! the generic word using M:. 2GENERIC words dispatch on
|
|
|
|
#! arithmetic types and should not be used for non-numerical
|
|
|
|
#! types.
|
2005-01-29 16:39:30 -05:00
|
|
|
[ arithmetic-combination ]
|
|
|
|
\ 2GENERIC: CREATE define-generic ; parsing
|
2004-12-13 00:13:54 -05:00
|
|
|
|
|
|
|
: M: ( -- class generic [ ] )
|
|
|
|
#! M: foo bar begins a definition of the bar generic word
|
|
|
|
#! specialized to the foo type.
|
2005-01-29 16:39:30 -05:00
|
|
|
scan-word scan-word [ define-method ] [ ] ; parsing
|
2004-12-23 02:14:40 -05:00
|
|
|
|
|
|
|
! Maps lists of builtin type numbers to class objects.
|
|
|
|
SYMBOL: classes
|
|
|
|
|
|
|
|
SYMBOL: object
|
|
|
|
|
|
|
|
: type-union ( list list -- list )
|
2004-12-31 02:17:45 -05:00
|
|
|
append prune ;
|
2004-12-23 18:46:21 -05:00
|
|
|
|
2004-12-27 22:58:43 -05:00
|
|
|
: lookup-union ( typelist -- class )
|
2004-12-31 02:17:45 -05:00
|
|
|
[ > ] sort classes 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
|
2004-12-27 22:58:43 -05:00
|
|
|
type-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 )
|
2004-12-23 02:14:40 -05:00
|
|
|
#! Return a class that is a subclass of both, or raise an
|
|
|
|
#! error if this is impossible.
|
2005-01-16 17:58:28 -05:00
|
|
|
swap builtin-supertypes swap builtin-supertypes
|
|
|
|
intersection lookup-union ;
|
2004-12-23 02:14:40 -05:00
|
|
|
|
2004-12-31 02:17:45 -05:00
|
|
|
: define-promise ( class -- )
|
|
|
|
#! A promise is a word that has no effect during
|
|
|
|
#! interpretation, but instructs the compiler that the value
|
|
|
|
#! at the top of the stack is statically-known to be of the
|
|
|
|
#! given type. Promises should only be used by kernel code.
|
2004-12-31 18:51:34 -05:00
|
|
|
dup word-name "%" swap cat2 "kernel-internals" create
|
2004-12-31 02:17:45 -05:00
|
|
|
dup [ ] define-compound
|
|
|
|
swap "promise" set-word-property ;
|
|
|
|
|
2004-12-23 02:14:40 -05:00
|
|
|
: define-class ( class metaclass -- )
|
|
|
|
dupd "metaclass" set-word-property
|
2004-12-31 02:17:45 -05:00
|
|
|
dup define-promise
|
2004-12-23 02:14:40 -05:00
|
|
|
dup builtin-supertypes [ > ] sort
|
|
|
|
classes get set-hash ;
|
|
|
|
|
2005-01-01 17:20:48 -05:00
|
|
|
classes get [ <namespace> classes set ] unless
|
2005-01-29 16:39:30 -05:00
|
|
|
|
|
|
|
GENERIC: class ( obj -- class )
|