2005-01-29 16:39:30 -05:00
|
|
|
! Copyright (C) 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
|
|
|
IN: generic
|
|
|
|
USING: words parser kernel namespaces lists strings
|
2005-02-08 22:02:44 -05:00
|
|
|
kernel-internals math hashtables errors vectors ;
|
2005-01-29 16:39:30 -05:00
|
|
|
|
2005-02-05 22:51:41 -05:00
|
|
|
: make-tuple ( class -- tuple )
|
2005-01-29 16:39:30 -05:00
|
|
|
dup "tuple-size" word-property <tuple>
|
|
|
|
[ 0 swap set-array-nth ] keep ;
|
|
|
|
|
2005-02-05 22:51:41 -05:00
|
|
|
: (literal-tuple) ( list size -- tuple )
|
|
|
|
dup <tuple> swap [
|
|
|
|
( list tuple n -- list tuple n )
|
|
|
|
pick car pick pick swap set-array-nth
|
|
|
|
>r >r cdr r> r>
|
|
|
|
] repeat nip ;
|
|
|
|
|
|
|
|
: literal-tuple ( list -- tuple )
|
|
|
|
dup car "tuple-size" word-property over length over = [
|
|
|
|
(literal-tuple)
|
|
|
|
] [
|
|
|
|
"Incorrect tuple length" throw
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: tuple>list ( tuple -- list )
|
|
|
|
>tuple array>list ;
|
|
|
|
|
2005-01-29 16:39:30 -05:00
|
|
|
: define-tuple-generic ( tuple word def -- )
|
2005-01-30 15:57:25 -05:00
|
|
|
over >r [ single-combination ] \ GENERIC: r> define-generic
|
2005-01-29 16:39:30 -05:00
|
|
|
define-method ;
|
|
|
|
|
2005-02-09 22:35:11 -05:00
|
|
|
: accessor-word ( name tuple -- word )
|
|
|
|
[ word-name , "-" , , ] make-string
|
|
|
|
create-in ;
|
|
|
|
|
|
|
|
: define-accessor ( tuple name n -- accessor )
|
|
|
|
#! Generic word with a method specializing on the tuple's
|
|
|
|
#! class that reads the right slot.
|
2005-02-10 15:14:20 -05:00
|
|
|
>r over accessor-word tuck r> [ slot ] cons
|
2005-02-09 22:35:11 -05:00
|
|
|
define-tuple-generic ;
|
|
|
|
|
|
|
|
: mutator-word ( name tuple -- word )
|
|
|
|
[ "set-" , word-name , "-" , , ] make-string
|
|
|
|
create-in ;
|
|
|
|
|
|
|
|
: define-mutator ( word name n -- mutator )
|
|
|
|
#! Generic word with a method specializing on the tuple's
|
|
|
|
#! class that writes to the right slot.
|
2005-02-10 15:14:20 -05:00
|
|
|
>r over mutator-word tuck r> [ set-slot ] cons
|
2005-02-09 22:35:11 -05:00
|
|
|
define-tuple-generic ;
|
|
|
|
|
2005-02-17 19:01:11 -05:00
|
|
|
: define-slot ( word name n -- [ n accessor mutator ] )
|
2005-01-30 15:57:25 -05:00
|
|
|
over "delegate" = [
|
|
|
|
pick over "delegate-field" set-word-property
|
|
|
|
] when
|
2005-02-17 21:19:27 -05:00
|
|
|
3dup define-mutator >r define-accessor r> cons ;
|
2005-01-29 16:39:30 -05:00
|
|
|
|
|
|
|
: tuple-predicate ( word -- )
|
|
|
|
#! Make a foo? word for testing the tuple class at the top
|
|
|
|
#! of the stack.
|
2005-02-14 21:58:07 -05:00
|
|
|
dup predicate-word swap [ swap class eq? ] cons
|
2005-01-29 16:39:30 -05:00
|
|
|
define-compound ;
|
|
|
|
|
2005-02-09 22:35:11 -05:00
|
|
|
: check-shape ( word slots -- )
|
|
|
|
#! If the new list of slots is different from the previous,
|
|
|
|
#! forget the old definition.
|
|
|
|
>r "use" get search dup [
|
|
|
|
dup "slots" word-property r> = [
|
|
|
|
drop
|
|
|
|
] [
|
|
|
|
forget
|
|
|
|
] ifte
|
|
|
|
] [
|
|
|
|
r> 2drop
|
|
|
|
] ifte ;
|
|
|
|
|
2005-02-10 15:14:20 -05:00
|
|
|
: define-slots ( tuple slots -- words )
|
2005-02-09 22:35:11 -05:00
|
|
|
2dup "slots" set-word-property
|
|
|
|
2dup length 1 + "tuple-size" set-word-property
|
|
|
|
dup length [ 3 + ] project zip
|
2005-02-10 15:14:20 -05:00
|
|
|
[ uncons define-slot ] map-with ;
|
|
|
|
|
|
|
|
: constructor-word ( word -- word )
|
|
|
|
word-name "<" swap ">" cat3 create-in ;
|
|
|
|
|
|
|
|
: define-constructor ( word def -- )
|
|
|
|
over constructor-word >r
|
|
|
|
[ swap literal, \ make-tuple , append, ] make-list
|
|
|
|
r> swap define-compound ;
|
|
|
|
|
|
|
|
: default-constructor ( tuple -- )
|
|
|
|
dup [
|
|
|
|
"slot-words" word-property
|
2005-02-17 21:19:27 -05:00
|
|
|
reverse [ cdr unit , \ keep , ] each
|
2005-02-10 15:14:20 -05:00
|
|
|
] make-list define-constructor ;
|
2005-02-09 22:35:11 -05:00
|
|
|
|
|
|
|
: define-tuple ( tuple slots -- )
|
|
|
|
2dup check-shape
|
|
|
|
>r
|
2005-02-10 15:14:20 -05:00
|
|
|
create-in
|
|
|
|
dup save-location
|
|
|
|
dup intern-symbol
|
|
|
|
dup tuple-predicate
|
|
|
|
dup tuple "metaclass" set-word-property
|
|
|
|
dup
|
|
|
|
dup r> define-slots "slot-words" set-word-property
|
|
|
|
default-constructor ;
|
2005-02-09 22:35:11 -05:00
|
|
|
|
2005-01-30 15:57:25 -05:00
|
|
|
: TUPLE:
|
2005-02-09 22:35:11 -05:00
|
|
|
#! Followed by a tuple name, then slot names, then ;
|
|
|
|
scan
|
2005-01-29 16:39:30 -05:00
|
|
|
string-mode on
|
|
|
|
[ string-mode off define-tuple ]
|
|
|
|
f ; parsing
|
|
|
|
|
2005-01-30 15:57:25 -05:00
|
|
|
: C:
|
2005-01-29 16:39:30 -05:00
|
|
|
#! Followed by a tuple name, then constructor code, then ;
|
|
|
|
#! Constructor code executes with the empty tuple on the
|
|
|
|
#! stack.
|
2005-02-10 15:14:20 -05:00
|
|
|
scan-word [ define-constructor ] f ; parsing
|
2005-01-29 16:39:30 -05:00
|
|
|
|
2005-01-30 15:57:25 -05:00
|
|
|
: tuple-delegate ( tuple -- obj )
|
2005-01-31 22:32:06 -05:00
|
|
|
dup tuple? [
|
|
|
|
dup class "delegate-field" word-property dup [
|
|
|
|
>fixnum slot
|
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] ifte
|
2005-01-30 15:57:25 -05:00
|
|
|
] [
|
2005-01-31 22:32:06 -05:00
|
|
|
drop f
|
2005-02-08 22:02:44 -05:00
|
|
|
] ifte ;
|
2005-01-30 15:57:25 -05:00
|
|
|
|
2005-02-08 22:02:44 -05:00
|
|
|
: alist>quot ( default alist -- quot )
|
|
|
|
#! Turn an association list that maps values to quotations
|
|
|
|
#! into a quotation that executes a quotation depending on
|
|
|
|
#! the value on the stack.
|
|
|
|
[
|
|
|
|
[
|
|
|
|
unswons
|
|
|
|
\ dup , unswons literal, \ = , \ drop swons ,
|
|
|
|
alist>quot , \ ifte ,
|
|
|
|
] make-list
|
|
|
|
] when* ;
|
|
|
|
|
|
|
|
: (hash>quot) ( default hash -- quot )
|
|
|
|
[
|
|
|
|
\ dup , \ hashcode , dup bucket-count , \ rem ,
|
|
|
|
buckets>list [ alist>quot ] map-with list>vector ,
|
|
|
|
\ dispatch ,
|
|
|
|
] make-list ;
|
|
|
|
|
|
|
|
: hash>quot ( default hash -- quot )
|
|
|
|
#! Turn a hash table that maps values to quotations into a
|
|
|
|
#! quotation that executes a quotation depending on the
|
|
|
|
#! value on the stack.
|
|
|
|
dup hash-size 4 <= [
|
|
|
|
hash>alist alist>quot
|
|
|
|
] [
|
|
|
|
(hash>quot)
|
|
|
|
] ifte ;
|
2005-02-01 19:00:16 -05:00
|
|
|
|
2005-02-08 22:02:44 -05:00
|
|
|
: default-tuple-method ( generic -- quot )
|
|
|
|
#! If the generic does not define a specific method for a
|
|
|
|
#! tuple, execute the return value of this.
|
|
|
|
dup "methods" word-property
|
|
|
|
tuple over hash dup [
|
|
|
|
2nip
|
2005-01-29 16:39:30 -05:00
|
|
|
] [
|
2005-02-08 22:02:44 -05:00
|
|
|
drop object over hash dup [
|
|
|
|
2nip
|
2005-01-30 15:57:25 -05:00
|
|
|
] [
|
2005-02-08 22:02:44 -05:00
|
|
|
2drop [ dup tuple-delegate ] swap
|
|
|
|
dup unit swap
|
|
|
|
unit [ car ] cons [ undefined-method ] append
|
|
|
|
\ ?ifte 3list append
|
|
|
|
] ifte
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: tuple-dispatch-quot ( generic -- quot )
|
|
|
|
#! Generate a quotation that performs tuple class dispatch
|
|
|
|
#! for methods defined on the given generic.
|
|
|
|
dup default-tuple-method \ drop swons
|
|
|
|
swap "methods" word-property hash>quot
|
|
|
|
[ dup class ] swap append ;
|
2005-01-29 16:39:30 -05:00
|
|
|
|
|
|
|
: add-tuple-dispatch ( word vtable -- )
|
2005-02-08 22:02:44 -05:00
|
|
|
>r tuple-dispatch-quot tuple r> set-vtable ;
|
2005-01-29 16:39:30 -05:00
|
|
|
|
2005-02-07 11:51:22 -05:00
|
|
|
: clone-tuple ( tuple -- tuple )
|
|
|
|
#! Make a shallow copy of a tuple, without cloning its
|
|
|
|
#! delegate.
|
2005-01-31 14:02:09 -05:00
|
|
|
dup array-capacity dup <tuple> [ -rot copy-array ] keep ;
|
|
|
|
|
2005-02-07 11:51:22 -05:00
|
|
|
: clone-delegate ( tuple -- )
|
|
|
|
dup class "delegate-field" word-property dup [
|
|
|
|
[ >fixnum slot clone ] 2keep set-slot
|
|
|
|
] [
|
|
|
|
2drop
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
M: tuple clone ( tuple -- tuple )
|
|
|
|
#! Clone a tuple and its delegate.
|
|
|
|
clone-tuple dup clone-delegate ;
|
|
|
|
|
2005-01-31 14:02:09 -05:00
|
|
|
: tuple>list ( tuple -- list )
|
2005-02-17 16:10:35 -05:00
|
|
|
>tuple dup array-capacity swap array>list ;
|
2005-01-31 14:02:09 -05:00
|
|
|
|
|
|
|
M: tuple = ( obj tuple -- ? )
|
|
|
|
over tuple? [
|
|
|
|
over class over class = [
|
|
|
|
swap tuple>list swap tuple>list =
|
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] ifte
|
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
M: tuple hashcode ( vec -- n )
|
|
|
|
dup array-capacity 1 number= [
|
|
|
|
drop 0
|
|
|
|
] [
|
|
|
|
1 swap array-nth hashcode
|
|
|
|
] ifte ;
|
|
|
|
|
2005-01-29 16:39:30 -05:00
|
|
|
M: tuple class ( obj -- class ) 2 slot ;
|
|
|
|
|
|
|
|
tuple [
|
|
|
|
( generic vtable definition class -- )
|
|
|
|
2drop add-tuple-dispatch
|
|
|
|
] "add-method" set-word-property
|
|
|
|
|
|
|
|
tuple [
|
|
|
|
drop tuple "builtin-type" word-property unit
|
|
|
|
] "builtin-supertypes" set-word-property
|
|
|
|
|
|
|
|
tuple 10 "priority" set-word-property
|
|
|
|
|
|
|
|
tuple [ 2drop t ] "class<" set-word-property
|