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