]> 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: alien alien.c-types arrays assocs effects grouping kernel
4 parser sequences splitting words fry locals lexer namespaces
5 summary math ;
6 IN: alien.parser
7
8 : scan-c-type ( -- c-type )
9     scan dup "{" =
10     [ drop \ } parse-until >array ]
11     [ parse-c-type ] if ; 
12
13 : normalize-c-arg ( type name -- type' name' )
14     [ length ]
15     [
16         [ CHAR: * = ] trim-head
17         [ length - CHAR: * <array> append ] keep
18     ] bi
19     [ parse-c-type ] dip ;
20
21 : parse-arglist ( parameters return -- types effect )
22     [
23         2 group [ first2 normalize-c-arg 2array ] map
24         unzip [ "," ?tail drop ] map
25     ]
26     [ [ { } ] [ 1array ] if-void ]
27     bi* <effect> ;
28
29 : function-quot ( return library function types -- quot )
30     '[ _ _ _ _ alien-invoke ] ;
31
32 :: make-function ( return! library function! parameters -- word quot effect )
33     return function normalize-c-arg function! return!
34     function create-in dup reset-generic
35     return library function
36     parameters return parse-arglist [ function-quot ] dip ;
37
38 : (FUNCTION:) ( -- word quot effect )
39     scan "c-library" get scan ";" parse-tokens
40     [ "()" subseq? not ] filter
41     make-function ;
42
43 : define-function ( return library function parameters -- )
44     make-function define-declared ;