1 ! Copyright (C) 2009, 2010 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.arrays alien.c-types alien.data
4 alien.parser arrays byte-arrays combinators effects.parser fry
5 generalizations grouping kernel make math sequences
6 sequences.generalizations sequences.private ;
7 FROM: alien.arrays => array-length ;
12 : <displaced-direct-array> ( displacement bytes length type -- direct-array )
13 [ <displaced-alien> ] 2dip <c-direct-array> ; inline
17 { count fixnum read-only }
19 { bytes c-ptr read-only }
20 { byte-length fixnum read-only }
21 { iter-length fixnum read-only }
22 { iter-count fixnum read-only } ;
24 M: data-map-param length
27 M: data-map-param nth-unsafe
29 [ iter-length>> * >fixnum ]
33 } cleave <displaced-direct-array> ; inline
35 INSTANCE: data-map-param immutable-sequence
37 : c-type-count ( in/out -- c-type count )
38 dup array? [ unclip swap array-length >fixnum ] [ 1 ] if ; inline
40 : c-type-iter-length ( c-type count -- iter-length )
41 swap heap-size * >fixnum ; inline
43 : [>c-type-param] ( c-type count -- quot )
44 2dup c-type-iter-length '[
54 : [>object-param] ( class count -- quot )
57 : [>param] ( type -- quot )
58 c-type-count over c-type-name?
59 [ [>c-type-param] ] [ [>object-param] ] if ;
61 MACRO: >param ( in -- quot: ( array -- param ) )
64 : [alloc-c-type-param] ( c-type count -- quot )
65 2dup c-type-iter-length dup '[
68 _ * >fixnum [ (byte-array) dup ] keep
74 : [alloc-object-param] ( type count -- quot )
75 "Factor sequences as data-map outputs not supported" throw ;
77 : [alloc-param] ( type -- quot )
78 c-type-count over c-type-name?
79 [ [alloc-c-type-param] ] [ [alloc-object-param] ] if ;
81 MACRO: alloc-param ( out -- quot: ( len -- param ) )
84 MACRO: unpack-params ( ins -- quot )
85 [ c-type-count nip '[ _ firstn-unsafe ] ] map '[ _ spread ] ;
87 MACRO: pack-params ( outs -- quot )
88 [ ] [ c-type-count nip dup
89 [ [ ndip POSTPONE: _ ] dip set-firstn ] 3curry ] reduce
90 fry [ call ] compose ;
92 :: [data-map] ( ins outs param-quot -- quot )
95 #ins #outs + :> #params
101 [ ins , \ unpack-params , \ @ , ] [ ] make ,
102 #outs , \ ndip , outs , \ pack-params ,
105 ] [ ] make , #outs , \ nkeep ,
106 [ orig>> ] , #outs , \ napply ,
107 ] [ ] make fry \ call suffix ;
109 MACRO: data-map ( ins outs -- quot )
112 [ [ '[ _ >param ] ] map '[ _ spread ] ]
113 [ length dup '[ _ ndup _ nmin-length ] compose ] bi
115 [ [ '[ _ alloc-param ] ] map '[ _ cleave ] ] bi* compose
118 MACRO: data-map! ( ins outs -- quot )
119 2dup append [ '[ _ >param ] ] map '[ _ spread ] [data-map] ;
121 : parse-data-map-effect ( accum -- accum )
123 [ in>> [ (parse-c-type) ] map suffix! ]
124 [ out>> [ (parse-c-type) ] map suffix! ] bi ;
129 parse-data-map-effect \ data-map suffix! ;
132 parse-data-map-effect \ data-map! suffix! ;