pprint structs with tuple syntax
parent
940fbd5ace
commit
25c3434892
|
@ -125,7 +125,7 @@ M: pathname pprint*
|
|||
] if ; inline
|
||||
|
||||
: tuple>assoc ( tuple -- assoc )
|
||||
[ class all-slots ] [ tuple-slots ] bi zip
|
||||
[ class class-slots ] [ object-slots ] bi zip
|
||||
[ [ initial>> ] dip = not ] assoc-filter
|
||||
[ [ name>> ] dip ] assoc-map ;
|
||||
|
||||
|
@ -182,10 +182,12 @@ M: hashtable >pprint-sequence >alist ;
|
|||
M: wrapper >pprint-sequence wrapped>> 1array ;
|
||||
M: callstack >pprint-sequence callstack>array ;
|
||||
|
||||
M: tuple >pprint-sequence
|
||||
[ class ] [ tuple-slots ] bi
|
||||
: class-slot-sequence ( class slots -- sequence )
|
||||
[ 1array ] [ [ f 2array ] dip append ] if-empty ;
|
||||
|
||||
M: tuple >pprint-sequence
|
||||
[ class ] [ object-slots ] bi class-slot-sequence ;
|
||||
|
||||
M: object pprint-narrow? drop f ;
|
||||
M: byte-vector pprint-narrow? drop f ;
|
||||
M: array pprint-narrow? drop t ;
|
||||
|
|
|
@ -18,6 +18,11 @@ ERROR: not-a-tuple object ;
|
|||
: all-slots ( class -- slots )
|
||||
superclasses [ "slots" word-prop ] map concat ;
|
||||
|
||||
GENERIC: class-slots ( class -- slots )
|
||||
|
||||
M: tuple-class class-slots
|
||||
all-slots ;
|
||||
|
||||
PREDICATE: immutable-tuple-class < tuple-class ( class -- ? )
|
||||
all-slots [ read-only>> ] all? ;
|
||||
|
||||
|
@ -64,6 +69,10 @@ PRIVATE>
|
|||
: tuple-slots ( tuple -- seq )
|
||||
prepare-tuple>array drop copy-tuple-slots ;
|
||||
|
||||
GENERIC: object-slots ( object -- seq )
|
||||
M: tuple object-slots
|
||||
tuple-slots ;
|
||||
|
||||
GENERIC: slots>tuple ( seq class -- tuple )
|
||||
|
||||
M: tuple-class slots>tuple ( seq class -- tuple )
|
||||
|
|
|
@ -8,4 +8,3 @@ M: struct-class see-class*
|
|||
<block "struct-slots" word-prop [ pprint-slot ] each
|
||||
block> pprint-; block> ;
|
||||
|
||||
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
USING: accessors alien alien.c-types byte-arrays classes
|
||||
classes.c-types classes.parser classes.tuple
|
||||
classes.tuple.parser classes.tuple.private combinators
|
||||
combinators.smart fry generalizations kernel kernel.private
|
||||
libc macros make math math.order quotations sequences slots
|
||||
slots.private words ;
|
||||
combinators.smart fry generalizations generic.parser kernel
|
||||
kernel.private libc macros make math math.order quotations
|
||||
sequences slots slots.private words ;
|
||||
IN: classes.struct
|
||||
|
||||
! struct class
|
||||
|
@ -61,6 +61,19 @@ M: struct-class reader-quot
|
|||
M: struct-class writer-quot
|
||||
nip (writer-quot) ;
|
||||
|
||||
M: struct-class class-slots
|
||||
"struct-slots" word-prop ;
|
||||
|
||||
: object-slots-quot ( class -- quot )
|
||||
"struct-slots" word-prop
|
||||
[ name>> reader-word 1quotation ] map
|
||||
\ cleave [ ] 2sequence
|
||||
\ output>array [ ] 2sequence ;
|
||||
|
||||
: (define-object-slots-method) ( class -- )
|
||||
[ \ object-slots create-method-in ]
|
||||
[ object-slots-quot ] bi define ;
|
||||
|
||||
! Struct as c-type
|
||||
|
||||
: align-offset ( offset class -- offset' )
|
||||
|
@ -124,7 +137,11 @@ M: struct-class heap-size
|
|||
make-slots dup
|
||||
[ check-struct-slots ] [ struct-offsets ] [ struct-align [ align ] keep ] tri
|
||||
(define-struct-class)
|
||||
] [ drop dup struct-prototype "prototype" set-word-prop ] 2tri ;
|
||||
] [
|
||||
drop
|
||||
[ dup struct-prototype "prototype" set-word-prop ]
|
||||
[ (define-object-slots-method) ] bi
|
||||
] 2tri ;
|
||||
|
||||
: parse-struct-definition ( -- class slots )
|
||||
CREATE-CLASS [ parse-tuple-slots ] { } make ;
|
||||
|
|
Loading…
Reference in New Issue