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 ;
80 : define-enum-member ( word-string value -- next-value )
81 [ create-in ] dip [ define-constant ] keep 1 + ;
83 : parse-enum-member ( word-string value -- next-value )
85 [ 2drop scan scan-object define-enum-member "}" expect ]
86 [ define-enum-member ] if ;
88 : parse-enum-members ( counter -- )
90 [ swap parse-enum-member parse-enum-members ] [ 2drop ] if ;
92 : scan-function-name ( -- return function )
93 scan-c-type scan parse-pointers ;
95 :: (scan-c-args) ( end-marker types names -- )
97 type-str end-marker = [
98 type-str { "(" ")" } member? [
99 type-str parse-c-type :> type
100 scan "," ?tail drop :> name
101 type name parse-pointers :> ( type' name' )
102 type' types push name' names push
104 end-marker types names (scan-c-args)
107 : scan-c-args ( end-marker -- types names )
108 V{ } clone V{ } clone [ (scan-c-args) ] 2keep [ >array ] bi@ ;
110 : function-quot ( return library function types -- quot )
111 '[ _ _ _ _ alien-invoke ] ;
113 : function-effect ( names return -- effect )
114 [ { } ] [ return-type-name 1array ] if-void <effect> ;
116 :: make-function ( return function library types names -- word quot effect )
117 function create-in dup reset-generic
118 return library function types function-quot
119 names return function-effect ;
121 : (FUNCTION:) ( -- word quot effect )
122 scan-function-name current-library get ";" scan-c-args make-function ;
124 : callback-quot ( return types abi -- quot )
125 '[ [ _ _ _ ] dip alien-callback ] ;
127 :: make-callback-type ( lib return type-name types names -- word quot effect )
128 type-name current-vocab create :> type-word
129 type-word [ reset-generic ] [ reset-c-type ] bi
130 void* type-word typedef
131 type-word names return function-effect "callback-effect" set-word-prop
132 type-word lib "callback-library" set-word-prop
133 type-word return types lib library-abi callback-quot (( quot -- alien )) ;
135 : (CALLBACK:) ( -- word quot effect )
137 scan-function-name ";" scan-c-args make-callback-type ;
139 PREDICATE: alien-function-word < word
142 [ last \ alien-invoke eq? ]
145 PREDICATE: alien-callback-type-word < typedef-word
146 "callback-effect" word-prop ;
148 : global-quot ( type word -- quot )
149 name>> current-library get '[ _ _ address-of 0 ]
150 swap c-type-getter-boxer append ;
152 : define-global ( type word -- )
153 [ nip ] [ global-quot ] 2bi (( -- value )) define-declared ;