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.libraries arrays
4 assocs classes combinators combinators.short-circuit
5 compiler.units effects grouping kernel parser sequences
6 splitting words fry locals lexer namespaces summary math
7 vocabs.parser words.constant classes.parser ;
10 SYMBOL: current-library
12 : parse-c-type-name ( name -- word )
13 dup search [ ] [ no-word ] ?if ;
17 ERROR: bad-array-type ;
19 : parse-array-type ( name -- c-type )
21 [ [ "]" ?tail [ bad-array-type ] unless parse-word ] map ]
25 : (parse-c-type) ( string -- type )
27 { [ "*" ?tail ] [ (parse-c-type) <pointer> ] }
28 { [ CHAR: ] over member? ] [ parse-array-type ] }
29 { [ dup search ] [ parse-c-type-name ] }
30 [ dup search [ ] [ no-word ] ?if ]
33 : c-array? ( c-type -- ? )
34 { [ array? ] [ first { [ c-type-word? ] [ pointer? ] } 1|| ] } 1&& ;
36 : valid-c-type? ( c-type -- ? )
37 { [ c-array? ] [ c-type-word? ] [ pointer? ] [ void? ] } 1|| ;
39 : parse-c-type ( string -- type )
40 (parse-c-type) dup valid-c-type? [ no-c-type ] unless ;
42 : scan-c-type ( -- c-type )
44 { [ dup "{" = ] [ drop \ } parse-until >array ] }
45 { [ dup "pointer:" = ] [ drop scan-c-type <pointer> ] }
49 : reset-c-type ( word -- )
50 dup "struct-size" word-prop
51 [ dup [ forget-class ] [ { "struct-size" } reset-props ] bi ] when
58 ERROR: *-in-c-type-name name ;
60 : validate-c-type-name ( name -- name )
62 [ *-in-c-type-name ] when ;
64 : (CREATE-C-TYPE) ( word -- word )
65 validate-c-type-name current-vocab create {
72 : CREATE-C-TYPE ( -- word )
73 scan (CREATE-C-TYPE) ;
76 GENERIC: return-type-name ( type -- name )
78 M: object return-type-name drop "void" ;
79 M: word return-type-name name>> ;
80 M: pointer return-type-name to>> return-type-name CHAR: * suffix ;
82 : parse-pointers ( type name -- type' name' )
84 [ [ <pointer> ] dip parse-pointers ] when ;
86 : next-enum-member ( members name value -- members value' )
87 [ 2array suffix! ] [ 1 + ] bi ;
89 : parse-enum-name ( -- name )
90 scan (CREATE-C-TYPE) dup save-location ;
92 : parse-enum-base-type ( -- base-type token )
94 [ drop scan-object scan ]
97 : parse-enum-member ( members name value -- members value' )
99 [ 2drop scan create-class-in scan-object next-enum-member "}" expect ]
100 [ [ create-class-in ] dip next-enum-member ] if ;
102 : parse-enum-members ( members counter token -- members )
104 [ swap parse-enum-member scan parse-enum-members ] [ 2drop ] if ;
108 : parse-enum ( -- name base-type members )
111 [ V{ } clone 0 ] dip parse-enum-members ;
113 : scan-function-name ( -- return function )
114 scan-c-type scan parse-pointers ;
116 :: (scan-c-args) ( end-marker types names -- )
118 type-str end-marker = [
119 type-str { "(" ")" } member? [
120 type-str parse-c-type :> type
121 scan "," ?tail drop :> name
122 type name parse-pointers :> ( type' name' )
123 type' types push name' names push
125 end-marker types names (scan-c-args)
128 : scan-c-args ( end-marker -- types names )
129 V{ } clone V{ } clone [ (scan-c-args) ] 2keep [ >array ] bi@ ;
131 : function-quot ( return library function types -- quot )
132 '[ _ _ _ _ alien-invoke ] ;
134 : function-effect ( names return -- effect )
135 [ { } ] [ return-type-name 1array ] if-void <effect> ;
137 : create-function ( name -- word )
138 create-in dup reset-generic ;
140 :: (make-function) ( return function library types names -- quot effect )
141 return library function types function-quot
142 names return function-effect ;
144 :: make-function ( return function library types names -- word quot effect )
145 function create-function
146 return function library types names (make-function) ;
148 : (FUNCTION:) ( -- return function library types names )
149 scan-function-name current-library get ";" scan-c-args ;
151 : callback-quot ( return types abi -- quot )
152 '[ [ _ _ _ ] dip alien-callback ] ;
154 :: make-callback-type ( lib return type-name types names -- word quot effect )
155 type-name current-vocab create :> type-word
156 type-word [ reset-generic ] [ reset-c-type ] bi
157 void* type-word typedef
158 type-word names return function-effect "callback-effect" set-word-prop
159 type-word lib "callback-library" set-word-prop
160 type-word return types lib library-abi callback-quot (( quot -- alien )) ;
162 : (CALLBACK:) ( -- word quot effect )
164 scan-function-name ";" scan-c-args make-callback-type ;
166 PREDICATE: alien-function-alias-word < word
169 [ last \ alien-invoke eq? ]
172 PREDICATE: alien-function-word < alien-function-alias-word
173 [ def>> third ] [ name>> ] bi = ;
175 PREDICATE: alien-callback-type-word < typedef-word
176 "callback-effect" word-prop ;
178 : global-quot ( type word -- quot )
179 swap [ name>> current-library get ] dip
180 '[ _ _ address-of 0 _ alien-value ] ;
182 : define-global ( type word -- )
183 [ nip ] [ global-quot ] 2bi (( -- value )) define-declared ;