]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/parser/parser.factor
Eliminate duplicate syntax for stack effects "(" no longer drops and is identical...
[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 alien.enums ;
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 [ 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" } reset-props ] bi ] when
49     {
50         "c-type"
51         "callback-effect"
52         "callback-library"
53     } reset-props ;
54
55 ERROR: *-in-c-type-name name ;
56
57 : validate-c-type-name ( name -- name )
58     dup "*" tail?
59     [ *-in-c-type-name ] when ;
60
61 : (CREATE-C-TYPE) ( word -- word )
62     validate-c-type-name current-vocab create {
63         [ fake-definition ]
64         [ set-word ]
65         [ reset-c-type ]
66         [ ]
67     } cleave ;
68
69 : CREATE-C-TYPE ( -- word )
70     scan-token (CREATE-C-TYPE) ;
71
72 <PRIVATE
73 GENERIC: return-type-name ( type -- name )
74
75 M: object return-type-name drop "void" ;
76 M: word return-type-name name>> ;
77 M: pointer return-type-name to>> return-type-name CHAR: * suffix ;
78
79 : parse-pointers ( type name -- type' name' )
80     "*" ?head
81     [ [ <pointer> ] dip parse-pointers ] when ;
82
83 : next-enum-member ( members name value -- members value' )
84     [ define-enum-value ]
85     [ [ 2array suffix! ] [ enum>number 1 + ] bi ] 2bi ;
86
87 : parse-enum-name ( -- name )
88     CREATE-C-TYPE dup save-location ;
89
90 : parse-enum-base-type ( -- base-type token )
91     scan-token dup "<" =
92     [ drop scan-object scan-token ]
93     [ [ int ] dip ] if ;
94
95 : parse-enum-member ( members name value -- members value' )
96     over "{" =
97     [ 2drop scan-token create-class-in scan-object next-enum-member "}" expect ]
98     [ [ create-class-in ] dip next-enum-member ] if ;
99
100 : parse-enum-members ( members counter token -- members )
101     dup ";" = not
102     [ swap parse-enum-member scan-token parse-enum-members ] [ 2drop ] if ;
103
104 PRIVATE>
105
106 : parse-enum ( -- name base-type members )
107     parse-enum-name
108     parse-enum-base-type
109     [ V{ } clone 0 ] dip parse-enum-members ;
110
111 : scan-function-name ( -- return function )
112     scan-c-type scan-token parse-pointers ;
113
114 :: (scan-c-args) ( end-marker types names -- )
115     scan-token :> type-str
116     type-str end-marker = [
117         type-str { "(" ")" } member? [
118             type-str parse-c-type :> type
119             scan-token "," ?tail drop :> name
120             type name parse-pointers :> ( type' name' )
121             type' types push name' names push
122         ] unless
123         end-marker types names (scan-c-args)
124     ] unless ;
125
126 : scan-c-args ( end-marker -- types names )
127     V{ } clone V{ } clone [ (scan-c-args) ] 2keep [ >array ] bi@ ;
128
129 : function-quot ( return library function types -- quot )
130     '[ _ _ _ _ alien-invoke ] ;
131
132 : function-effect ( names return -- effect )
133     [ { } ] [ return-type-name 1array ] if-void <effect> ;
134
135 : create-function ( name -- word )
136     create-in dup reset-generic ;
137
138 :: (make-function) ( return function library types names -- quot effect )
139     return library function types function-quot
140     names return function-effect ;
141
142 :: make-function ( return function library types names -- word quot effect )
143     function create-function
144     return function library types names (make-function) ;
145
146 : (FUNCTION:) ( -- return function library types names )
147     scan-function-name current-library get ";" scan-c-args ;
148
149 : callback-quot ( return types abi -- quot )
150     '[ [ _ _ _ ] dip alien-callback ] ;
151
152 :: make-callback-type ( lib return type-name types names -- word quot effect )
153     type-name current-vocab create :> type-word 
154     type-word [ reset-generic ] [ reset-c-type ] bi
155     void* type-word typedef
156     type-word names return function-effect "callback-effect" set-word-prop
157     type-word lib "callback-library" set-word-prop
158     type-word return types lib library-abi callback-quot ( quot -- alien ) ;
159
160 : (CALLBACK:) ( -- word quot effect )
161     current-library get
162     scan-function-name ";" scan-c-args make-callback-type ;
163
164 PREDICATE: alien-function-alias-word < word
165     def>> {
166         [ length 5 = ]
167         [ last \ alien-invoke eq? ]
168     } 1&& ;
169
170 PREDICATE: alien-function-word < alien-function-alias-word
171     [ def>> third ] [ name>> ] bi = ;
172
173 PREDICATE: alien-callback-type-word < typedef-word
174     "callback-effect" word-prop ;
175
176 : global-quot ( type word -- quot )
177     swap [ name>> current-library get ] dip
178     '[ _ _ address-of 0 _ alien-value ] ;
179
180 : set-global-quot ( type word -- quot )
181     swap [ name>> current-library get ] dip
182     '[ _ _ address-of 0 _ set-alien-value ] ;
183
184 : define-global-getter ( type word -- )
185     [ nip ] [ global-quot ] 2bi ( -- value ) define-declared ;
186
187 : define-global-setter ( type word -- )
188     [ nip name>> "set-" prepend create-in ]
189     [ set-global-quot ] 2bi ( obj -- ) define-declared ;
190
191 : define-global ( type word -- )
192     [ define-global-getter ] [ define-global-setter ] 2bi ;