]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/parser/parser.factor
hurr
[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 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 ;
8 IN: alien.parser
9
10 : parse-c-type-name ( name -- word )
11     dup search [ ] [ no-word ] ?if ;
12
13 : (parse-c-type) ( string -- type )
14     {
15         { [ dup "void" =         ] [ drop void ] }
16         { [ CHAR: ] over member? ] [ parse-array-type parse-c-type-name prefix ] }
17         { [ dup search           ] [ parse-c-type-name ] }
18         { [ "**" ?tail           ] [ drop void* ] }
19         { [ "*" ?tail            ] [ parse-c-type-name resolve-pointer-type ] }
20         [ dup search [ ] [ no-word ] ?if ]
21     } cond ;
22
23 : valid-c-type? ( c-type -- ? )
24     { [ array? ] [ c-type-name? ] [ void? ] } 1|| ;
25
26 : parse-c-type ( string -- type )
27     (parse-c-type) dup valid-c-type? [ no-c-type ] unless ;
28
29 : scan-c-type ( -- c-type )
30     scan dup "{" =
31     [ drop \ } parse-until >array ]
32     [ parse-c-type ] if ; 
33
34 : reset-c-type ( word -- )
35     dup "struct-size" word-prop
36     [ dup [ forget-class ] [ { "struct-size" } reset-props ] bi ] when
37     {
38         "c-type"
39         "pointer-c-type"
40         "callback-effect"
41         "callback-library"
42     } reset-props ;
43
44 : CREATE-C-TYPE ( -- word )
45     scan current-vocab create {
46         [ fake-definition ]
47         [ set-word ]
48         [ reset-c-type ]
49         [ ]
50     } cleave ;
51
52 : normalize-c-arg ( type name -- type' name' )
53     [ length ]
54     [
55         [ CHAR: * = ] trim-head
56         [ length - CHAR: * <array> append ] keep
57     ] bi
58     [ parse-c-type ] dip ;
59
60 : parse-arglist ( parameters return -- types effect )
61     [
62         2 group [ first2 normalize-c-arg 2array ] map
63         unzip [ "," ?tail drop ] map
64     ]
65     [ [ { } ] [ 1array ] if-void ]
66     bi* <effect> ;
67
68 : function-quot ( return library function types -- quot )
69     '[ _ _ _ _ alien-invoke ] ;
70
71 :: make-function ( return! library function! parameters -- word quot effect )
72     return function normalize-c-arg function! return!
73     function create-in dup reset-generic
74     return library function
75     parameters return parse-arglist [ function-quot ] dip ;
76
77 : parse-arg-tokens ( -- tokens )
78     ";" parse-tokens [ "()" subseq? not ] filter ;
79
80 : (FUNCTION:) ( -- word quot effect )
81     scan "c-library" get scan parse-arg-tokens make-function ;
82
83 : define-function ( return library function parameters -- )
84     make-function define-declared ;
85
86 : callback-quot ( return types abi -- quot )
87     [ [ ] 3curry dip alien-callback ] 3curry ;
88
89 : library-abi ( lib -- abi )
90     library [ abi>> ] [ "cdecl" ] if* ;
91
92 :: make-callback-type ( lib return! type-name! parameters -- word quot effect )
93     return type-name normalize-c-arg type-name! return!
94     type-name current-vocab create :> type-word 
95     type-word [ reset-generic ] [ reset-c-type ] bi
96     void* type-word typedef
97     parameters return parse-arglist :> callback-effect :> types
98     type-word callback-effect "callback-effect" set-word-prop
99     type-word lib "callback-library" set-word-prop
100     type-word return types lib library-abi callback-quot (( quot -- alien )) ;
101
102 : (CALLBACK:) ( -- word quot effect )
103     "c-library" get
104     scan scan parse-arg-tokens make-callback-type ;
105
106 PREDICATE: alien-function-word < word
107     def>> {
108         [ length 5 = ]
109         [ last \ alien-invoke eq? ]
110     } 1&& ;
111
112 PREDICATE: alien-callback-type-word < typedef-word
113     "callback-effect" word-prop ;
114