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