]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/parser/parser.factor
alien: some code cleanups and fixes
[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         { [ dup search           ] [ parse-c-type-name ] }
22         { [ "**" ?tail           ] [ drop void* ] }
23         { [ "*" ?tail            ] [ parse-c-type-name resolve-pointer-type ] }
24         [ dup search [ ] [ no-word ] ?if ]
25     } cond ;
26
27 : valid-c-type? ( c-type -- ? )
28     { [ array? ] [ c-type-name? ] [ void? ] } 1|| ;
29
30 : parse-c-type ( string -- type )
31     (parse-c-type) dup valid-c-type? [ no-c-type ] unless ;
32
33 : scan-c-type ( -- c-type )
34     scan dup "{" =
35     [ drop \ } parse-until >array ]
36     [ parse-c-type ] if ; 
37
38 : reset-c-type ( word -- )
39     dup "struct-size" word-prop
40     [ dup [ forget-class ] [ { "struct-size" } reset-props ] bi ] when
41     {
42         "c-type"
43         "pointer-c-type"
44         "callback-effect"
45         "callback-library"
46     } reset-props ;
47
48 : CREATE-C-TYPE ( -- word )
49     scan current-vocab create {
50         [ fake-definition ]
51         [ set-word ]
52         [ reset-c-type ]
53         [ ]
54     } cleave ;
55
56 : normalize-c-arg ( type name -- type' name' )
57     [ length ]
58     [
59         [ CHAR: * = ] trim-head
60         [ length - CHAR: * <array> append ] keep
61     ] bi
62     [ parse-c-type ] dip ;
63
64 : parse-arglist ( parameters return -- types effect )
65     [
66         2 group [ first2 normalize-c-arg 2array ] map
67         unzip [ "," ?tail drop ] map
68     ]
69     [ [ { } ] [ name>> 1array ] if-void ]
70     bi* <effect> ;
71
72 : function-quot ( return library function types -- quot )
73     '[ _ _ _ _ alien-invoke ] ;
74
75 :: make-function ( return library function parameters -- word quot effect )
76     return function normalize-c-arg :> ( return function )
77     function create-in dup reset-generic
78     return library function
79     parameters return parse-arglist [ function-quot ] dip ;
80
81 : parse-arg-tokens ( -- tokens )
82     ";" parse-tokens [ "()" subseq? not ] filter ;
83
84 : (FUNCTION:) ( -- word quot effect )
85     scan "c-library" get scan parse-arg-tokens make-function ;
86
87 : define-function ( return library function parameters -- )
88     make-function define-declared ;
89
90 : callback-quot ( return types abi -- quot )
91     '[ [ _ _ _ ] dip alien-callback ] ;
92
93 :: make-callback-type ( lib return type-name parameters -- word quot effect )
94     return type-name normalize-c-arg :> ( return type-name )
95     type-name current-vocab create :> type-word 
96     type-word [ reset-generic ] [ reset-c-type ] bi
97     void* type-word typedef
98     parameters return parse-arglist :> ( types callback-effect )
99     type-word callback-effect "callback-effect" set-word-prop
100     type-word lib "callback-library" set-word-prop
101     type-word return types lib library-abi callback-quot (( quot -- alien )) ;
102
103 : (CALLBACK:) ( -- word quot effect )
104     "c-library" get
105     scan scan parse-arg-tokens make-callback-type ;
106
107 PREDICATE: alien-function-word < word
108     def>> {
109         [ length 5 = ]
110         [ last \ alien-invoke eq? ]
111     } 1&& ;
112
113 PREDICATE: alien-callback-type-word < typedef-word
114     "callback-effect" word-prop ;