]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/parser/parser.factor
Remove ENUM: f and replace uses with CONSTANTs.
[factor.git] / basis / alien / parser / parser.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov, Doug Coleman, Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.libraries arrays
4 assocs classes combinators combinators.short-circuit
5 compiler.units effects grouping kernel parser sequences
6 splitting words fry locals lexer namespaces summary math
7 vocabs.parser words.constant ;
8 IN: alien.parser
9
10 SYMBOL: current-library
11
12 : parse-c-type-name ( name -- word )
13     dup search [ ] [ no-word ] ?if ;
14
15 : parse-array-type ( name -- dims c-type )
16     "[" split unclip
17     [ [ "]" ?tail drop parse-word ] map ] dip ;
18
19 : (parse-c-type) ( string -- type )
20     {
21         { [ dup "void" =         ] [ drop void ] }
22         { [ CHAR: ] over member? ] [ parse-array-type parse-c-type-name prefix ] }
23         { [ "*" ?tail            ] [ (parse-c-type) <pointer> ] }
24         { [ dup search           ] [ parse-c-type-name ] }
25         [ dup search [ ] [ no-word ] ?if ]
26     } cond ;
27
28 : valid-c-type? ( c-type -- ? )
29     { [ array? ] [ c-type-word? ] [ pointer? ] [ void? ] } 1|| ;
30
31 : parse-c-type ( string -- type )
32     (parse-c-type) dup valid-c-type? [ no-c-type ] unless ;
33
34 : scan-c-type ( -- c-type )
35     scan {
36         { [ dup "{" = ] [ drop \ } parse-until >array ] }
37         { [ dup "pointer:" = ] [ drop scan-c-type <pointer> ] }
38         [ parse-c-type ]
39     } cond ; 
40
41 : reset-c-type ( word -- )
42     dup "struct-size" word-prop
43     [ dup [ forget-class ] [ { "struct-size" } reset-props ] bi ] when
44     {
45         "c-type"
46         "callback-effect"
47         "callback-library"
48     } reset-props ;
49
50 ERROR: *-in-c-type-name name ;
51
52 : validate-c-type-name ( name -- name )
53     dup "*" tail?
54     [ *-in-c-type-name ] when ;
55
56 : (CREATE-C-TYPE) ( word -- word )
57     validate-c-type-name current-vocab create {
58         [ fake-definition ]
59         [ set-word ]
60         [ reset-c-type ]
61         [ ]
62     } cleave ;
63
64 : CREATE-C-TYPE ( -- word )
65     scan (CREATE-C-TYPE) ;
66
67 <PRIVATE
68 GENERIC: return-type-name ( type -- name )
69
70 M: object return-type-name drop "void" ;
71 M: word return-type-name name>> ;
72 M: pointer return-type-name to>> return-type-name CHAR: * suffix ;
73
74 : parse-pointers ( type name -- type' name' )
75     "*" ?head
76     [ [ <pointer> ] dip parse-pointers ] when ;
77
78 : next-enum-member ( members name value -- members value' )
79     [ 2array suffix! ] [ 1 + ] bi ;
80
81 : parse-enum-name ( -- name )
82     scan (CREATE-C-TYPE) dup save-location ;
83
84 : parse-enum-base-type ( -- base-type token )
85     scan dup "<" =
86     [ drop scan-object scan ]
87     [ [ int ] dip ] if ;
88
89 : parse-enum-member ( members name value -- members value' )
90     over "{" =
91     [ 2drop scan create-in scan-object next-enum-member "}" expect ]
92     [ [ create-in ] dip next-enum-member ] if ;
93
94 : parse-enum-members ( members counter token -- members )
95     dup ";" = not
96     [ swap parse-enum-member scan parse-enum-members ] [ 2drop ] if ;
97
98 PRIVATE>
99
100 : parse-enum ( -- name base-type members )
101     parse-enum-name
102     parse-enum-base-type
103     [ V{ } clone 0 ] dip parse-enum-members ;
104
105 : scan-function-name ( -- return function )
106     scan-c-type scan parse-pointers ;
107
108 :: (scan-c-args) ( end-marker types names -- )
109     scan :> type-str
110     type-str end-marker = [
111         type-str { "(" ")" } member? [
112             type-str parse-c-type :> type
113             scan "," ?tail drop :> name
114             type name parse-pointers :> ( type' name' )
115             type' types push name' names push
116         ] unless
117         end-marker types names (scan-c-args)
118     ] unless ;
119
120 : scan-c-args ( end-marker -- types names )
121     V{ } clone V{ } clone [ (scan-c-args) ] 2keep [ >array ] bi@ ;
122
123 : function-quot ( return library function types -- quot )
124     '[ _ _ _ _ alien-invoke ] ;
125
126 : function-effect ( names return -- effect )
127     [ { } ] [ return-type-name 1array ] if-void <effect> ;
128
129 : create-function ( name -- word )
130     create-in dup reset-generic ;
131
132 :: (make-function) ( return function library types names -- quot effect )
133     return library function types function-quot
134     names return function-effect ;
135
136 :: make-function ( return function library types names -- word quot effect )
137     function create-function
138     return function library types names (make-function) ;
139
140 : (FUNCTION:) ( -- return function library types names )
141     scan-function-name current-library get ";" scan-c-args ;
142
143 : callback-quot ( return types abi -- quot )
144     '[ [ _ _ _ ] dip alien-callback ] ;
145
146 :: make-callback-type ( lib return type-name types names -- word quot effect )
147     type-name current-vocab create :> type-word 
148     type-word [ reset-generic ] [ reset-c-type ] bi
149     void* type-word typedef
150     type-word names return function-effect "callback-effect" set-word-prop
151     type-word lib "callback-library" set-word-prop
152     type-word return types lib library-abi callback-quot (( quot -- alien )) ;
153
154 : (CALLBACK:) ( -- word quot effect )
155     current-library get
156     scan-function-name ";" scan-c-args make-callback-type ;
157
158 PREDICATE: alien-function-alias-word < word
159     def>> {
160         [ length 5 = ]
161         [ last \ alien-invoke eq? ]
162     } 1&& ;
163
164 PREDICATE: alien-function-word < alien-function-alias-word
165     [ def>> third ] [ name>> ] bi = ;
166
167 PREDICATE: alien-callback-type-word < typedef-word
168     "callback-effect" word-prop ;
169
170 : global-quot ( type word -- quot )
171     name>> current-library get '[ _ _ address-of 0 ]
172     swap c-type-getter-boxer append ;
173
174 : define-global ( type word -- )
175     [ nip ] [ global-quot ] 2bi (( -- value )) define-declared ;