]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/parser/parser.factor
Merge branch 'master' into c-type-words
[factor.git] / basis / alien / parser / parser.factor
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 arrays assocs
4 combinators combinators.short-circuit effects grouping
5 kernel parser sequences splitting words fry locals lexer
6 namespaces summary math vocabs.parser ;
7 IN: alien.parser
8
9 : parse-c-type-name ( name -- word/string )
10     [ search ] keep or ;
11
12 : parse-c-type ( string -- array )
13     {
14         { [ dup "void" =            ] [ drop void ] }
15         { [ CHAR: ] over member?    ] [ parse-array-type parse-c-type-name prefix ] }
16         { [ dup search c-type-word? ] [ parse-c-type-name ] }
17         { [ dup c-types get at      ] [ ] }
18         { [ "*" ?tail               ] [ parse-c-type-name resolve-pointer-type ] }
19         [ no-c-type ]
20     } cond ;
21
22 : scan-c-type ( -- c-type )
23     scan dup "{" =
24     [ drop \ } parse-until >array ]
25     [ parse-c-type ] if ; 
26
27 : normalize-c-arg ( type name -- type' name' )
28     [ length ]
29     [
30         [ CHAR: * = ] trim-head
31         [ length - CHAR: * <array> append ] keep
32     ] bi
33     [ parse-c-type ] dip ;
34
35 : parse-arglist ( parameters return -- types effect )
36     [
37         2 group [ first2 normalize-c-arg 2array ] map
38         unzip [ "," ?tail drop ] map
39     ]
40     [ [ { } ] [ 1array ] if-void ]
41     bi* <effect> ;
42
43 : function-quot ( return library function types -- quot )
44     '[ _ _ _ _ alien-invoke ] ;
45
46 :: make-function ( return! library function! parameters -- word quot effect )
47     return function normalize-c-arg function! return!
48     function create-in dup reset-generic
49     return library function
50     parameters return parse-arglist [ function-quot ] dip ;
51
52 : (FUNCTION:) ( -- word quot effect )
53     scan "c-library" get scan ";" parse-tokens
54     [ "()" subseq? not ] filter
55     make-function ;
56
57 : define-function ( return library function parameters -- )
58     make-function define-declared ;
59
60 PREDICATE: alien-function-word < word
61     def>> {
62         [ length 5 = ]
63         [ last \ alien-invoke eq? ]
64     } 1&& ;