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
|
|
|
|
hashtables errors vectors ;
|
|
|
|
|
|
|
|
: 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.
|
|
|
|
<tuple> [ 0 swap set-array-nth ] keep ;
|
|
|
|
|
2005-01-29 16:39:30 -05:00
|
|
|
IN: generic
|
|
|
|
|
2005-02-20 19:03:37 -05:00
|
|
|
BUILTIN: tuple 18 [ 1 array-capacity f ] ;
|
|
|
|
|
|
|
|
#! arrayed objects can be passed to array-capacity,
|
|
|
|
#! array-nth, and set-array-nth.
|
|
|
|
UNION: arrayed array tuple ;
|
2005-02-19 23:25:21 -05:00
|
|
|
|
2005-02-18 20:37:01 -05:00
|
|
|
: class ( obj -- class )
|
|
|
|
#! The class of an object.
|
|
|
|
dup tuple? [ 2 slot ] [ type builtin-type ] ifte ;
|
|
|
|
|
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 )
|
2005-03-05 14:45:23 -05:00
|
|
|
dup car "tuple-size" word-prop over length over = [
|
2005-02-05 22:51:41 -05:00
|
|
|
(literal-tuple)
|
|
|
|
] [
|
|
|
|
"Incorrect tuple length" throw
|
|
|
|
] ifte ;
|
|
|
|
|
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 [
|
2005-03-05 14:45:23 -05:00
|
|
|
dup "tuple-size" word-prop r> length 1 + =
|
2005-02-20 19:03:37 -05:00
|
|
|
[ drop ] [ forget ] 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-05 14:45:23 -05:00
|
|
|
2dup length 1 + "tuple-size" set-word-prop
|
2005-02-20 19:03:37 -05:00
|
|
|
3 -rot simple-slots ;
|
2005-02-10 15:14:20 -05:00
|
|
|
|
|
|
|
: constructor-word ( word -- word )
|
|
|
|
word-name "<" swap ">" cat3 create-in ;
|
|
|
|
|
|
|
|
: define-constructor ( word def -- )
|
2005-02-20 19:03:37 -05:00
|
|
|
>r [ 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-03-05 14:45:23 -05:00
|
|
|
"slots" word-prop
|
2005-02-20 19:03:37 -05:00
|
|
|
reverse [ last 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 save-location
|
|
|
|
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 ,
|
|
|
|
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.
|
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-03-06 19:46:29 -05:00
|
|
|
2drop [ dup delegate ] swap
|
2005-02-08 22:02:44 -05:00
|
|
|
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
|
2005-03-05 14:45:23 -05:00
|
|
|
swap "methods" word-prop hash>quot
|
2005-02-08 22:02:44 -05:00
|
|
|
[ 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 -- )
|
2005-03-05 14:45:23 -05:00
|
|
|
dup class "delegate-slot" word-prop dup [
|
2005-02-07 11:51:22 -05:00
|
|
|
[ >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-20 19:03:37 -05:00
|
|
|
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
|
|
|
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
|