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