1 ! Copyright (C) 2008, 2009 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 { [ dup search ] [ parse-c-type-name ] }
22 { [ "*" ?tail ] [ (parse-c-type) <pointer> ] }
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
49 : CREATE-C-TYPE ( -- word )
50 scan current-vocab create {
57 : normalize-c-arg ( type name -- type' name' )
60 [ CHAR: * = ] trim-head
61 [ length - CHAR: * <array> append ] keep
63 [ parse-c-type ] dip ;
65 : parse-arglist ( parameters return -- types effect )
67 2 group [ first2 normalize-c-arg 2array ] map
68 unzip [ "," ?tail drop ] map
70 [ [ { } ] [ 1array ] if-void ]
73 : function-quot ( return library function types -- quot )
74 '[ _ _ _ _ alien-invoke ] ;
76 :: make-function ( return! library function! parameters -- word quot effect )
77 return function normalize-c-arg function! return!
78 function create-in dup reset-generic
79 return library function
80 parameters return parse-arglist [ function-quot ] dip ;
82 : parse-arg-tokens ( -- tokens )
83 ";" parse-tokens [ "()" subseq? not ] filter ;
85 : (FUNCTION:) ( -- word quot effect )
86 scan "c-library" get scan parse-arg-tokens make-function ;
88 : define-function ( return library function parameters -- )
89 make-function define-declared ;
91 : callback-quot ( return types abi -- quot )
92 [ [ ] 3curry dip alien-callback ] 3curry ;
94 : library-abi ( lib -- abi )
95 library [ abi>> ] [ "cdecl" ] if* ;
97 :: make-callback-type ( lib return! type-name! parameters -- word quot effect )
98 return type-name normalize-c-arg type-name! return!
99 type-name current-vocab create :> type-word
100 type-word [ reset-generic ] [ reset-c-type ] bi
101 void* type-word typedef
102 parameters return parse-arglist :> ( types callback-effect )
103 type-word callback-effect "callback-effect" set-word-prop
104 type-word lib "callback-library" set-word-prop
105 type-word return types lib library-abi callback-quot (( quot -- alien )) ;
107 : (CALLBACK:) ( -- word quot effect )
109 scan scan parse-arg-tokens make-callback-type ;
111 PREDICATE: alien-function-word < word
114 [ last \ alien-invoke eq? ]
117 PREDICATE: alien-callback-type-word < typedef-word
118 "callback-effect" word-prop ;