]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/parser/parser.factor
Merge branch 'dcn' of git://factorcode.org/git/factor into dcn
[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 : normalize-c-arg ( type name -- type' name' )
9     [ length ]
10     [
11         [ CHAR: * = ] trim-head
12         [ length - CHAR: * <array> append ] keep
13     ] bi ;
14
15 : parse-arglist ( parameters return -- types effect )
16     [
17         2 group [ first2 normalize-c-arg 2array ] map
18         unzip [ "," ?tail drop ] map
19     ]
20     [ [ { } ] [ 1array ] if-void ]
21     bi* <effect> ;
22
23 : function-quot ( return library function types -- quot )
24     '[ _ _ _ _ alien-invoke ] ;
25
26 :: make-function ( return! library function! parameters -- word quot effect )
27     return function normalize-c-arg function! return!
28     function create-in dup reset-generic
29     return library function
30     parameters return parse-arglist [ function-quot ] dip ;
31
32 : (FUNCTION:) ( -- word quot effect )
33     scan "c-library" get scan ";" parse-tokens
34     [ "()" subseq? not ] filter
35     make-function ;
36
37 : define-function ( return library function parameters -- )
38     make-function define-declared ;