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