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-c-type) ( string -- type )
15 { [ dup "void" = ] [ drop void ] }
16 { [ CHAR: ] over member? ] [ parse-array-type parse-c-type-name prefix ] }
17 { [ dup search ] [ parse-c-type-name ] }
18 { [ "**" ?tail ] [ drop void* ] }
19 { [ "*" ?tail ] [ parse-c-type-name resolve-pointer-type ] }
20 [ dup search [ ] [ no-word ] ?if ]
23 : valid-c-type? ( c-type -- ? )
24 { [ array? ] [ c-type-name? ] [ void? ] } 1|| ;
26 : parse-c-type ( string -- type )
27 (parse-c-type) dup valid-c-type? [ no-c-type ] unless ;
29 : scan-c-type ( -- c-type )
31 [ drop \ } parse-until >array ]
34 : reset-c-type ( word -- )
35 dup "struct-size" word-prop
36 [ dup [ forget-class ] [ { "struct-size" } reset-props ] bi ] when
44 : CREATE-C-TYPE ( -- word )
45 scan current-vocab create {
52 : normalize-c-arg ( type name -- type' name' )
55 [ CHAR: * = ] trim-head
56 [ length - CHAR: * <array> append ] keep
58 [ parse-c-type ] dip ;
60 : parse-arglist ( parameters return -- types effect )
62 2 group [ first2 normalize-c-arg 2array ] map
63 unzip [ "," ?tail drop ] map
65 [ [ { } ] [ 1array ] if-void ]
68 : function-quot ( return library function types -- quot )
69 '[ _ _ _ _ alien-invoke ] ;
71 :: make-function ( return! library function! parameters -- word quot effect )
72 return function normalize-c-arg function! return!
73 function create-in dup reset-generic
74 return library function
75 parameters return parse-arglist [ function-quot ] dip ;
77 : parse-arg-tokens ( -- tokens )
78 ";" parse-tokens [ "()" subseq? not ] filter ;
80 : (FUNCTION:) ( -- word quot effect )
81 scan "c-library" get scan parse-arg-tokens make-function ;
83 : define-function ( return library function parameters -- )
84 make-function define-declared ;
86 : callback-quot ( return types abi -- quot )
87 [ [ ] 3curry dip alien-callback ] 3curry ;
89 : library-abi ( lib -- abi )
90 library [ abi>> ] [ "cdecl" ] if* ;
92 :: make-callback-type ( lib return! type-name! parameters -- word quot effect )
93 return type-name normalize-c-arg type-name! return!
94 type-name current-vocab create :> type-word
95 type-word [ reset-generic ] [ reset-c-type ] bi
96 void* type-word typedef
97 parameters return parse-arglist :> callback-effect :> types
98 type-word callback-effect "callback-effect" set-word-prop
99 type-word lib "callback-library" set-word-prop
100 type-word return types lib library-abi callback-quot (( quot -- alien )) ;
102 : (CALLBACK:) ( -- word quot effect )
104 scan scan parse-arg-tokens make-callback-type ;
106 PREDICATE: alien-function-word < word
109 [ last \ alien-invoke eq? ]
112 PREDICATE: alien-callback-type-word < typedef-word
113 "callback-effect" word-prop ;