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 alien.enums ;
10 SYMBOL: current-library
12 : parse-c-type-name ( name -- word )
13 dup search [ ] [ no-word ] ?if ;
15 : parse-array-type ( name -- dims c-type )
17 [ [ "]" ?tail drop parse-word ] map ] dip ;
19 : (parse-c-type) ( string -- type )
21 { [ dup "void" = ] [ drop void ] }
22 { [ CHAR: ] over member? ] [ parse-array-type parse-c-type-name prefix ] }
23 { [ "*" ?tail ] [ (parse-c-type) <pointer> ] }
24 { [ dup search ] [ parse-c-type-name ] }
25 [ dup search [ ] [ no-word ] ?if ]
28 : valid-c-type? ( c-type -- ? )
29 { [ array? ] [ c-type-word? ] [ pointer? ] [ void? ] } 1|| ;
31 : parse-c-type ( string -- type )
32 (parse-c-type) dup valid-c-type? [ no-c-type ] unless ;
34 : scan-c-type ( -- c-type )
36 { [ dup "{" = ] [ drop \ } parse-until >array ] }
37 { [ dup "pointer:" = ] [ drop scan-c-type <pointer> ] }
41 : reset-c-type ( word -- )
42 dup "struct-size" word-prop
43 [ dup [ forget-class ] [ { "struct-size" } reset-props ] bi ] when
50 ERROR: *-in-c-type-name name ;
52 : validate-c-type-name ( name -- name )
54 [ *-in-c-type-name ] when ;
56 : (CREATE-C-TYPE) ( word -- word )
57 validate-c-type-name current-vocab create {
64 : CREATE-C-TYPE ( -- word )
65 scan (CREATE-C-TYPE) ;
68 GENERIC: return-type-name ( type -- name )
70 M: object return-type-name drop "void" ;
71 M: word return-type-name name>> ;
72 M: pointer return-type-name to>> return-type-name CHAR: * suffix ;
74 : parse-pointers ( type name -- type' name' )
76 [ [ <pointer> ] dip parse-pointers ] when ;
78 : define-enum-value ( class value -- )
79 enum>number "enum-value" set-word-prop ;
81 : next-enum-member ( members name value -- members value' )
83 [ [ 2array suffix! ] [ enum>number 1 + ] bi ] 2bi ;
85 : parse-enum-name ( -- name )
86 scan (CREATE-C-TYPE) dup save-location ;
88 : parse-enum-base-type ( -- base-type token )
90 [ drop scan-object scan ]
93 : parse-enum-member ( members name value -- members value' )
95 [ 2drop scan create-in scan-object next-enum-member "}" expect ]
96 [ [ create-in ] dip next-enum-member ] if ;
98 : parse-enum-members ( members counter token -- members )
100 [ swap parse-enum-member scan parse-enum-members ] [ 2drop ] if ;
104 : parse-enum ( -- name base-type members )
107 [ V{ } clone 0 ] dip parse-enum-members ;
109 : scan-function-name ( -- return function )
110 scan-c-type scan parse-pointers ;
112 :: (scan-c-args) ( end-marker types names -- )
114 type-str end-marker = [
115 type-str { "(" ")" } member? [
116 type-str parse-c-type :> type
117 scan "," ?tail drop :> name
118 type name parse-pointers :> ( type' name' )
119 type' types push name' names push
121 end-marker types names (scan-c-args)
124 : scan-c-args ( end-marker -- types names )
125 V{ } clone V{ } clone [ (scan-c-args) ] 2keep [ >array ] bi@ ;
127 : function-quot ( return library function types -- quot )
128 '[ _ _ _ _ alien-invoke ] ;
130 : function-effect ( names return -- effect )
131 [ { } ] [ return-type-name 1array ] if-void <effect> ;
133 : create-function ( name -- word )
134 create-in dup reset-generic ;
136 :: (make-function) ( return function library types names -- quot effect )
137 return library function types function-quot
138 names return function-effect ;
140 :: make-function ( return function library types names -- word quot effect )
141 function create-function
142 return function library types names (make-function) ;
144 : (FUNCTION:) ( -- return function library types names )
145 scan-function-name current-library get ";" scan-c-args ;
147 : callback-quot ( return types abi -- quot )
148 '[ [ _ _ _ ] dip alien-callback ] ;
150 :: make-callback-type ( lib return type-name types names -- word quot effect )
151 type-name current-vocab create :> type-word
152 type-word [ reset-generic ] [ reset-c-type ] bi
153 void* type-word typedef
154 type-word names return function-effect "callback-effect" set-word-prop
155 type-word lib "callback-library" set-word-prop
156 type-word return types lib library-abi callback-quot (( quot -- alien )) ;
158 : (CALLBACK:) ( -- word quot effect )
160 scan-function-name ";" scan-c-args make-callback-type ;
162 PREDICATE: alien-function-alias-word < word
165 [ last \ alien-invoke eq? ]
168 PREDICATE: alien-function-word < alien-function-alias-word
169 [ def>> third ] [ name>> ] bi = ;
171 PREDICATE: alien-callback-type-word < typedef-word
172 "callback-effect" word-prop ;
174 : global-quot ( type word -- quot )
175 swap [ name>> current-library get ] dip
176 '[ _ _ address-of 0 _ alien-value ] ;
178 : define-global ( type word -- )
179 [ nip ] [ global-quot ] 2bi (( -- value )) define-declared ;