! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
-USING: accessors alien alien.c-types arrays assocs classes
-combinators combinators.short-circuit compiler.units effects
-grouping kernel parser sequences splitting words fry locals
-lexer namespaces summary math vocabs.parser ;
+USING: accessors alien alien.c-types alien.parser
+alien.libraries arrays assocs classes combinators
+combinators.short-circuit compiler.units effects grouping
+kernel parser sequences splitting words fry locals lexer
+namespaces summary math vocabs.parser ;
IN: alien.parser
: parse-c-type-name ( name -- word )
- dup search [ nip ] [ no-word ] if* ;
+ dup search [ ] [ no-word ] ?if ;
-: parse-c-type ( string -- array )
+: parse-array-type ( name -- dims c-type )
+ "[" split unclip
+ [ [ "]" ?tail drop parse-word ] map ] dip ;
+
+: (parse-c-type) ( string -- type )
{
- { [ dup "void" = ] [ drop void ] }
- { [ CHAR: ] over member? ] [ parse-array-type parse-c-type-name prefix ] }
- { [ dup search c-type-word? ] [ parse-c-type-name ] }
- { [ "**" ?tail ] [ drop void* ] }
- { [ "*" ?tail ] [ parse-c-type-name resolve-pointer-type ] }
- [ parse-c-type-name no-c-type ]
+ { [ dup "void" = ] [ drop void ] }
+ { [ CHAR: ] over member? ] [ parse-array-type parse-c-type-name prefix ] }
+ { [ dup search ] [ parse-c-type-name ] }
+ { [ "*" ?tail ] [ (parse-c-type) <pointer> ] }
+ [ dup search [ ] [ no-word ] ?if ]
} cond ;
+: valid-c-type? ( c-type -- ? )
+ { [ array? ] [ c-type-word? ] [ pointer? ] [ void? ] } 1|| ;
+
+: parse-c-type ( string -- type )
+ (parse-c-type) dup valid-c-type? [ no-c-type ] unless ;
+
: scan-c-type ( -- c-type )
scan dup "{" =
[ drop \ } parse-until >array ]
: reset-c-type ( word -- )
dup "struct-size" word-prop
[ dup [ forget-class ] [ { "struct-size" } reset-props ] bi ] when
- { "c-type" "pointer-c-type" "callback-effect" "callback-abi" } reset-props ;
+ {
+ "c-type"
+ "pointer-c-type"
+ "callback-effect"
+ "callback-library"
+ } reset-props ;
: CREATE-C-TYPE ( -- word )
scan current-vocab create {
: callback-quot ( return types abi -- quot )
[ [ ] 3curry dip alien-callback ] 3curry ;
-:: make-callback-type ( abi return! type-name! parameters -- word quot effect )
+: library-abi ( lib -- abi )
+ library [ abi>> ] [ "cdecl" ] if* ;
+
+:: make-callback-type ( lib return! type-name! parameters -- word quot effect )
return type-name normalize-c-arg type-name! return!
type-name current-vocab create :> type-word
type-word [ reset-generic ] [ reset-c-type ] bi
void* type-word typedef
- parameters return parse-arglist :> callback-effect :> types
+ parameters return parse-arglist :> ( types callback-effect )
type-word callback-effect "callback-effect" set-word-prop
- type-word abi "callback-abi" set-word-prop
- type-word return types abi callback-quot (( quot -- alien )) ;
+ type-word lib "callback-library" set-word-prop
+ type-word return types lib library-abi callback-quot (( quot -- alien )) ;
-: (CALLBACK:) ( abi -- word quot effect )
+: (CALLBACK:) ( -- word quot effect )
+ "c-library" get
scan scan parse-arg-tokens make-callback-type ;
PREDICATE: alien-function-word < word