2009-07-23 19:05:09 -04:00
|
|
|
! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman.
|
2008-11-30 18:47:29 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-09-15 23:39:25 -04:00
|
|
|
USING: accessors alien alien.c-types arrays assocs
|
2009-09-16 15:17:13 -04:00
|
|
|
combinators combinators.short-circuit effects grouping
|
|
|
|
kernel parser sequences splitting words fry locals lexer
|
|
|
|
namespaces summary math vocabs.parser ;
|
2008-11-30 18:47:29 -05:00
|
|
|
IN: alien.parser
|
|
|
|
|
2009-09-15 22:43:18 -04:00
|
|
|
: parse-c-type-name ( name -- word/string )
|
|
|
|
[ search ] keep or ;
|
|
|
|
|
|
|
|
: parse-c-type ( string -- array )
|
|
|
|
{
|
|
|
|
{ [ dup "void" = ] [ drop void ] }
|
|
|
|
{ [ CHAR: ] over member? ] [ parse-array-type parse-c-type-name prefix ] }
|
|
|
|
{ [ dup search c-type-word? ] [ parse-c-type-name ] }
|
|
|
|
{ [ dup c-types get at ] [ ] }
|
|
|
|
{ [ "*" ?tail ] [ parse-c-type-name resolve-pointer-type ] }
|
|
|
|
[ no-c-type ]
|
|
|
|
} cond ;
|
|
|
|
|
2009-09-15 16:18:54 -04:00
|
|
|
: scan-c-type ( -- c-type )
|
|
|
|
scan dup "{" =
|
|
|
|
[ drop \ } parse-until >array ]
|
|
|
|
[ parse-c-type ] if ;
|
|
|
|
|
2009-09-16 18:18:19 -04:00
|
|
|
: reset-c-type ( word -- )
|
|
|
|
{ "c-type" "pointer-c-type" } reset-props ;
|
|
|
|
|
|
|
|
: CREATE-C-TYPE ( -- word )
|
|
|
|
scan current-vocab create dup reset-c-type ;
|
|
|
|
|
2009-07-23 19:05:09 -04:00
|
|
|
: normalize-c-arg ( type name -- type' name' )
|
|
|
|
[ length ]
|
|
|
|
[
|
|
|
|
[ CHAR: * = ] trim-head
|
|
|
|
[ length - CHAR: * <array> append ] keep
|
2009-09-15 16:18:54 -04:00
|
|
|
] bi
|
|
|
|
[ parse-c-type ] dip ;
|
2009-07-23 16:48:10 -04:00
|
|
|
|
2008-11-30 18:47:29 -05:00
|
|
|
: parse-arglist ( parameters return -- types effect )
|
2009-07-23 19:05:09 -04:00
|
|
|
[
|
|
|
|
2 group [ first2 normalize-c-arg 2array ] map
|
2009-07-23 19:14:07 -04:00
|
|
|
unzip [ "," ?tail drop ] map
|
2009-07-23 19:05:09 -04:00
|
|
|
]
|
2008-11-30 18:47:29 -05:00
|
|
|
[ [ { } ] [ 1array ] if-void ]
|
|
|
|
bi* <effect> ;
|
|
|
|
|
|
|
|
: function-quot ( return library function types -- quot )
|
|
|
|
'[ _ _ _ _ alien-invoke ] ;
|
|
|
|
|
2009-07-23 19:05:09 -04:00
|
|
|
:: make-function ( return! library function! parameters -- word quot effect )
|
|
|
|
return function normalize-c-arg function! return!
|
2009-07-23 19:14:07 -04:00
|
|
|
function create-in dup reset-generic
|
2008-11-30 18:47:29 -05:00
|
|
|
return library function
|
2009-04-18 03:37:35 -04:00
|
|
|
parameters return parse-arglist [ function-quot ] dip ;
|
|
|
|
|
|
|
|
: (FUNCTION:) ( -- word quot effect )
|
|
|
|
scan "c-library" get scan ";" parse-tokens
|
|
|
|
[ "()" subseq? not ] filter
|
|
|
|
make-function ;
|
|
|
|
|
|
|
|
: define-function ( return library function parameters -- )
|
|
|
|
make-function define-declared ;
|
2009-09-15 23:39:25 -04:00
|
|
|
|
|
|
|
PREDICATE: alien-function-word < word
|
2009-09-16 15:17:13 -04:00
|
|
|
def>> {
|
|
|
|
[ length 5 = ]
|
|
|
|
[ last \ alien-invoke eq? ]
|
|
|
|
} 1&& ;
|