]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/parser/parser.factor
Switch to https urls
[factor.git] / basis / alien / parser / parser.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov, Doug Coleman, Joe Groff.
2 ! See https://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 kernel lexer
6 math namespaces parser sequences splitting summary vocabs.parser
7 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 [ 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 M: *-in-c-type-name summary
59     name>>
60     "Cannot define a C type “"
61     "” that ends with an asterisk (*)" surround ;
62
63 : validate-c-type-name ( name -- name )
64     dup "*" tail?
65     [ *-in-c-type-name ] when ;
66
67 : (CREATE-C-TYPE) ( name -- word )
68     validate-c-type-name current-vocab create-word {
69         [ fake-definition ]
70         [ set-last-word ]
71         [ reset-generic ]
72         [ reset-c-type ]
73         [ ]
74     } cleave ;
75
76 : CREATE-C-TYPE ( -- word )
77     scan-token (CREATE-C-TYPE) ;
78
79 <PRIVATE
80
81 : parse-pointers ( type name -- type' name' )
82     "*" ?head
83     [ [ <pointer> ] dip parse-pointers ] when ;
84
85 : next-enum-member ( members name value -- members value' )
86     [ define-enum-value ]
87     [ [ 2array suffix! ] [ enum>number 1 + ] bi ] 2bi ;
88
89 : parse-enum-name ( -- word )
90     CREATE-C-TYPE dup save-location ;
91
92 : parse-enum-base-type ( -- base-type token )
93     scan-token dup "<" =
94     [ drop scan-object scan-token ]
95     [ [ int ] dip ] if ;
96
97 : parse-enum-member ( members name value -- members value' )
98     over "{" =
99     [ 2drop scan-token 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-token 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-token parse-pointers ;
115
116 :: scan-c-args ( -- types names )
117     V{ } clone :> types
118     V{ } clone :> names
119     "(" expect scan-token [ dup ")" = ] [
120         parse-c-type
121         scan-token "," ?tail drop
122         parse-pointers [ types push ] [ names push ] bi*
123         scan-token
124     ] until drop types names [ >array ] bi@ ;
125
126 : function-effect ( names return -- effect )
127     [ { } ] [ c-type-string 1array ] if-void <effect> ;
128
129 : create-function ( name -- word )
130     create-word-in dup reset-generic ;
131
132 :: (make-function) ( return function library types names -- quot effect )
133     return library function types '[ _ _ _ _ f alien-invoke ]
134     names return function-effect ;
135
136 :: make-function ( return function library types names -- word quot effect )
137     function create-function
138     return function library types names (make-function) ;
139
140 : (FUNCTION:) ( -- return function library types names )
141     scan-function-name current-library get scan-c-args ;
142
143 : callback-quot ( return types abi -- quot )
144     '[ [ _ _ _ ] dip alien-callback ] ;
145
146 :: make-callback-type ( return function library types names -- word quot effect )
147     function create-function :> type-word
148     void* type-word typedef
149     type-word names return function-effect "callback-effect" set-word-prop
150     type-word library "callback-library" set-word-prop
151     type-word return types library library-abi callback-quot ( quot -- alien ) ;
152
153 : (CALLBACK:) ( -- word quot effect )
154     (FUNCTION:) make-callback-type ;
155
156 : global-quot ( type word -- quot )
157     swap [ name>> current-library get ] dip
158     '[ _ _ address-of 0 _ alien-value ] ;
159
160 : set-global-quot ( type word -- quot )
161     swap [ name>> current-library get ] dip
162     '[ _ _ address-of 0 _ set-alien-value ] ;
163
164 : define-global-getter ( type word -- )
165     [ nip ] [ global-quot ] 2bi ( -- value ) define-declared ;
166
167 : define-global-setter ( type word -- )
168     [ nip name>> "set-" prepend create-word-in ]
169     [ set-global-quot ] 2bi ( obj -- ) define-declared ;
170
171 : define-global ( type word -- )
172     [ define-global-getter ] [ define-global-setter ] 2bi ;