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 { [ dup search ] [ parse-c-type-name ] }
22 { [ "**" ?tail ] [ drop void* ] }
23 { [ "*" ?tail ] [ parse-c-type-name resolve-pointer-type ] }
24 [ dup search [ ] [ no-word ] ?if ]
27 : valid-c-type? ( c-type -- ? )
28 { [ array? ] [ c-type-name? ] [ void? ] } 1|| ;
30 : parse-c-type ( string -- type )
31 (parse-c-type) dup valid-c-type? [ no-c-type ] unless ;
33 : scan-c-type ( -- c-type )
35 [ drop \ } parse-until >array ]
38 : reset-c-type ( word -- )
39 dup "struct-size" word-prop
40 [ dup [ forget-class ] [ { "struct-size" } reset-props ] bi ] when
48 : CREATE-C-TYPE ( -- word )
49 scan current-vocab create {
56 : normalize-c-arg ( type name -- type' name' )
59 [ CHAR: * = ] trim-head
60 [ length - CHAR: * <array> append ] keep
62 [ parse-c-type ] dip ;
64 : parse-arglist ( parameters return -- types effect )
66 2 group [ first2 normalize-c-arg 2array ] map
67 unzip [ "," ?tail drop ] map
69 [ dup "void" = [ drop { } ] [ 1array ] if ]
72 : function-quot ( return library function types -- quot )
73 '[ _ _ _ _ alien-invoke ] ;
75 :: make-function ( return library function parameters -- word quot effect )
76 return function normalize-c-arg :> ( return-c-type function )
77 function create-in dup reset-generic
78 return-c-type library function
79 parameters return parse-arglist [ function-quot ] dip ;
81 : parse-arg-tokens ( -- tokens )
82 ";" parse-tokens [ "()" subseq? not ] filter ;
84 : (FUNCTION:) ( -- word quot effect )
85 scan "c-library" get scan parse-arg-tokens make-function ;
87 : define-function ( return library function parameters -- )
88 make-function define-declared ;
90 : callback-quot ( return types abi -- quot )
91 [ [ ] 3curry dip alien-callback ] 3curry ;
93 : library-abi ( lib -- abi )
94 library [ abi>> ] [ "cdecl" ] if* ;
96 :: make-callback-type ( lib return! type-name! parameters -- word quot effect )
97 return type-name normalize-c-arg type-name! return!
98 type-name current-vocab create :> type-word
99 type-word [ reset-generic ] [ reset-c-type ] bi
100 void* type-word typedef
101 parameters return parse-arglist :> ( types callback-effect )
102 type-word callback-effect "callback-effect" set-word-prop
103 type-word lib "callback-library" set-word-prop
104 type-word return types lib library-abi callback-quot (( quot -- alien )) ;
106 : (CALLBACK:) ( -- word quot effect )
108 scan scan parse-arg-tokens make-callback-type ;
110 PREDICATE: alien-function-word < word
113 [ last \ alien-invoke eq? ]
116 PREDICATE: alien-callback-type-word < typedef-word
117 "callback-effect" word-prop ;