]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/parser/parser.factor
Merge branch 'master' of http://factorcode.org/git/factor into native-image-loader
[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 alien.enums ;
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-token {
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 : define-enum-value ( class value -- )
79     enum>number "enum-value" set-word-prop ;
80
81 : next-enum-member ( members name value -- members value' )
82     [ define-enum-value ]
83     [ [ 2array suffix! ] [ enum>number 1 + ] bi ] 2bi ;
84
85 : parse-enum-name ( -- name )
86     scan (CREATE-C-TYPE) dup save-location ;
87
88 : parse-enum-base-type ( -- base-type token )
89     scan dup "<" =
90     [ drop scan-object scan ]
91     [ [ int ] dip ] if ;
92
93 : parse-enum-member ( members name value -- members value' )
94     over "{" =
95     [ 2drop scan create-in scan-object next-enum-member "}" expect ]
96     [ [ create-in ] dip next-enum-member ] if ;
97
98 : parse-enum-members ( members counter token -- members )
99     dup ";" = not
100     [ swap parse-enum-member scan parse-enum-members ] [ 2drop ] if ;
101
102 PRIVATE>
103
104 : parse-enum ( -- name base-type members )
105     parse-enum-name
106     parse-enum-base-type
107     [ V{ } clone 0 ] dip parse-enum-members ;
108
109 : scan-function-name ( -- return function )
110     scan-c-type scan parse-pointers ;
111
112 :: (scan-c-args) ( end-marker types names -- )
113     scan :> type-str
114     type-str end-marker = [
115         type-str { "(" ")" } member? [
116             type-str parse-c-type :> type
117             scan "," ?tail drop :> name
118             type name parse-pointers :> ( type' name' )
119             type' types push name' names push
120         ] unless
121         end-marker types names (scan-c-args)
122     ] unless ;
123
124 : scan-c-args ( end-marker -- types names )
125     V{ } clone V{ } clone [ (scan-c-args) ] 2keep [ >array ] bi@ ;
126
127 : function-quot ( return library function types -- quot )
128     '[ _ _ _ _ alien-invoke ] ;
129
130 : function-effect ( names return -- effect )
131     [ { } ] [ return-type-name 1array ] if-void <effect> ;
132
133 : create-function ( name -- word )
134     create-in dup reset-generic ;
135
136 :: (make-function) ( return function library types names -- quot effect )
137     return library function types function-quot
138     names return function-effect ;
139
140 :: make-function ( return function library types names -- word quot effect )
141     function create-function
142     return function library types names (make-function) ;
143
144 : (FUNCTION:) ( -- return function library types names )
145     scan-function-name current-library get ";" scan-c-args ;
146
147 : callback-quot ( return types abi -- quot )
148     '[ [ _ _ _ ] dip alien-callback ] ;
149
150 :: make-callback-type ( lib return type-name types names -- word quot effect )
151     type-name current-vocab create :> type-word 
152     type-word [ reset-generic ] [ reset-c-type ] bi
153     void* type-word typedef
154     type-word names return function-effect "callback-effect" set-word-prop
155     type-word lib "callback-library" set-word-prop
156     type-word return types lib library-abi callback-quot (( quot -- alien )) ;
157
158 : (CALLBACK:) ( -- word quot effect )
159     current-library get
160     scan-function-name ";" scan-c-args make-callback-type ;
161
162 PREDICATE: alien-function-alias-word < word
163     def>> {
164         [ length 5 = ]
165         [ last \ alien-invoke eq? ]
166     } 1&& ;
167
168 PREDICATE: alien-function-word < alien-function-alias-word
169     [ def>> third ] [ name>> ] bi = ;
170
171 PREDICATE: alien-callback-type-word < typedef-word
172     "callback-effect" word-prop ;
173
174 : global-quot ( type word -- quot )
175     swap [ name>> current-library get ] dip
176     '[ _ _ address-of 0 _ alien-value ] ;
177
178 : define-global ( type word -- )
179     [ nip ] [ global-quot ] 2bi (( -- value )) define-declared ;