]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/parser/parser.factor
Merge branch 'master' of git://factorcode.org/git/factor into bags
[factor.git] / basis / alien / parser / parser.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.parser
4 alien.libraries arrays assocs classes combinators
5 combinators.short-circuit compiler.units effects grouping
6 kernel parser sequences splitting words fry locals lexer
7 namespaces summary math vocabs.parser ;
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 )
55     scan validate-c-type-name current-vocab create {
56         [ fake-definition ]
57         [ set-word ]
58         [ reset-c-type ]
59         [ ]
60     } cleave ;
61
62 : normalize-c-arg ( type name -- type' name' )
63     [ length ]
64     [
65         [ CHAR: * = ] trim-head
66         [ length - CHAR: * <array> append ] keep
67     ] bi
68     [ parse-c-type ] dip ;
69
70 <PRIVATE
71 GENERIC: return-type-name ( type -- name )
72
73 M: object return-type-name drop "void" ;
74 M: word return-type-name name>> ;
75 M: pointer return-type-name to>> return-type-name CHAR: * suffix ;
76 PRIVATE>
77
78 : parse-arglist ( parameters return -- types effect )
79     [
80         2 group [ first2 normalize-c-arg 2array ] map
81         unzip [ "," ?tail drop ] map
82     ]
83     [ [ { } ] [ return-type-name 1array ] if-void ]
84     bi* <effect> ;
85
86 : function-quot ( return library function types -- quot )
87     '[ _ _ _ _ alien-invoke ] ;
88
89 :: make-function ( return library function parameters -- word quot effect )
90     return function normalize-c-arg :> ( return function )
91     function create-in dup reset-generic
92     return library function
93     parameters return parse-arglist [ function-quot ] dip ;
94
95 : parse-arg-tokens ( -- tokens )
96     ";" parse-tokens [ "()" subseq? not ] filter ;
97
98 : (FUNCTION:) ( -- word quot effect )
99     scan "c-library" get scan parse-arg-tokens make-function ;
100
101 : define-function ( return library function parameters -- )
102     make-function define-declared ;
103
104 : callback-quot ( return types abi -- quot )
105     '[ [ _ _ _ ] dip alien-callback ] ;
106
107 :: make-callback-type ( lib return type-name parameters -- word quot effect )
108     return type-name normalize-c-arg :> ( return type-name )
109     type-name current-vocab create :> type-word 
110     type-word [ reset-generic ] [ reset-c-type ] bi
111     void* type-word typedef
112     parameters return parse-arglist :> ( types callback-effect )
113     type-word callback-effect "callback-effect" set-word-prop
114     type-word lib "callback-library" set-word-prop
115     type-word return types lib library-abi callback-quot (( quot -- alien )) ;
116
117 : (CALLBACK:) ( -- word quot effect )
118     "c-library" get
119     scan scan parse-arg-tokens make-callback-type ;
120
121 PREDICATE: alien-function-word < word
122     def>> {
123         [ length 5 = ]
124         [ last \ alien-invoke eq? ]
125     } 1&& ;
126
127 PREDICATE: alien-callback-type-word < typedef-word
128     "callback-effect" word-prop ;