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 : parse-c-type-name ( name -- word )
11 dup search [ ] [ no-word ] ?if ;
13 : parse-array-type ( name -- dims c-type )
15 [ [ "]" ?tail drop parse-word ] map ] dip ;
17 : (parse-c-type) ( string -- type )
19 { [ dup "void" = ] [ drop void ] }
20 { [ CHAR: ] over member? ] [ parse-array-type parse-c-type-name prefix ] }
21 { [ "*" ?tail ] [ (parse-c-type) <pointer> ] }
22 { [ dup search ] [ parse-c-type-name ] }
23 [ dup search [ ] [ no-word ] ?if ]
26 : valid-c-type? ( c-type -- ? )
27 { [ array? ] [ c-type-word? ] [ pointer? ] [ void? ] } 1|| ;
29 : parse-c-type ( string -- type )
30 (parse-c-type) dup valid-c-type? [ no-c-type ] unless ;
32 : scan-c-type ( -- c-type )
34 { [ dup "{" = ] [ drop \ } parse-until >array ] }
35 { [ dup "pointer:" = ] [ drop scan-c-type <pointer> ] }
39 : reset-c-type ( word -- )
40 dup "struct-size" word-prop
41 [ dup [ forget-class ] [ { "struct-size" } reset-props ] bi ] when
48 ERROR: *-in-c-type-name name ;
50 : validate-c-type-name ( name -- name )
52 [ *-in-c-type-name ] when ;
54 : (CREATE-C-TYPE) ( word -- word )
55 validate-c-type-name current-vocab create {
62 : CREATE-C-TYPE ( -- word )
63 scan (CREATE-C-TYPE) ;
66 GENERIC: return-type-name ( type -- name )
68 M: object return-type-name drop "void" ;
69 M: word return-type-name name>> ;
70 M: pointer return-type-name to>> return-type-name CHAR: * suffix ;
72 : parse-pointers ( type name -- type' name' )
74 [ [ <pointer> ] dip parse-pointers ] when ;
78 : define-enum-member ( word-string value -- next-value )
79 [ create-in ] dip [ define-constant ] keep 1 + ;
81 : parse-enum-member ( word-string value -- next-value )
83 [ 2drop scan scan-object define-enum-member "}" expect ]
84 [ define-enum-member ] if ;
86 : parse-enum-members ( counter -- )
88 [ swap parse-enum-member parse-enum-members ] [ 2drop ] if ;
90 : scan-function-name ( -- return function )
91 scan-c-type scan parse-pointers ;
93 :: (scan-c-args) ( end-marker types names -- )
95 type-str end-marker = [
96 type-str { "(" ")" } member? [
97 type-str parse-c-type :> type
98 scan "," ?tail drop :> name
99 type name parse-pointers :> ( type' name' )
100 type' types push name' names push
102 end-marker types names (scan-c-args)
105 : scan-c-args ( end-marker -- types names )
106 V{ } clone V{ } clone [ (scan-c-args) ] 2keep [ >array ] bi@ ;
108 : function-quot ( return library function types -- quot )
109 '[ _ _ _ _ alien-invoke ] ;
111 : function-effect ( names return -- effect )
112 [ { } ] [ return-type-name 1array ] if-void <effect> ;
114 :: make-function ( return function library types names -- word quot effect )
115 function create-in dup reset-generic
116 return library function types function-quot
117 names return function-effect ;
119 : (FUNCTION:) ( -- word quot effect )
120 scan-function-name "c-library" get ";" scan-c-args make-function ;
122 : callback-quot ( return types abi -- quot )
123 '[ [ _ _ _ ] dip alien-callback ] ;
125 :: make-callback-type ( lib return type-name types names -- word quot effect )
126 type-name current-vocab create :> type-word
127 type-word [ reset-generic ] [ reset-c-type ] bi
128 void* type-word typedef
129 type-word names return function-effect "callback-effect" set-word-prop
130 type-word lib "callback-library" set-word-prop
131 type-word return types lib library-abi callback-quot (( quot -- alien )) ;
133 : (CALLBACK:) ( -- word quot effect )
135 scan-function-name ";" scan-c-args make-callback-type ;
137 PREDICATE: alien-function-word < word
140 [ last \ alien-invoke eq? ]
143 PREDICATE: alien-callback-type-word < typedef-word
144 "callback-effect" word-prop ;