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 ;
10 SYMBOL: current-library
12 : parse-c-type-name ( name -- word )
13 dup search [ ] [ no-word ] ?if ;
15 : parse-array-type ( name -- dims c-type )
17 [ [ "]" ?tail drop parse-word ] map ] dip ;
19 : (parse-c-type) ( string -- type )
21 { [ dup "void" = ] [ drop void ] }
22 { [ CHAR: ] over member? ] [ parse-array-type parse-c-type-name prefix ] }
23 { [ "*" ?tail ] [ (parse-c-type) <pointer> ] }
24 { [ dup search ] [ parse-c-type-name ] }
25 [ dup search [ ] [ no-word ] ?if ]
28 : valid-c-type? ( c-type -- ? )
29 { [ array? ] [ c-type-word? ] [ pointer? ] [ void? ] } 1|| ;
31 : parse-c-type ( string -- type )
32 (parse-c-type) dup valid-c-type? [ no-c-type ] unless ;
34 : scan-c-type ( -- c-type )
36 { [ dup "{" = ] [ drop \ } parse-until >array ] }
37 { [ dup "pointer:" = ] [ drop scan-c-type <pointer> ] }
41 : reset-c-type ( word -- )
42 dup "struct-size" word-prop
43 [ dup [ forget-class ] [ { "struct-size" } reset-props ] bi ] when
50 ERROR: *-in-c-type-name name ;
52 : validate-c-type-name ( name -- name )
54 [ *-in-c-type-name ] when ;
56 : (CREATE-C-TYPE) ( word -- word )
57 validate-c-type-name current-vocab create {
64 : CREATE-C-TYPE ( -- word )
65 scan (CREATE-C-TYPE) ;
68 GENERIC: return-type-name ( type -- name )
70 M: object return-type-name drop "void" ;
71 M: word return-type-name name>> ;
72 M: pointer return-type-name to>> return-type-name CHAR: * suffix ;
74 : parse-pointers ( type name -- type' name' )
76 [ [ <pointer> ] dip parse-pointers ] when ;
78 : next-enum-member ( members name value -- members value' )
79 [ 2array suffix! ] [ 1 + ] bi ;
83 : define-enum-member ( name value -- )
84 [ create-in ] [ define-constant ] bi* ;
86 : define-enum-members ( members -- )
87 [ first2 define-enum-member ] each ;
89 : parse-enum-member ( members name value -- members value' )
91 [ 2drop scan scan-object next-enum-member "}" expect ]
92 [ next-enum-member ] if ;
94 : parse-enum-members ( members counter -- members )
96 [ swap parse-enum-member parse-enum-members ] [ 2drop ] if ;
98 : define-enum ( word members -- )
99 [ [ int swap typedef ] when ] [ define-enum-members ] bi* ;
101 : scan-function-name ( -- return function )
102 scan-c-type scan parse-pointers ;
104 :: (scan-c-args) ( end-marker types names -- )
106 type-str end-marker = [
107 type-str { "(" ")" } member? [
108 type-str parse-c-type :> type
109 scan "," ?tail drop :> name
110 type name parse-pointers :> ( type' name' )
111 type' types push name' names push
113 end-marker types names (scan-c-args)
116 : scan-c-args ( end-marker -- types names )
117 V{ } clone V{ } clone [ (scan-c-args) ] 2keep [ >array ] bi@ ;
119 : function-quot ( return library function types -- quot )
120 '[ _ _ _ _ alien-invoke ] ;
122 : function-effect ( names return -- effect )
123 [ { } ] [ return-type-name 1array ] if-void <effect> ;
125 : create-function ( name -- word )
126 create-in dup reset-generic ;
128 :: (make-function) ( return function library types names -- quot effect )
129 return library function types function-quot
130 names return function-effect ;
132 :: make-function ( return function library types names -- word quot effect )
133 function create-function
134 return function library types names (make-function) ;
136 : (FUNCTION:) ( -- return function library types names )
137 scan-function-name current-library get ";" scan-c-args ;
139 : callback-quot ( return types abi -- quot )
140 '[ [ _ _ _ ] dip alien-callback ] ;
142 :: make-callback-type ( lib return type-name types names -- word quot effect )
143 type-name current-vocab create :> type-word
144 type-word [ reset-generic ] [ reset-c-type ] bi
145 void* type-word typedef
146 type-word names return function-effect "callback-effect" set-word-prop
147 type-word lib "callback-library" set-word-prop
148 type-word return types lib library-abi callback-quot (( quot -- alien )) ;
150 : (CALLBACK:) ( -- word quot effect )
152 scan-function-name ";" scan-c-args make-callback-type ;
154 PREDICATE: alien-function-alias-word < word
157 [ last \ alien-invoke eq? ]
160 PREDICATE: alien-function-word < alien-function-alias-word
161 [ def>> third ] [ name>> ] bi = ;
163 PREDICATE: alien-callback-type-word < typedef-word
164 "callback-effect" word-prop ;
166 : global-quot ( type word -- quot )
167 name>> current-library get '[ _ _ address-of 0 ]
168 swap c-type-getter-boxer append ;
170 : define-global ( type word -- )
171 [ nip ] [ global-quot ] 2bi (( -- value )) define-declared ;