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