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