]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/parser/parser.factor
Merge branch 'master' of git://factorcode.org/git/factor into bags
[factor.git] / basis / alien / parser / parser.factor
1 ! Copyright (C) 2008, 2010 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         { [ "*" ?tail            ] [ (parse-c-type) <pointer> ] }
22         { [ dup search           ] [ parse-c-type-name ] }
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 {
34         { [ dup "{" = ] [ drop \ } parse-until >array ] }
35         { [ dup "pointer:" = ] [ drop scan-c-type <pointer> ] }
36         [ parse-c-type ]
37     } cond ; 
38
39 : reset-c-type ( word -- )
40     dup "struct-size" word-prop
41     [ dup [ forget-class ] [ { "struct-size" } reset-props ] bi ] when
42     {
43         "c-type"
44         "callback-effect"
45         "callback-library"
46     } reset-props ;
47
48 ERROR: *-in-c-type-name name ;
49
50 : validate-c-type-name ( name -- name )
51     dup "*" tail?
52     [ *-in-c-type-name ] when ;
53
54 : CREATE-C-TYPE ( -- word )
55     scan validate-c-type-name current-vocab create {
56         [ fake-definition ]
57         [ set-word ]
58         [ reset-c-type ]
59         [ ]
60     } cleave ;
61
62 <PRIVATE
63 GENERIC: return-type-name ( type -- name )
64
65 M: object return-type-name drop "void" ;
66 M: word return-type-name name>> ;
67 M: pointer return-type-name to>> return-type-name CHAR: * suffix ;
68
69 : parse-pointers ( type name -- type' name' )
70     "*" ?head
71     [ [ <pointer> ] dip parse-pointers ] when ;
72
73 PRIVATE>
74
75 : scan-function-name ( -- return function )
76     scan-c-type scan parse-pointers ;
77
78 :: (scan-c-args) ( end-marker types names -- )
79     scan :> type-str
80     type-str end-marker = [
81         type-str { "(" ")" } member? [
82             type-str parse-c-type :> type
83             scan "," ?tail drop :> name
84             type name parse-pointers :> ( type' name' )
85             type' types push name' names push
86         ] unless
87         end-marker types names (scan-c-args)
88     ] unless ;
89
90 : scan-c-args ( end-marker -- types names )
91     V{ } clone V{ } clone [ (scan-c-args) ] 2keep [ >array ] bi@ ;
92
93 : function-quot ( return library function types -- quot )
94     '[ _ _ _ _ alien-invoke ] ;
95
96 : function-effect ( names return -- effect )
97     [ { } ] [ return-type-name 1array ] if-void <effect> ;
98
99 :: make-function ( return function library types names -- word quot effect )
100     function create-in dup reset-generic
101     return library function types function-quot
102     names return function-effect ;
103
104 : (FUNCTION:) ( -- word quot effect )
105     scan-function-name "c-library" get ";" scan-c-args make-function ;
106
107 : callback-quot ( return types abi -- quot )
108     '[ [ _ _ _ ] dip alien-callback ] ;
109
110 :: make-callback-type ( lib return type-name types names -- word quot effect )
111     type-name current-vocab create :> type-word 
112     type-word [ reset-generic ] [ reset-c-type ] bi
113     void* type-word typedef
114     type-word names return function-effect "callback-effect" set-word-prop
115     type-word lib "callback-library" set-word-prop
116     type-word return types lib library-abi callback-quot (( quot -- alien )) ;
117
118 : (CALLBACK:) ( -- word quot effect )
119     "c-library" get
120     scan-function-name ";" scan-c-args make-callback-type ;
121
122 PREDICATE: alien-function-word < word
123     def>> {
124         [ length 5 = ]
125         [ last \ alien-invoke eq? ]
126     } 1&& ;
127
128 PREDICATE: alien-callback-type-word < typedef-word
129     "callback-effect" word-prop ;