unified method map

cvs
Slava Pestov 2005-01-01 23:02:23 +00:00
parent 911b614351
commit aa128f7257
3 changed files with 21 additions and 44 deletions

View File

@ -8,7 +8,6 @@
- make see work with union, builtin, predicate - make see work with union, builtin, predicate
- doc comments of generics - doc comments of generics
- redo traits with generic method map
+ ffi: + ffi:

View File

@ -38,70 +38,46 @@ USE: vectors
! Traits metaclass for user-defined classes based on hashtables ! Traits metaclass for user-defined classes based on hashtables
! Hashtable slot holding a selector->method map. : traits ( object -- symbol ) \ traits swap hash ;
SYMBOL: traits
: traits-map ( class -- hash )
#! The method map word property maps selector words to
#! definitions.
"traits-map" word-property ;
! Hashtable slot holding an optional delegate. Any undefined ! Hashtable slot holding an optional delegate. Any undefined
! methods are called on the delegate. The object can also ! methods are called on the delegate. The object can also
! manually pass any methods on to the delegate. ! manually pass any methods on to the delegate.
SYMBOL: delegate SYMBOL: delegate
: object-map ( obj -- hash ) : traits-dispatch ( object selector -- object quot )
#! Get the method map for an object. over traits over "methods" word-property hash* dup [
#! We will use hashtable? here when its a first-class type. nip cdr ( method is defined )
dup vector? [ traits swap hash ] [ drop f ] ifte ;
: traits-dispatch ( selector traits -- traits quot )
#! Look up the method with the traits object on the stack.
#! Returns the traits to call the method on; either the
#! original object, or one of the delegates.
2dup object-map hash* dup [
rot drop cdr ( method is defined )
] [ ] [
drop delegate swap hash* dup [ drop delegate rot hash [
cdr traits-dispatch ( check delegate ) swap traits-dispatch ( check delegate )
] [ ] [
drop [ undefined-method ] ( no delegate ) [ undefined-method ] ( no delegate )
] ifte ] ifte*
] ifte ; ] ifte ;
: add-traits-dispatch ( word vtable -- ) : add-traits-dispatch ( word vtable -- )
>r unit [ car swap traits-dispatch call ] cons \ vector r> >r unit [ car traits-dispatch call ] cons \ vector r>
set-vtable ; set-vtable ;
traits [ \ traits [
( generic vtable definition class -- ) ( generic vtable definition class -- )
2drop add-traits-dispatch 2drop add-traits-dispatch
] "add-method" set-word-property ] "add-method" set-word-property
traits [ \ traits [
( class generic quotation )
3dup -rot (define-method)
over dup word-parameter car add-traits-dispatch
swap rot traits-map set-hash
] "define-method" set-word-property
traits [
drop vector "builtin-type" word-property unit drop vector "builtin-type" word-property unit
] "builtin-supertypes" set-word-property ] "builtin-supertypes" set-word-property
traits 10 "priority" set-word-property \ traits 10 "priority" set-word-property
traits [ 2drop t ] "class<" set-word-property \ traits [ 2drop t ] "class<" set-word-property
: init-traits-map ( word -- )
<namespace> "traits-map" set-word-property ;
: traits-predicate ( word -- ) : traits-predicate ( word -- )
#! foo? where foo is a traits type tests if the top of stack #! foo? where foo is a traits type tests if the top of stack
#! is of this type. #! is of this type.
dup predicate-word swap dup predicate-word swap
traits-map [ swap object-map eq? ] cons [ swap traits eq? ] cons
define-compound ; define-compound ;
: TRAITS: : TRAITS:
@ -109,8 +85,7 @@ traits [ 2drop t ] "class<" set-word-property
#! created with <foo>, and tested with foo?. #! created with <foo>, and tested with foo?.
CREATE CREATE
dup define-symbol dup define-symbol
dup init-traits-map dup \ traits "metaclass" set-word-property
dup traits "metaclass" set-word-property
traits-predicate ; parsing traits-predicate ; parsing
: constructor-word ( word -- word ) : constructor-word ( word -- word )
@ -118,7 +93,7 @@ traits [ 2drop t ] "class<" set-word-property
: define-constructor ( constructor traits definition -- ) : define-constructor ( constructor traits definition -- )
>r >r
traits-map [ traits pick set-hash ] cons \ <namespace> swons [ \ traits pick set-hash ] cons \ <namespace> swons
r> append define-compound ; r> append define-compound ;
: C: ( -- constructor traits [ ] ) : C: ( -- constructor traits [ ] )

View File

@ -192,13 +192,16 @@ SYMBOL: meta-cf
: not-done ( quot -- ) : not-done ( quot -- )
done? [ "Stepper is done." print drop ] [ call ] ifte ; done? [ "Stepper is done." print drop ] [ call ] ifte ;
: next-report ( -- obj )
next dup report meta-cf get report ;
: step : step
#! Step into current word. #! Step into current word.
[ meta-cf get . next do-1 ] not-done ; [ next-report do-1 ] not-done ;
: into : into
#! Step into current word. #! Step into current word.
[ meta-cf get . next do ] not-done ; [ next-report do ] not-done ;
: walk-banner ( -- ) : walk-banner ( -- )
"The following words control the single-stepper:" print "The following words control the single-stepper:" print