]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/parser/parser.factor
factor: Make source files/resources 644 instead of 755.
[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 combinators.short-circuit
5 compiler.units effects fry kernel lexer locals math namespaces parser
6 sequences splitting summary vocabs.parser words ;
7 IN: alien.parser
8
9 SYMBOL: current-library
10
11 DEFER: (parse-c-type)
12
13 ERROR: bad-array-type ;
14
15 : parse-array-type ( name -- c-type )
16     "[" split unclip
17     [ [ "]" ?tail [ bad-array-type ] unless parse-datum ] map ]
18     [ (parse-c-type) ]
19     bi* prefix ;
20
21 : (parse-c-type) ( string -- type )
22     {
23         { [ "*" ?tail ] [ (parse-c-type) <pointer> ] }
24         { [ CHAR: ] over member? ] [ parse-array-type ] }
25         { [ dup search ] [ parse-word ] }
26         [ parse-word ]
27     } cond ;
28
29 : c-array? ( c-type -- ? )
30     { [ array? ] [ first { [ c-type-word? ] [ pointer? ] } 1|| ] } 1&& ;
31
32 : valid-c-type? ( c-type -- ? )
33     { [ c-array? ] [ c-type-word? ] [ pointer? ] [ void? ] } 1|| ;
34
35 : parse-c-type ( string -- type )
36     (parse-c-type) dup valid-c-type? [ no-c-type ] unless ;
37
38 : scan-c-type ( -- c-type )
39     scan-token {
40         { [ dup "{" = ] [ drop \ } parse-until >array ] }
41         { [ dup "pointer:" = ] [ drop scan-c-type <pointer> ] }
42         [ parse-c-type ]
43     } cond ;
44
45 : reset-c-type ( word -- )
46     dup "struct-size" word-prop [
47         dup [ forget-class ] [ "struct-size" remove-word-prop ] bi
48     ] when
49     {
50         "c-type"
51         "callback-effect"
52         "callback-library"
53     } remove-word-props ;
54
55 ERROR: *-in-c-type-name name ;
56
57 M: *-in-c-type-name summary
58     name>>
59     "Cannot define a C type “"
60     "” that ends with an asterisk (*)" surround ;
61
62 : validate-c-type-name ( name -- name )
63     dup "*" tail?
64     [ *-in-c-type-name ] when ;
65
66 : (CREATE-C-TYPE) ( name -- word )
67     validate-c-type-name current-vocab create-word {
68         [ fake-definition ]
69         [ set-last-word ]
70         [ reset-generic ]
71         [ reset-c-type ]
72         [ ]
73     } cleave ;
74
75 : CREATE-C-TYPE ( -- word )
76     scan-token (CREATE-C-TYPE) ;
77
78 <PRIVATE
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 ( -- word )
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-effect ( names return -- effect )
126     [ { } ] [ c-type-string 1array ] if-void <effect> ;
127
128 : create-function ( name -- word )
129     create-word-in dup reset-generic ;
130
131 :: (make-function) ( return function library types names -- quot effect )
132     return library function types '[ _ _ _ _ f alien-invoke ]
133     names return function-effect ;
134
135 :: make-function ( return function library types names -- word quot effect )
136     function create-function
137     return function library types names (make-function) ;
138
139 : (FUNCTION:) ( -- return function library types names )
140     scan-function-name current-library get scan-c-args ;
141
142 : callback-quot ( return types abi -- quot )
143     '[ [ _ _ _ ] dip alien-callback ] ;
144
145 :: make-callback-type ( return function library types names -- word quot effect )
146     function create-function :> type-word
147     void* type-word typedef
148     type-word names return function-effect "callback-effect" set-word-prop
149     type-word library "callback-library" set-word-prop
150     type-word return types library library-abi callback-quot ( quot -- alien ) ;
151
152 : (CALLBACK:) ( -- word quot effect )
153     (FUNCTION:) make-callback-type ;
154
155 : global-quot ( type word -- quot )
156     swap [ name>> current-library get ] dip
157     '[ _ _ address-of 0 _ alien-value ] ;
158
159 : set-global-quot ( type word -- quot )
160     swap [ name>> current-library get ] dip
161     '[ _ _ address-of 0 _ set-alien-value ] ;
162
163 : define-global-getter ( type word -- )
164     [ nip ] [ global-quot ] 2bi ( -- value ) define-declared ;
165
166 : define-global-setter ( type word -- )
167     [ nip name>> "set-" prepend create-word-in ]
168     [ set-global-quot ] 2bi ( obj -- ) define-declared ;
169
170 : define-global ( type word -- )
171     [ define-global-getter ] [ define-global-setter ] 2bi ;