2010-02-22 03:31:41 -05:00
|
|
|
! Copyright (C) 2008, 2010 Slava Pestov, Doug Coleman.
|
2008-11-30 18:47:29 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-09-28 20:27:28 -04:00
|
|
|
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 ;
|
2008-11-30 18:47:29 -05:00
|
|
|
IN: alien.parser
|
|
|
|
|
2009-09-27 17:06:13 -04:00
|
|
|
: parse-c-type-name ( name -- word )
|
2009-10-09 05:25:25 -04:00
|
|
|
dup search [ ] [ no-word ] ?if ;
|
2009-09-15 22:43:18 -04:00
|
|
|
|
2009-10-17 00:50:17 -04:00
|
|
|
: parse-array-type ( name -- dims c-type )
|
|
|
|
"[" split unclip
|
|
|
|
[ [ "]" ?tail drop parse-word ] map ] dip ;
|
|
|
|
|
2009-10-16 17:10:09 -04:00
|
|
|
: (parse-c-type) ( string -- type )
|
2009-09-15 22:43:18 -04:00
|
|
|
{
|
2009-10-16 17:10:09 -04:00
|
|
|
{ [ dup "void" = ] [ drop void ] }
|
|
|
|
{ [ CHAR: ] over member? ] [ parse-array-type parse-c-type-name prefix ] }
|
2010-02-21 19:27:36 -05:00
|
|
|
{ [ "*" ?tail ] [ (parse-c-type) <pointer> ] }
|
2010-02-22 00:06:00 -05:00
|
|
|
{ [ dup search ] [ parse-c-type-name ] }
|
2009-10-16 17:10:09 -04:00
|
|
|
[ dup search [ ] [ no-word ] ?if ]
|
2009-09-15 22:43:18 -04:00
|
|
|
} cond ;
|
|
|
|
|
2009-10-16 17:10:09 -04:00
|
|
|
: valid-c-type? ( c-type -- ? )
|
2010-02-21 19:27:36 -05:00
|
|
|
{ [ array? ] [ c-type-word? ] [ pointer? ] [ void? ] } 1|| ;
|
2009-10-16 17:10:09 -04:00
|
|
|
|
|
|
|
: parse-c-type ( string -- type )
|
|
|
|
(parse-c-type) dup valid-c-type? [ no-c-type ] unless ;
|
|
|
|
|
2009-09-15 16:18:54 -04:00
|
|
|
: scan-c-type ( -- c-type )
|
2010-02-21 22:23:47 -05:00
|
|
|
scan {
|
|
|
|
{ [ dup "{" = ] [ drop \ } parse-until >array ] }
|
|
|
|
{ [ dup "pointer:" = ] [ drop scan-c-type <pointer> ] }
|
|
|
|
[ parse-c-type ]
|
|
|
|
} cond ;
|
2009-09-15 16:18:54 -04:00
|
|
|
|
2009-09-16 18:18:19 -04:00
|
|
|
: reset-c-type ( word -- )
|
2009-09-27 23:11:51 -04:00
|
|
|
dup "struct-size" word-prop
|
|
|
|
[ dup [ forget-class ] [ { "struct-size" } reset-props ] bi ] when
|
2009-09-28 20:27:28 -04:00
|
|
|
{
|
|
|
|
"c-type"
|
|
|
|
"pointer-c-type"
|
|
|
|
"callback-effect"
|
|
|
|
"callback-library"
|
|
|
|
} reset-props ;
|
2009-09-16 18:18:19 -04:00
|
|
|
|
|
|
|
: CREATE-C-TYPE ( -- word )
|
2009-09-27 23:11:51 -04:00
|
|
|
scan current-vocab create {
|
|
|
|
[ fake-definition ]
|
|
|
|
[ set-word ]
|
|
|
|
[ reset-c-type ]
|
|
|
|
[ ]
|
|
|
|
} cleave ;
|
2009-09-16 18:18:19 -04:00
|
|
|
|
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
|
|
|
|
2010-02-22 14:25:01 -05:00
|
|
|
<PRIVATE
|
|
|
|
GENERIC: return-type-name ( type -- name )
|
|
|
|
|
|
|
|
M: object return-type-name drop "void" ;
|
|
|
|
M: word return-type-name name>> ;
|
|
|
|
M: pointer return-type-name to>> return-type-name CHAR: * suffix ;
|
|
|
|
PRIVATE>
|
|
|
|
|
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
|
|
|
]
|
2010-02-22 14:25:01 -05:00
|
|
|
[ [ { } ] [ return-type-name 1array ] if-void ]
|
2008-11-30 18:47:29 -05:00
|
|
|
bi* <effect> ;
|
|
|
|
|
|
|
|
: function-quot ( return library function types -- quot )
|
|
|
|
'[ _ _ _ _ alien-invoke ] ;
|
|
|
|
|
2010-02-20 06:02:56 -05:00
|
|
|
:: make-function ( return library function parameters -- word quot effect )
|
2010-02-22 07:28:56 -05:00
|
|
|
return function normalize-c-arg :> ( return function )
|
2009-07-23 19:14:07 -04:00
|
|
|
function create-in dup reset-generic
|
2010-02-22 07:28:56 -05:00
|
|
|
return library function
|
2009-04-18 03:37:35 -04:00
|
|
|
parameters return parse-arglist [ function-quot ] dip ;
|
|
|
|
|
2009-09-21 12:59:41 -04:00
|
|
|
: parse-arg-tokens ( -- tokens )
|
|
|
|
";" parse-tokens [ "()" subseq? not ] filter ;
|
|
|
|
|
2009-04-18 03:37:35 -04:00
|
|
|
: (FUNCTION:) ( -- word quot effect )
|
2009-09-21 12:59:41 -04:00
|
|
|
scan "c-library" get scan parse-arg-tokens make-function ;
|
2009-04-18 03:37:35 -04:00
|
|
|
|
|
|
|
: define-function ( return library function parameters -- )
|
|
|
|
make-function define-declared ;
|
2009-09-15 23:39:25 -04:00
|
|
|
|
2009-09-21 12:59:41 -04:00
|
|
|
: callback-quot ( return types abi -- quot )
|
2010-02-22 07:28:56 -05:00
|
|
|
'[ [ _ _ _ ] dip alien-callback ] ;
|
2009-09-21 12:59:41 -04:00
|
|
|
|
2010-02-22 07:28:56 -05:00
|
|
|
:: make-callback-type ( lib return type-name parameters -- word quot effect )
|
|
|
|
return type-name normalize-c-arg :> ( return type-name )
|
2009-09-21 12:59:41 -04:00
|
|
|
type-name current-vocab create :> type-word
|
|
|
|
type-word [ reset-generic ] [ reset-c-type ] bi
|
|
|
|
void* type-word typedef
|
2009-10-28 17:11:33 -04:00
|
|
|
parameters return parse-arglist :> ( types callback-effect )
|
2009-09-21 12:59:41 -04:00
|
|
|
type-word callback-effect "callback-effect" set-word-prop
|
2009-09-28 20:27:28 -04:00
|
|
|
type-word lib "callback-library" set-word-prop
|
|
|
|
type-word return types lib library-abi callback-quot (( quot -- alien )) ;
|
2009-09-21 12:59:41 -04:00
|
|
|
|
2009-09-28 20:27:28 -04:00
|
|
|
: (CALLBACK:) ( -- word quot effect )
|
|
|
|
"c-library" get
|
2009-09-21 12:59:41 -04:00
|
|
|
scan scan parse-arg-tokens make-callback-type ;
|
|
|
|
|
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&& ;
|
2009-09-21 12:59:41 -04:00
|
|
|
|
|
|
|
PREDICATE: alien-callback-type-word < typedef-word
|
|
|
|
"callback-effect" word-prop ;
|