slots, classes.tuple: make 'integer'-declared slots call >integer on the new value before storing it in, and remove code duplication between type checks in writer methods and boa constructors
parent
dd0571e69a
commit
d0dc6ba8af
|
@ -6,7 +6,7 @@ io.streams.string kernel kernel.private math math.constants
|
||||||
math.order namespaces parser parser.notes prettyprint
|
math.order namespaces parser parser.notes prettyprint
|
||||||
quotations random see sequences sequences.private slots
|
quotations random see sequences sequences.private slots
|
||||||
slots.private splitting strings summary threads tools.test
|
slots.private splitting strings summary threads tools.test
|
||||||
vectors vocabs words words.symbol fry ;
|
vectors vocabs words words.symbol fry literals ;
|
||||||
IN: classes.tuple.tests
|
IN: classes.tuple.tests
|
||||||
|
|
||||||
TUPLE: rect x y w h ;
|
TUPLE: rect x y w h ;
|
||||||
|
@ -577,8 +577,31 @@ unit-test
|
||||||
[ T{ bad-slot-value f "hi" fixnum } = ]
|
[ T{ bad-slot-value f "hi" fixnum } = ]
|
||||||
must-fail-with
|
must-fail-with
|
||||||
|
|
||||||
[ T{ declared-types f 0 "hi" } ]
|
! Check fixnum coercer
|
||||||
[ 0.0 "hi" declared-types boa ] unit-test
|
[ 0 ] [ 0.0 "hi" declared-types boa n>> ] unit-test
|
||||||
|
|
||||||
|
[ 0 ] [ declared-types new 0.0 >>n n>> ] unit-test
|
||||||
|
|
||||||
|
! Check bignum coercer
|
||||||
|
TUPLE: bignum-coercer { n bignum initial: $[ 0 >bignum ] } ;
|
||||||
|
|
||||||
|
[ 13 bignum ] [ 13.5 bignum-coercer boa n>> dup class ] unit-test
|
||||||
|
|
||||||
|
[ 13 bignum ] [ bignum-coercer new 13.5 >>n n>> dup class ] unit-test
|
||||||
|
|
||||||
|
! Check float coercer
|
||||||
|
TUPLE: float-coercer { n float } ;
|
||||||
|
|
||||||
|
[ 13.0 float ] [ 13 float-coercer boa n>> dup class ] unit-test
|
||||||
|
|
||||||
|
[ 13.0 float ] [ float-coercer new 13 >>n n>> dup class ] unit-test
|
||||||
|
|
||||||
|
! Check integer coercer
|
||||||
|
TUPLE: integer-coercer { n integer } ;
|
||||||
|
|
||||||
|
[ 13 fixnum ] [ 13.5 integer-coercer boa n>> dup class ] unit-test
|
||||||
|
|
||||||
|
[ 13 fixnum ] [ integer-coercer new 13.5 >>n n>> dup class ] unit-test
|
||||||
|
|
||||||
: foo ( a b -- c ) declared-types boa ;
|
: foo ( a b -- c ) declared-types boa ;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
! Copyright (C) 2005, 2008 Slava Pestov.
|
! Copyright (C) 2005, 2010 Slava Pestov.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: arrays definitions hashtables kernel kernel.private math
|
USING: arrays definitions hashtables kernel kernel.private math
|
||||||
namespaces make sequences sequences.private strings vectors
|
namespaces make sequences sequences.private strings vectors
|
||||||
|
@ -121,25 +121,6 @@ ERROR: bad-superclass class ;
|
||||||
: class-size ( class -- n )
|
: class-size ( class -- n )
|
||||||
superclasses [ "slots" word-prop length ] map-sum ;
|
superclasses [ "slots" word-prop length ] map-sum ;
|
||||||
|
|
||||||
: (instance-check-quot) ( class -- quot )
|
|
||||||
[
|
|
||||||
\ dup ,
|
|
||||||
[ "predicate" word-prop % ]
|
|
||||||
[ [ literalize , \ bad-slot-value , ] [ ] make , ] bi
|
|
||||||
\ unless ,
|
|
||||||
] [ ] make ;
|
|
||||||
|
|
||||||
: (fixnum-check-quot) ( class -- quot )
|
|
||||||
(instance-check-quot) fixnum "coercer" word-prop prepend ;
|
|
||||||
|
|
||||||
: instance-check-quot ( class -- quot )
|
|
||||||
{
|
|
||||||
{ [ dup object bootstrap-word eq? ] [ drop [ ] ] }
|
|
||||||
{ [ dup "coercer" word-prop ] [ "coercer" word-prop ] }
|
|
||||||
{ [ dup \ fixnum class<= ] [ (fixnum-check-quot) ] }
|
|
||||||
[ (instance-check-quot) ]
|
|
||||||
} cond ;
|
|
||||||
|
|
||||||
: boa-check-quot ( class -- quot )
|
: boa-check-quot ( class -- quot )
|
||||||
all-slots [ class>> instance-check-quot ] map spread>quot
|
all-slots [ class>> instance-check-quot ] map spread>quot
|
||||||
f like ;
|
f like ;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
! Copyright (C) 2005, 2009 Slava Pestov.
|
! Copyright (C) 2005, 2010 Slava Pestov.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: arrays byte-arrays kernel kernel.private math namespaces
|
USING: arrays byte-arrays kernel kernel.private math namespaces
|
||||||
make sequences strings effects generic generic.standard
|
make sequences strings effects generic generic.standard
|
||||||
|
@ -64,39 +64,29 @@ M: object reader-quot
|
||||||
|
|
||||||
ERROR: bad-slot-value value class ;
|
ERROR: bad-slot-value value class ;
|
||||||
|
|
||||||
: writer-quot/object ( slot-spec -- )
|
: (instance-check-quot) ( class -- quot )
|
||||||
offset>> , \ set-slot , ;
|
|
||||||
|
|
||||||
: writer-quot/coerce ( slot-spec -- )
|
|
||||||
[ class>> "coercer" word-prop [ dip ] curry % ]
|
|
||||||
[ offset>> , \ set-slot , ]
|
|
||||||
bi ;
|
|
||||||
|
|
||||||
: writer-quot/check ( slot-spec -- )
|
|
||||||
[ offset>> , ]
|
|
||||||
[
|
[
|
||||||
\ pick ,
|
\ dup ,
|
||||||
dup class>> "predicate" word-prop %
|
[ "predicate" word-prop % ]
|
||||||
[ set-slot ] ,
|
[ [ bad-slot-value ] curry , ] bi
|
||||||
class>> [ 2nip bad-slot-value ] curry [ ] like ,
|
\ unless ,
|
||||||
\ if ,
|
] [ ] make ;
|
||||||
]
|
|
||||||
bi ;
|
|
||||||
|
|
||||||
: writer-quot/fixnum ( slot-spec -- )
|
: instance-check-quot ( class -- quot )
|
||||||
[ [ >fixnum ] dip ] % writer-quot/check ;
|
{
|
||||||
|
{ [ dup object bootstrap-word eq? ] [ drop [ ] ] }
|
||||||
|
{ [ dup "coercer" word-prop ] [ "coercer" word-prop ] }
|
||||||
|
{ [ dup integer bootstrap-word eq? ] [ drop [ >integer ] ] }
|
||||||
|
[ (instance-check-quot) ]
|
||||||
|
} cond ;
|
||||||
|
|
||||||
GENERIC# writer-quot 1 ( class slot-spec -- quot )
|
GENERIC# writer-quot 1 ( class slot-spec -- quot )
|
||||||
|
|
||||||
M: object writer-quot
|
M: object writer-quot
|
||||||
nip [
|
nip
|
||||||
{
|
[ class>> instance-check-quot dup empty? [ [ dip ] curry ] unless ]
|
||||||
{ [ dup class>> object bootstrap-word eq? ] [ writer-quot/object ] }
|
[ offset>> [ set-slot ] curry ]
|
||||||
{ [ dup class>> "coercer" word-prop ] [ writer-quot/coerce ] }
|
bi append ;
|
||||||
{ [ dup class>> fixnum bootstrap-word class<= ] [ writer-quot/fixnum ] }
|
|
||||||
[ writer-quot/check ]
|
|
||||||
} cond
|
|
||||||
] [ ] make ;
|
|
||||||
|
|
||||||
: writer-props ( slot-spec -- assoc )
|
: writer-props ( slot-spec -- assoc )
|
||||||
"writing" associate ;
|
"writing" associate ;
|
||||||
|
|
Loading…
Reference in New Issue