2009-08-12 09:15:46 -04:00
|
|
|
! (c)Joe Groff bsd license
|
2009-08-26 18:19:30 -04:00
|
|
|
USING: accessors alien alien.c-types alien.structs
|
|
|
|
alien.structs.fields arrays byte-arrays classes classes.parser
|
|
|
|
classes.tuple classes.tuple.parser classes.tuple.private
|
2009-08-29 22:04:19 -04:00
|
|
|
combinators combinators.short-circuit combinators.smart
|
|
|
|
functors.backend fry generalizations generic.parser kernel
|
|
|
|
kernel.private lexer libc locals macros make math math.order parser
|
|
|
|
quotations sequences slots slots.private struct-arrays vectors
|
|
|
|
words compiler.tree.propagation.transforms ;
|
2009-08-20 09:44:19 -04:00
|
|
|
FROM: slots => reader-word writer-word ;
|
2009-08-11 22:13:18 -04:00
|
|
|
IN: classes.struct
|
|
|
|
|
|
|
|
! struct class
|
|
|
|
|
2009-08-26 23:37:59 -04:00
|
|
|
ERROR: struct-must-have-slots ;
|
|
|
|
|
2009-08-11 22:13:18 -04:00
|
|
|
TUPLE: struct
|
|
|
|
{ (underlying) c-ptr read-only } ;
|
|
|
|
|
2009-08-25 14:03:43 -04:00
|
|
|
TUPLE: struct-slot-spec < slot-spec
|
|
|
|
c-type ;
|
|
|
|
|
2009-08-11 22:13:18 -04:00
|
|
|
PREDICATE: struct-class < tuple-class
|
2009-08-26 22:18:19 -04:00
|
|
|
{ [ \ struct subclass-of? ] [ all-slots length 1 = ] } 1&& ;
|
2009-08-11 22:13:18 -04:00
|
|
|
|
2009-08-19 21:21:57 -04:00
|
|
|
: struct-slots ( struct -- slots )
|
2009-08-19 19:53:44 -04:00
|
|
|
"struct-slots" word-prop ;
|
|
|
|
|
2009-08-11 22:13:18 -04:00
|
|
|
! struct allocation
|
|
|
|
|
|
|
|
M: struct >c-ptr
|
|
|
|
2 slot { c-ptr } declare ; inline
|
|
|
|
|
2009-08-26 19:05:38 -04:00
|
|
|
M: struct equal?
|
|
|
|
{
|
|
|
|
[ [ class ] bi@ = ]
|
|
|
|
[ [ >c-ptr ] [ [ >c-ptr ] [ byte-length ] bi ] bi* memory= ]
|
|
|
|
} 2&& ;
|
|
|
|
|
2009-08-11 22:13:18 -04:00
|
|
|
: memory>struct ( ptr class -- struct )
|
2009-08-26 22:18:19 -04:00
|
|
|
[ 1array ] dip slots>tuple ;
|
|
|
|
|
|
|
|
\ memory>struct [
|
|
|
|
dup struct-class? [ '[ _ boa ] ] [ drop f ] if
|
|
|
|
] 1 define-partial-eval
|
2009-08-11 22:13:18 -04:00
|
|
|
|
|
|
|
: malloc-struct ( class -- struct )
|
2009-08-29 18:17:25 -04:00
|
|
|
[ 1 swap heap-size calloc ] keep memory>struct ; inline
|
2009-08-11 22:13:18 -04:00
|
|
|
|
2009-08-14 07:09:37 -04:00
|
|
|
: (struct) ( class -- struct )
|
2009-08-11 22:13:18 -04:00
|
|
|
[ heap-size <byte-array> ] keep memory>struct ; inline
|
|
|
|
|
2009-08-26 22:27:12 -04:00
|
|
|
: struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
|
|
|
|
|
2009-08-14 07:09:37 -04:00
|
|
|
: <struct> ( class -- struct )
|
2009-08-26 22:27:12 -04:00
|
|
|
dup struct-prototype
|
2009-08-14 07:09:37 -04:00
|
|
|
[ >c-ptr clone swap memory>struct ] [ (struct) ] if* ; inline
|
2009-08-11 22:13:18 -04:00
|
|
|
|
2009-08-12 09:37:39 -04:00
|
|
|
MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
|
|
|
|
[
|
2009-08-14 07:09:37 -04:00
|
|
|
[ <wrapper> \ (struct) [ ] 2sequence ]
|
2009-08-12 09:37:39 -04:00
|
|
|
[
|
2009-08-19 19:53:44 -04:00
|
|
|
struct-slots
|
2009-08-12 09:37:39 -04:00
|
|
|
[ length \ ndip ]
|
|
|
|
[ [ name>> setter-word 1quotation ] map \ spread ] bi
|
|
|
|
] bi
|
|
|
|
] [ ] output>sequence ;
|
|
|
|
|
2009-08-18 11:26:45 -04:00
|
|
|
: pad-struct-slots ( values class -- values' class )
|
2009-08-19 21:21:57 -04:00
|
|
|
[ struct-slots [ initial>> ] map over length tail append ] keep ;
|
2009-08-12 15:40:06 -04:00
|
|
|
|
2009-08-19 23:50:02 -04:00
|
|
|
: (reader-quot) ( slot -- quot )
|
2009-08-25 14:03:43 -04:00
|
|
|
[ c-type>> c-type-getter-boxer ]
|
2009-08-19 23:50:02 -04:00
|
|
|
[ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
|
|
|
|
|
2009-08-18 11:26:45 -04:00
|
|
|
: (writer-quot) ( slot -- quot )
|
2009-08-25 14:03:43 -04:00
|
|
|
[ c-type>> c-setter ]
|
2009-08-18 11:26:45 -04:00
|
|
|
[ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
|
|
|
|
|
2009-08-19 23:50:02 -04:00
|
|
|
: (boxer-quot) ( class -- quot )
|
|
|
|
'[ _ memory>struct ] ;
|
|
|
|
|
|
|
|
: (unboxer-quot) ( class -- quot )
|
|
|
|
drop [ >c-ptr ] ;
|
|
|
|
|
2009-08-12 15:40:06 -04:00
|
|
|
M: struct-class boa>object
|
|
|
|
swap pad-struct-slots
|
2009-08-19 19:53:44 -04:00
|
|
|
[ (struct) ] [ struct-slots ] bi
|
2009-08-18 11:26:45 -04:00
|
|
|
[ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
|
2009-08-12 15:40:06 -04:00
|
|
|
|
2009-08-11 22:13:18 -04:00
|
|
|
! Struct slot accessors
|
|
|
|
|
2009-08-19 19:53:44 -04:00
|
|
|
GENERIC: struct-slot-values ( struct -- sequence )
|
|
|
|
|
2009-08-11 22:13:18 -04:00
|
|
|
M: struct-class reader-quot
|
2009-08-19 23:50:02 -04:00
|
|
|
nip (reader-quot) ;
|
2009-08-11 22:13:18 -04:00
|
|
|
|
2009-08-12 09:15:46 -04:00
|
|
|
M: struct-class writer-quot
|
|
|
|
nip (writer-quot) ;
|
|
|
|
|
2009-08-19 19:53:44 -04:00
|
|
|
: struct-slot-values-quot ( class -- quot )
|
|
|
|
struct-slots
|
2009-08-12 13:16:43 -04:00
|
|
|
[ name>> reader-word 1quotation ] map
|
|
|
|
\ cleave [ ] 2sequence
|
|
|
|
\ output>array [ ] 2sequence ;
|
|
|
|
|
2009-08-19 19:53:44 -04:00
|
|
|
: (define-struct-slot-values-method) ( class -- )
|
|
|
|
[ \ struct-slot-values create-method-in ]
|
|
|
|
[ struct-slot-values-quot ] bi define ;
|
2009-08-12 13:16:43 -04:00
|
|
|
|
2009-08-25 20:04:29 -04:00
|
|
|
: (define-byte-length-method) ( class -- )
|
|
|
|
[ \ byte-length create-method-in ]
|
|
|
|
[ heap-size \ drop swap [ ] 2sequence ] bi define ;
|
|
|
|
|
2009-08-11 22:13:18 -04:00
|
|
|
! Struct as c-type
|
|
|
|
|
2009-08-19 23:50:02 -04:00
|
|
|
: slot>field ( slot -- field )
|
2009-08-20 09:44:19 -04:00
|
|
|
field-spec new swap {
|
|
|
|
[ name>> >>name ]
|
|
|
|
[ offset>> >>offset ]
|
2009-08-25 14:03:43 -04:00
|
|
|
[ c-type>> >>type ]
|
2009-08-20 09:44:19 -04:00
|
|
|
[ name>> reader-word >>reader ]
|
|
|
|
[ name>> writer-word >>writer ]
|
|
|
|
} cleave ;
|
2009-08-19 23:50:02 -04:00
|
|
|
|
|
|
|
: define-struct-for-class ( class -- )
|
|
|
|
[
|
2009-08-20 09:44:19 -04:00
|
|
|
{
|
|
|
|
[ name>> ]
|
|
|
|
[ "struct-size" word-prop ]
|
|
|
|
[ "struct-align" word-prop ]
|
|
|
|
[ struct-slots [ slot>field ] map ]
|
|
|
|
} cleave
|
2009-08-25 20:58:04 -04:00
|
|
|
struct-type (define-struct)
|
2009-08-19 23:50:02 -04:00
|
|
|
] [
|
2009-08-25 14:03:43 -04:00
|
|
|
{
|
|
|
|
[ name>> c-type ]
|
|
|
|
[ (unboxer-quot) >>unboxer-quot ]
|
|
|
|
[ (boxer-quot) >>boxer-quot ]
|
|
|
|
[ >>boxed-class ]
|
|
|
|
} cleave drop
|
2009-08-19 23:50:02 -04:00
|
|
|
] bi ;
|
|
|
|
|
2009-08-11 22:13:18 -04:00
|
|
|
: align-offset ( offset class -- offset' )
|
|
|
|
c-type-align align ;
|
|
|
|
|
|
|
|
: struct-offsets ( slots -- size )
|
|
|
|
0 [
|
2009-08-25 14:03:43 -04:00
|
|
|
[ c-type>> align-offset ] keep
|
|
|
|
[ (>>offset) ] [ c-type>> heap-size + ] 2bi
|
2009-08-11 22:13:18 -04:00
|
|
|
] reduce ;
|
|
|
|
|
2009-08-13 16:55:22 -04:00
|
|
|
: union-struct-offsets ( slots -- size )
|
2009-08-25 14:03:43 -04:00
|
|
|
[ 0 >>offset c-type>> heap-size ] [ max ] map-reduce ;
|
2009-08-13 16:55:22 -04:00
|
|
|
|
2009-08-11 22:13:18 -04:00
|
|
|
: struct-align ( slots -- align )
|
2009-08-25 14:03:43 -04:00
|
|
|
[ c-type>> c-type-align ] [ max ] map-reduce ;
|
2009-08-11 22:13:18 -04:00
|
|
|
|
2009-08-19 23:50:02 -04:00
|
|
|
M: struct-class c-type
|
|
|
|
name>> c-type ;
|
2009-08-11 22:13:18 -04:00
|
|
|
|
|
|
|
M: struct-class c-type-align
|
|
|
|
"struct-align" word-prop ;
|
|
|
|
|
|
|
|
M: struct-class c-type-getter
|
|
|
|
drop [ swap <displaced-alien> ] ;
|
|
|
|
|
|
|
|
M: struct-class c-type-setter
|
|
|
|
[ c-type-getter ] [ c-type-unboxer-quot ] [ heap-size ] tri
|
|
|
|
'[ @ swap @ _ memcpy ] ;
|
|
|
|
|
|
|
|
M: struct-class c-type-boxer-quot
|
2009-08-19 23:50:02 -04:00
|
|
|
(boxer-quot) ;
|
2009-08-11 22:13:18 -04:00
|
|
|
|
|
|
|
M: struct-class c-type-unboxer-quot
|
2009-08-19 23:50:02 -04:00
|
|
|
(unboxer-quot) ;
|
2009-08-11 22:13:18 -04:00
|
|
|
|
|
|
|
M: struct-class heap-size
|
|
|
|
"struct-size" word-prop ;
|
|
|
|
|
|
|
|
! class definition
|
|
|
|
|
2009-08-26 22:27:12 -04:00
|
|
|
: make-struct-prototype ( class -- prototype )
|
2009-08-12 09:15:46 -04:00
|
|
|
[ heap-size <byte-array> ]
|
2009-08-12 10:01:32 -04:00
|
|
|
[ memory>struct ]
|
2009-08-19 19:53:44 -04:00
|
|
|
[ struct-slots ] tri
|
2009-08-12 09:15:46 -04:00
|
|
|
[
|
|
|
|
[ initial>> ]
|
|
|
|
[ (writer-quot) ] bi
|
|
|
|
over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
|
|
|
|
] each ;
|
2009-08-11 22:13:18 -04:00
|
|
|
|
2009-08-25 20:04:29 -04:00
|
|
|
: (struct-methods) ( class -- )
|
|
|
|
[ (define-struct-slot-values-method) ]
|
|
|
|
[ (define-byte-length-method) ] bi ;
|
|
|
|
|
2009-08-13 16:55:22 -04:00
|
|
|
: (struct-word-props) ( class slots size align -- )
|
2009-08-11 22:13:18 -04:00
|
|
|
[
|
2009-08-19 21:21:57 -04:00
|
|
|
[ "struct-slots" set-word-prop ]
|
2009-08-11 22:13:18 -04:00
|
|
|
[ define-accessors ] 2bi
|
|
|
|
]
|
|
|
|
[ "struct-size" set-word-prop ]
|
2009-08-13 16:55:22 -04:00
|
|
|
[ "struct-align" set-word-prop ] tri-curry*
|
|
|
|
[ tri ] 3curry
|
2009-08-26 22:27:12 -04:00
|
|
|
[ dup make-struct-prototype "prototype" set-word-prop ]
|
2009-08-25 20:04:29 -04:00
|
|
|
[ (struct-methods) ] tri ;
|
2009-08-11 22:13:18 -04:00
|
|
|
|
|
|
|
: check-struct-slots ( slots -- )
|
2009-08-25 14:03:43 -04:00
|
|
|
[ c-type>> c-type drop ] each ;
|
2009-08-11 22:13:18 -04:00
|
|
|
|
2009-08-13 16:55:22 -04:00
|
|
|
: (define-struct-class) ( class slots offsets-quot -- )
|
2009-08-26 23:37:59 -04:00
|
|
|
[
|
|
|
|
[ struct-must-have-slots ]
|
|
|
|
[ drop struct f define-tuple-class ] if-empty
|
|
|
|
]
|
2009-08-20 09:44:19 -04:00
|
|
|
swap '[
|
2009-08-11 22:13:18 -04:00
|
|
|
make-slots dup
|
2009-08-13 16:55:22 -04:00
|
|
|
[ check-struct-slots ] _ [ struct-align [ align ] keep ] tri
|
|
|
|
(struct-word-props)
|
2009-08-19 23:50:02 -04:00
|
|
|
]
|
|
|
|
[ drop define-struct-for-class ] 2tri ; inline
|
2009-08-13 16:55:22 -04:00
|
|
|
|
|
|
|
: define-struct-class ( class slots -- )
|
|
|
|
[ struct-offsets ] (define-struct-class) ;
|
|
|
|
|
|
|
|
: define-union-struct-class ( class slots -- )
|
|
|
|
[ union-struct-offsets ] (define-struct-class) ;
|
2009-08-11 22:13:18 -04:00
|
|
|
|
2009-08-25 14:03:43 -04:00
|
|
|
ERROR: invalid-struct-slot token ;
|
|
|
|
|
|
|
|
: struct-slot-class ( c-type -- class' )
|
2009-08-25 14:18:20 -04:00
|
|
|
c-type c-type-boxed-class
|
2009-08-25 14:03:43 -04:00
|
|
|
dup \ byte-array = [ drop \ c-ptr ] when ;
|
|
|
|
|
2009-08-27 22:39:43 -04:00
|
|
|
: scan-c-type ( -- c-type )
|
|
|
|
scan dup "{" = [ drop \ } parse-until >array ] when ;
|
|
|
|
|
2009-08-25 14:03:43 -04:00
|
|
|
: parse-struct-slot ( -- slot )
|
|
|
|
struct-slot-spec new
|
|
|
|
scan >>name
|
2009-08-27 22:39:43 -04:00
|
|
|
scan-c-type [ >>c-type ] [ struct-slot-class >>class ] bi
|
2009-08-25 14:03:43 -04:00
|
|
|
\ } parse-until [ dup empty? ] [ peel-off-attributes ] until drop ;
|
|
|
|
|
|
|
|
: parse-struct-slots ( slots -- slots' more? )
|
|
|
|
scan {
|
|
|
|
{ ";" [ f ] }
|
|
|
|
{ "{" [ parse-struct-slot over push t ] }
|
|
|
|
[ invalid-struct-slot ]
|
|
|
|
} case ;
|
|
|
|
|
2009-08-11 22:13:18 -04:00
|
|
|
: parse-struct-definition ( -- class slots )
|
2009-08-25 14:03:43 -04:00
|
|
|
CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array ;
|
2009-08-11 22:13:18 -04:00
|
|
|
|
|
|
|
SYNTAX: STRUCT:
|
|
|
|
parse-struct-definition define-struct-class ;
|
2009-08-13 16:55:22 -04:00
|
|
|
SYNTAX: UNION-STRUCT:
|
|
|
|
parse-struct-definition define-union-struct-class ;
|
2009-08-11 22:13:18 -04:00
|
|
|
|
2009-08-26 18:19:30 -04:00
|
|
|
SYNTAX: S{
|
|
|
|
scan-word dup struct-slots parse-tuple-literal-slots parsed ;
|
|
|
|
|
2009-08-29 22:04:19 -04:00
|
|
|
: scan-c-type` ( -- c-type/param )
|
|
|
|
scan dup "{" = [ drop \ } parse-until >array ] [ >string-param ] if ;
|
|
|
|
|
|
|
|
:: parse-struct-slot` ( accum -- accum )
|
|
|
|
scan-string-param :> name
|
|
|
|
scan-c-type` :> c-type
|
|
|
|
\ } parse-until :> attributes
|
|
|
|
accum {
|
|
|
|
\ struct-slot-spec new
|
|
|
|
name >>name
|
|
|
|
c-type [ >>c-type ] [ struct-slot-class >>class ] bi
|
|
|
|
attributes [ dup empty? ] [ peel-off-attributes ] until drop
|
|
|
|
over push
|
|
|
|
} over push-all ;
|
|
|
|
|
|
|
|
: parse-struct-slots` ( accum -- accum more? )
|
|
|
|
scan {
|
|
|
|
{ ";" [ f ] }
|
|
|
|
{ "{" [ parse-struct-slot` t ] }
|
|
|
|
[ invalid-struct-slot ]
|
|
|
|
} case ;
|
|
|
|
|
|
|
|
FUNCTOR-SYNTAX: STRUCT:
|
|
|
|
scan-param parsed
|
|
|
|
[ 8 <vector> ] over push-all
|
|
|
|
[ parse-struct-slots` ] [ ] while
|
|
|
|
[ >array define-struct-class ] over push-all ;
|
|
|
|
|
2009-08-12 10:37:09 -04:00
|
|
|
USING: vocabs vocabs.loader ;
|
|
|
|
|
|
|
|
"prettyprint" vocab [ "classes.struct.prettyprint" require ] when
|