1 ! Copyright (C) 2008, 2010 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.parser
4 alien.libraries arrays assocs classes combinators
5 combinators.short-circuit compiler.units effects grouping
6 kernel parser sequences splitting words fry locals lexer
7 namespaces summary math vocabs.parser ;
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 )
55 scan validate-c-type-name current-vocab create {
63 GENERIC: return-type-name ( type -- name )
65 M: object return-type-name drop "void" ;
66 M: word return-type-name name>> ;
67 M: pointer return-type-name to>> return-type-name CHAR: * suffix ;
69 : parse-pointers ( type name -- type' name' )
71 [ [ <pointer> ] dip parse-pointers ] when ;
75 : scan-function-name ( -- return function )
76 scan-c-type scan parse-pointers ;
78 :: (scan-c-args) ( end-marker types names -- )
80 type-str end-marker = [
81 type-str { "(" ")" } member? [
82 type-str parse-c-type :> type
83 scan "," ?tail drop :> name
84 type name parse-pointers :> ( type' name' )
85 type' types push name' names push
87 end-marker types names (scan-c-args)
90 : scan-c-args ( end-marker -- types names )
91 V{ } clone V{ } clone [ (scan-c-args) ] 2keep [ >array ] bi@ ;
93 : function-quot ( return library function types -- quot )
94 '[ _ _ _ _ alien-invoke ] ;
96 : function-effect ( names return -- effect )
97 [ { } ] [ return-type-name 1array ] if-void <effect> ;
99 :: make-function ( return function library types names -- word quot effect )
100 function create-in dup reset-generic
101 return library function types function-quot
102 names return function-effect ;
104 : (FUNCTION:) ( -- word quot effect )
105 scan-function-name "c-library" get ";" scan-c-args make-function ;
107 : callback-quot ( return types abi -- quot )
108 '[ [ _ _ _ ] dip alien-callback ] ;
110 :: make-callback-type ( lib return type-name types names -- word quot effect )
111 type-name current-vocab create :> type-word
112 type-word [ reset-generic ] [ reset-c-type ] bi
113 void* type-word typedef
114 type-word names return function-effect "callback-effect" set-word-prop
115 type-word lib "callback-library" set-word-prop
116 type-word return types lib library-abi callback-quot (( quot -- alien )) ;
118 : (CALLBACK:) ( -- word quot effect )
120 scan-function-name ";" scan-c-args make-callback-type ;
122 PREDICATE: alien-function-word < word
125 [ last \ alien-invoke eq? ]
128 PREDICATE: alien-callback-type-word < typedef-word
129 "callback-effect" word-prop ;