1 ! Copyright (C) 2008, 2010 Slava Pestov, Doug Coleman, Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.enums alien.libraries
4 arrays classes classes.parser combinators combinators.short-circuit
5 compiler.units effects fry kernel lexer locals math namespaces parser
6 sequences splitting summary vocabs.parser words ;
9 SYMBOL: current-library
13 ERROR: bad-array-type ;
15 : parse-array-type ( name -- c-type )
17 [ [ "]" ?tail [ bad-array-type ] unless parse-datum ] map ]
21 : (parse-c-type) ( string -- type )
23 { [ "*" ?tail ] [ (parse-c-type) <pointer> ] }
24 { [ CHAR: ] over member? ] [ parse-array-type ] }
25 { [ dup search ] [ parse-word ] }
29 : c-array? ( c-type -- ? )
30 { [ array? ] [ first { [ c-type-word? ] [ pointer? ] } 1|| ] } 1&& ;
32 : valid-c-type? ( c-type -- ? )
33 { [ c-array? ] [ c-type-word? ] [ pointer? ] [ void? ] } 1|| ;
35 : parse-c-type ( string -- type )
36 (parse-c-type) dup valid-c-type? [ no-c-type ] unless ;
38 : scan-c-type ( -- c-type )
40 { [ dup "{" = ] [ drop \ } parse-until >array ] }
41 { [ dup "pointer:" = ] [ drop scan-c-type <pointer> ] }
45 : reset-c-type ( word -- )
46 dup "struct-size" word-prop [
47 dup [ forget-class ] [ "struct-size" remove-word-prop ] bi
55 ERROR: *-in-c-type-name name ;
57 M: *-in-c-type-name summary
59 "Cannot define a C type “"
60 "” that ends with an asterisk (*)" surround ;
62 : validate-c-type-name ( name -- name )
64 [ *-in-c-type-name ] when ;
66 : (CREATE-C-TYPE) ( name -- word )
67 validate-c-type-name current-vocab create-word {
75 : CREATE-C-TYPE ( -- word )
76 scan-token (CREATE-C-TYPE) ;
80 : parse-pointers ( type name -- type' name' )
82 [ [ <pointer> ] dip parse-pointers ] when ;
84 : next-enum-member ( members name value -- members value' )
86 [ [ 2array suffix! ] [ enum>number 1 + ] bi ] 2bi ;
88 : parse-enum-name ( -- word )
89 CREATE-C-TYPE dup save-location ;
91 : parse-enum-base-type ( -- base-type token )
93 [ drop scan-object scan-token ]
96 : parse-enum-member ( members name value -- members value' )
98 [ 2drop scan-token create-class-in scan-object next-enum-member "}" expect ]
99 [ [ create-class-in ] dip next-enum-member ] if ;
101 : parse-enum-members ( members counter token -- members )
103 [ swap parse-enum-member scan-token parse-enum-members ] [ 2drop ] if ;
107 : parse-enum ( -- name base-type members )
110 [ V{ } clone 0 ] dip parse-enum-members ;
112 : scan-function-name ( -- return function )
113 scan-c-type scan-token parse-pointers ;
115 :: scan-c-args ( -- types names )
118 "(" expect scan-token [ dup ")" = ] [
120 scan-token "," ?tail drop
121 parse-pointers [ types push ] [ names push ] bi*
123 ] until drop types names [ >array ] bi@ ;
125 : function-effect ( names return -- effect )
126 [ { } ] [ c-type-string 1array ] if-void <effect> ;
128 : create-function ( name -- word )
129 create-word-in dup reset-generic ;
131 :: (make-function) ( return function library types names -- quot effect )
132 return library function types '[ _ _ _ _ f alien-invoke ]
133 names return function-effect ;
135 :: make-function ( return function library types names -- word quot effect )
136 function create-function
137 return function library types names (make-function) ;
139 : (FUNCTION:) ( -- return function library types names )
140 scan-function-name current-library get scan-c-args ;
142 : callback-quot ( return types abi -- quot )
143 '[ [ _ _ _ ] dip alien-callback ] ;
145 :: make-callback-type ( return function library types names -- word quot effect )
146 function create-function :> type-word
147 void* type-word typedef
148 type-word names return function-effect "callback-effect" set-word-prop
149 type-word library "callback-library" set-word-prop
150 type-word return types library library-abi callback-quot ( quot -- alien ) ;
152 : (CALLBACK:) ( -- word quot effect )
153 (FUNCTION:) make-callback-type ;
155 : global-quot ( type word -- quot )
156 swap [ name>> current-library get ] dip
157 '[ _ _ address-of 0 _ alien-value ] ;
159 : set-global-quot ( type word -- quot )
160 swap [ name>> current-library get ] dip
161 '[ _ _ address-of 0 _ set-alien-value ] ;
163 : define-global-getter ( type word -- )
164 [ nip ] [ global-quot ] 2bi ( -- value ) define-declared ;
166 : define-global-setter ( type word -- )
167 [ nip name>> "set-" prepend create-word-in ]
168 [ set-global-quot ] 2bi ( obj -- ) define-declared ;
170 : define-global ( type word -- )
171 [ define-global-getter ] [ define-global-setter ] 2bi ;