factor/basis/alien/inline/inline.factor

82 lines
2.2 KiB
Factor
Raw Normal View History

USING: accessors alien.inline.compiler alien.libraries
alien.parser arrays fry generalizations io.files io.files.info
io.files.temp kernel lexer math.order multiline namespaces
sequences system vocabs.loader vocabs.parser words ;
IN: alien.inline
2009-07-01 18:43:51 -04:00
<PRIVATE
SYMBOL: c-library
SYMBOL: library-is-c++
SYMBOL: compiler-args
SYMBOL: c-strings
2009-07-01 18:43:51 -04:00
: (C-LIBRARY:) ( -- )
scan c-library set
V{ } clone c-strings set
V{ } clone compiler-args set ;
2009-07-01 18:43:51 -04:00
: (C-LINK:) ( -- )
"-l" scan append compiler-args get push ;
2009-07-01 18:43:51 -04:00
: (C-FRAMEWORK:) ( -- )
"-framework" scan compiler-args get '[ _ push ] bi@ ;
2009-07-01 18:43:51 -04:00
: return-library-function-params ( -- return library function params )
scan c-library get scan ")" parse-tokens
2009-07-02 15:33:14 -04:00
[ "(" subseq? not ] filter [
[ dup CHAR: - = [ drop CHAR: space ] when ] map
] 3dip ;
: factor-function ( return library functions params -- )
[ dup "const " head? [ 6 tail ] when ] 3dip
make-function define-declared ;
2009-07-01 18:43:51 -04:00
: (C-FUNCTION:) ( return library function params -- str )
2009-07-02 15:33:14 -04:00
[ nip ] dip
" " join "(" prepend ")" append 3array " " join
library-is-c++ get [ "extern \"C\" " prepend ] when ;
2009-07-01 18:43:51 -04:00
: library-path ( -- str )
"lib" c-library get library-suffix
2009-07-01 18:43:51 -04:00
3array concat temp-file ;
: compile-library? ( -- ? )
2009-07-02 15:33:14 -04:00
library-path dup exists? [
current-vocab vocab-source-path
[ file-info modified>> ] bi@ <=> +lt+ =
] [ drop t ] if ;
2009-07-01 18:43:51 -04:00
: compile-library ( -- )
library-is-c++ get [ C++ ] [ C ] if
compiler-args get
c-strings get "\n" join
c-library get compile-to-library ;
2009-07-01 18:43:51 -04:00
: (;C-LIBRARY) ( -- )
compile-library? [ compile-library ] when
c-library get library-path "cdecl" add-library ;
2009-07-01 18:43:51 -04:00
PRIVATE>
SYNTAX: C-LIBRARY: (C-LIBRARY:) ;
SYNTAX: COMPILE-AS-C++ t library-is-c++ set ;
2009-07-01 18:43:51 -04:00
SYNTAX: C-LINK: (C-LINK:) ;
SYNTAX: C-FRAMEWORK: (C-FRAMEWORK:) ;
SYNTAX: C-LINK/FRAMEWORK:
2009-07-02 15:33:14 -04:00
os macosx? [ (C-FRAMEWORK:) ] [ (C-LINK:) ] if ;
2009-07-01 18:43:51 -04:00
SYNTAX: C-INCLUDE:
"#include " scan append c-strings get push ;
2009-07-01 18:43:51 -04:00
SYNTAX: C-FUNCTION:
return-library-function-params
2009-07-02 15:33:14 -04:00
[ factor-function ]
2009-07-01 18:43:51 -04:00
4 nkeep (C-FUNCTION:)
" {\n" append parse-here append "\n}\n" append
c-strings get push ;
2009-07-01 18:43:51 -04:00
SYNTAX: ;C-LIBRARY (;C-LIBRARY) ;