2005-01-29 16:39:30 -05:00
|
|
|
! Copyright (C) 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2005-02-20 19:03:37 -05:00
|
|
|
IN: kernel-internals
|
|
|
|
USING: words parser kernel namespaces lists strings math
|
2005-04-02 02:39:33 -05:00
|
|
|
hashtables errors sequences vectors ;
|
2005-02-20 19:03:37 -05:00
|
|
|
|
2005-03-08 22:54:59 -05:00
|
|
|
! Tuples are really arrays in the runtime, but with a different
|
|
|
|
! type number. The layout is as follows:
|
|
|
|
|
|
|
|
! slot 0 - object header with type number (as usual)
|
|
|
|
! slot 1 - length, including class/delegate slots
|
|
|
|
! slot 2 - the class, a word
|
|
|
|
! slot 3 - the delegate tuple, or f
|
|
|
|
|
2005-02-20 19:03:37 -05:00
|
|
|
: make-tuple ( class size -- tuple )
|
|
|
|
#! Internal allocation function. Do not call it directly,
|
|
|
|
#! since you can fool the runtime and corrupt memory by
|
|
|
|
#! specifying an incorrect size.
|
2005-05-13 18:27:18 -04:00
|
|
|
<tuple> [ 2 set-slot ] keep ;
|
2005-02-20 19:03:37 -05:00
|
|
|
|
2005-01-29 16:39:30 -05:00
|
|
|
IN: generic
|
|
|
|
|
2005-05-14 17:18:45 -04:00
|
|
|
DEFER: tuple?
|
2005-05-14 21:15:50 -04:00
|
|
|
BUILTIN: tuple 18 tuple? ;
|
2005-02-20 19:03:37 -05:00
|
|
|
|
2005-04-12 13:35:27 -04:00
|
|
|
M: tuple delegate 3 slot ;
|
2005-03-08 22:54:59 -05:00
|
|
|
M: tuple set-delegate 3 set-slot ;
|
|
|
|
|
2005-02-18 20:37:01 -05:00
|
|
|
: class ( obj -- class )
|
|
|
|
#! The class of an object.
|
2005-07-28 18:20:31 -04:00
|
|
|
dup tuple? [ 2 slot ] [ type builtin-type ] ifte ; inline
|
2005-02-18 20:37:01 -05:00
|
|
|
|
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-07-28 18:20:31 -04:00
|
|
|
dup predicate-word
|
|
|
|
2dup register-predicate
|
|
|
|
swap [ \ class , literal, \ eq? , ] make-list
|
|
|
|
define-compound ;
|
2005-01-29 16:39:30 -05:00
|
|
|
|
2005-05-15 21:17:56 -04:00
|
|
|
: forget-tuple ( class -- )
|
2005-05-16 17:01:39 -04:00
|
|
|
dup forget "predicate" word-prop car [ forget ] when* ;
|
2005-05-15 21:17:56 -04:00
|
|
|
|
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 [
|
2005-03-08 22:54:59 -05:00
|
|
|
dup "tuple-size" word-prop r> length 2 + =
|
2005-05-15 21:17:56 -04:00
|
|
|
[ drop ] [ forget-tuple ] ifte
|
2005-02-09 22:35:11 -05:00
|
|
|
] [
|
|
|
|
r> 2drop
|
|
|
|
] ifte ;
|
|
|
|
|
2005-02-20 19:03:37 -05:00
|
|
|
: tuple-slots ( tuple slots -- )
|
2005-03-26 20:12:14 -05:00
|
|
|
2dup "slot-names" set-word-prop
|
2005-03-08 22:54:59 -05:00
|
|
|
2dup length 2 + "tuple-size" set-word-prop
|
2005-07-25 17:13:35 -04:00
|
|
|
dupd 4 simple-slots
|
|
|
|
2dup { [ 3 delegate set-delegate ] } swap append
|
|
|
|
"slots" set-word-prop
|
|
|
|
define-slots ;
|
2005-02-10 15:14:20 -05:00
|
|
|
|
|
|
|
: define-constructor ( word def -- )
|
2005-05-04 22:34:55 -04:00
|
|
|
>r [ word-name "in" get constructor-word ] keep [
|
2005-03-05 14:45:23 -05:00
|
|
|
dup literal, "tuple-size" word-prop , \ make-tuple ,
|
2005-02-20 19:03:37 -05:00
|
|
|
] make-list r> append define-compound ;
|
2005-02-10 15:14:20 -05:00
|
|
|
|
|
|
|
: default-constructor ( tuple -- )
|
|
|
|
dup [
|
2005-07-25 17:13:35 -04:00
|
|
|
"slots" word-prop 1 swap tail-slice reverse-slice
|
|
|
|
[ peek 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
|
2005-02-20 19:03:37 -05:00
|
|
|
>r create-in
|
2005-02-10 15:14:20 -05:00
|
|
|
dup intern-symbol
|
|
|
|
dup tuple-predicate
|
2005-03-05 14:45:23 -05:00
|
|
|
dup tuple "metaclass" set-word-prop
|
2005-02-20 19:03:37 -05:00
|
|
|
dup r> tuple-slots
|
2005-02-10 15:14:20 -05:00
|
|
|
default-constructor ;
|
2005-02-09 22:35:11 -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
|
2005-03-06 19:46:29 -05:00
|
|
|
\ dup , unswons literal, \ eq? , \ drop swons ,
|
2005-02-08 22:02:44 -05:00
|
|
|
alist>quot , \ ifte ,
|
|
|
|
] make-list
|
|
|
|
] when* ;
|
|
|
|
|
|
|
|
: (hash>quot) ( default hash -- quot )
|
|
|
|
[
|
|
|
|
\ dup , \ hashcode , dup bucket-count , \ rem ,
|
2005-04-17 21:59:11 -04:00
|
|
|
buckets>list [ alist>quot ] map-with >vector ,
|
2005-02-08 22:02:44 -05:00
|
|
|
\ 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.
|
2005-06-15 23:27:28 -04:00
|
|
|
( dup hash-size 4 <= ) t [
|
2005-02-08 22:02:44 -05:00
|
|
|
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.
|
2005-03-05 14:45:23 -05:00
|
|
|
dup "methods" word-prop
|
2005-03-01 18:55:25 -05:00
|
|
|
tuple over hash* dup [
|
|
|
|
2nip cdr
|
2005-01-29 16:39:30 -05:00
|
|
|
] [
|
2005-03-01 18:55:25 -05:00
|
|
|
drop object over hash* dup [
|
|
|
|
2nip cdr
|
2005-01-30 15:57:25 -05:00
|
|
|
] [
|
2005-05-14 21:15:50 -04:00
|
|
|
2drop empty-method
|
2005-02-08 22:02:44 -05:00
|
|
|
] ifte
|
|
|
|
] ifte ;
|
|
|
|
|
2005-05-03 23:50:04 -04:00
|
|
|
: tuple-methods ( generic -- hash )
|
|
|
|
#! A hashtable of methods on tuples.
|
|
|
|
"methods" word-prop [ car metaclass tuple = ] hash-subset ;
|
|
|
|
|
2005-02-08 22:02:44 -05:00
|
|
|
: 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
|
2005-05-14 17:18:45 -04:00
|
|
|
over tuple-methods hash>quot
|
2005-07-28 18:20:31 -04:00
|
|
|
>r "picker" word-prop [ class ] r> append3 ;
|
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-05-16 01:15:48 -04:00
|
|
|
! A sequence of all slots in a tuple, used for equality testing.
|
|
|
|
TUPLE: mirror tuple ;
|
|
|
|
|
|
|
|
C: mirror ( tuple -- mirror )
|
|
|
|
over tuple? [
|
|
|
|
[ set-mirror-tuple ] keep
|
2005-04-12 13:35:27 -04:00
|
|
|
] [
|
|
|
|
"Not a tuple" throw
|
|
|
|
] ifte ;
|
|
|
|
|
2005-05-16 01:15:48 -04:00
|
|
|
M: mirror nth ( n mirror -- elt )
|
|
|
|
bounds-check mirror-tuple array-nth ;
|
|
|
|
|
|
|
|
M: mirror set-nth ( n mirror -- elt )
|
|
|
|
bounds-check mirror-tuple set-array-nth ;
|
|
|
|
|
|
|
|
M: mirror length ( mirror -- len )
|
|
|
|
mirror-tuple array-capacity ;
|
|
|
|
|
2005-07-27 20:13:11 -04:00
|
|
|
: literal-tuple ( list -- tuple )
|
|
|
|
dup first "tuple-size" word-prop <tuple>
|
|
|
|
[ <mirror> swap copy-into ] keep ;
|
|
|
|
|
2005-02-07 11:51:22 -05:00
|
|
|
: clone-tuple ( tuple -- tuple )
|
|
|
|
#! Make a shallow copy of a tuple, without cloning its
|
|
|
|
#! delegate.
|
2005-06-08 18:11:53 -04:00
|
|
|
[ array-capacity <tuple> dup ] keep copy-array ;
|
2005-01-31 14:02:09 -05:00
|
|
|
|
2005-02-07 11:51:22 -05:00
|
|
|
M: tuple clone ( tuple -- tuple )
|
|
|
|
#! Clone a tuple and its delegate.
|
2005-03-08 22:54:59 -05:00
|
|
|
clone-tuple dup delegate clone over set-delegate ;
|
2005-02-07 11:51:22 -05:00
|
|
|
|
2005-01-31 14:02:09 -05:00
|
|
|
M: tuple hashcode ( vec -- n )
|
2005-03-08 22:54:59 -05:00
|
|
|
#! If the capacity is two, then all we have is the class
|
|
|
|
#! slot and delegate.
|
2005-06-08 04:49:05 -04:00
|
|
|
dup array-capacity 2 number= [
|
2005-01-31 14:02:09 -05:00
|
|
|
drop 0
|
|
|
|
] [
|
2005-05-28 20:52:23 -04:00
|
|
|
2 swap array-nth hashcode
|
2005-01-31 14:02:09 -05:00
|
|
|
] ifte ;
|
|
|
|
|
2005-04-12 13:35:27 -04:00
|
|
|
M: tuple = ( obj tuple -- ? )
|
|
|
|
2dup eq? [
|
|
|
|
2drop t
|
|
|
|
] [
|
|
|
|
over tuple? [
|
2005-05-16 01:15:48 -04:00
|
|
|
swap <mirror> swap <mirror> sequence=
|
2005-04-12 13:35:27 -04:00
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] ifte
|
|
|
|
] ifte ;
|
|
|
|
|
2005-01-29 16:39:30 -05:00
|
|
|
tuple [
|
|
|
|
( generic vtable definition class -- )
|
|
|
|
2drop add-tuple-dispatch
|
2005-03-05 14:45:23 -05:00
|
|
|
] "add-method" set-word-prop
|
2005-01-29 16:39:30 -05:00
|
|
|
|
|
|
|
tuple [
|
2005-03-05 14:45:23 -05:00
|
|
|
drop tuple "builtin-type" word-prop unit
|
|
|
|
] "builtin-supertypes" set-word-prop
|
2005-01-29 16:39:30 -05:00
|
|
|
|
2005-03-05 14:45:23 -05:00
|
|
|
tuple 10 "priority" set-word-prop
|
2005-01-29 16:39:30 -05:00
|
|
|
|
2005-03-05 14:45:23 -05:00
|
|
|
tuple [ 2drop t ] "class<" set-word-prop
|
2005-03-26 20:12:14 -05:00
|
|
|
|
|
|
|
PREDICATE: word tuple-class metaclass tuple = ;
|
2005-07-13 18:08:54 -04:00
|
|
|
|
|
|
|
: is? ( obj pred -- ? | pred: obj -- ? )
|
|
|
|
#! Tests if the object satisfies the predicate, or if
|
|
|
|
#! it delegates to an object satisfying it.
|
|
|
|
[ call ] 2keep rot [
|
|
|
|
2drop t
|
|
|
|
] [
|
|
|
|
over [ >r delegate r> is? ] [ 2drop f ] ifte
|
2005-07-18 02:08:41 -04:00
|
|
|
] ifte ; inline
|