1 ! Copyright (C) 2005, 2008 Slava Pestov, Alex Chapman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays alien alien.c-types alien.structs alien.arrays
4 alien.strings kernel math namespaces parser sequences words
5 quotations math.parser splitting effects prettyprint
6 prettyprint.sections prettyprint.backend assocs combinators ;
11 : parse-arglist ( return seq -- types effect )
12 2 group dup keys swap values [ "," ?tail drop ] map
13 rot dup "void" = [ drop { } ] [ 1array ] if <effect> ;
15 : function-quot ( type lib func types -- quot )
16 [ alien-invoke ] 2curry 2curry ;
18 : define-function ( return library function parameters -- )
19 >r pick r> parse-arglist
20 pick create-in dup reset-generic
21 >r >r function-quot r> r>
22 -rot define-declared ;
26 : indirect-quot ( function-ptr-quot return types abi -- quot )
27 [ alien-indirect ] 3curry compose ;
29 : define-indirect ( abi return function-ptr-quot function-name parameters -- )
30 >r pick r> parse-arglist
31 rot create-in dup reset-generic
32 >r >r swapd roll indirect-quot r> r>
33 -rot define-declared ;
35 : DLL" lexer get skip-blank parse-string dlopen parsed ; parsing
37 : ALIEN: scan string>number <alien> parsed ; parsing
39 : LIBRARY: scan "c-library" set ; parsing
42 scan "c-library" get scan ";" parse-tokens
43 [ "()" subseq? not ] filter
44 define-function ; parsing
47 scan scan typedef ; parsing
50 scan-word execute scan scan rot [ typedef ] [ 2drop ] if ; parsing
55 >r 2dup r> define-struct-early
56 define-struct ; parsing
59 scan in get parse-definition define-union ; parsing
64 [ >r create-in r> 1quotation define ] 2each ;
69 { [ dup expired? ] [ drop "( alien expired )" text ] }
70 { [ dup pinned-c-ptr? not ] [ drop "( displaced alien )" text ] }
71 [ \ ALIEN: [ alien-address pprint* ] pprint-prefix ]
74 M: dll pprint* dll-path dup "DLL\" " "\"" pprint-string ;