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
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 ;
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 ;
73 : parse-arglist ( parameters return -- types effect )
75 2 group [ first2 normalize-c-arg 2array ] map
76 unzip [ "," ?tail drop ] map
78 [ [ { } ] [ return-type-name 1array ] if-void ]
81 : function-quot ( return library function types -- quot )
82 '[ _ _ _ _ alien-invoke ] ;
84 :: make-function ( return library function parameters -- word quot effect )
85 return function normalize-c-arg :> ( return function )
86 function create-in dup reset-generic
87 return library function
88 parameters return parse-arglist [ function-quot ] dip ;
90 : parse-arg-tokens ( -- tokens )
91 ";" parse-tokens [ "()" subseq? not ] filter ;
93 : (FUNCTION:) ( -- word quot effect )
94 scan "c-library" get scan parse-arg-tokens make-function ;
96 : define-function ( return library function parameters -- )
97 make-function define-declared ;
99 : callback-quot ( return types abi -- quot )
100 '[ [ _ _ _ ] dip alien-callback ] ;
102 :: make-callback-type ( lib return type-name parameters -- word quot effect )
103 return type-name normalize-c-arg :> ( return type-name )
104 type-name current-vocab create :> type-word
105 type-word [ reset-generic ] [ reset-c-type ] bi
106 void* type-word typedef
107 parameters return parse-arglist :> ( types callback-effect )
108 type-word callback-effect "callback-effect" set-word-prop
109 type-word lib "callback-library" set-word-prop
110 type-word return types lib library-abi callback-quot (( quot -- alien )) ;
112 : (CALLBACK:) ( -- word quot effect )
114 scan scan parse-arg-tokens make-callback-type ;
116 PREDICATE: alien-function-word < word
119 [ last \ alien-invoke eq? ]
122 PREDICATE: alien-callback-type-word < typedef-word
123 "callback-effect" word-prop ;