| 
									
										
										
										
											2010-03-17 02:19:43 -04:00
										 |  |  | ! Copyright (C) 2008, 2010 Slava Pestov, Doug Coleman, Joe Groff. | 
					
						
							| 
									
										
										
										
											2008-11-30 18:47:29 -05:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2010-03-17 02:19:43 -04:00
										 |  |  | USING: accessors alien alien.c-types alien.libraries arrays | 
					
						
							|  |  |  | assocs classes combinators combinators.short-circuit | 
					
						
							|  |  |  | compiler.units effects grouping kernel parser sequences | 
					
						
							|  |  |  | splitting words fry locals lexer namespaces summary math | 
					
						
							| 
									
										
										
										
											2011-08-27 16:48:30 -04:00
										 |  |  | vocabs.parser words.constant classes.parser alien.enums ;
 | 
					
						
							| 
									
										
										
										
											2008-11-30 18:47:29 -05:00
										 |  |  | IN: alien.parser | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-12 19:09:26 -04:00
										 |  |  | SYMBOL: current-library | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-15 04:51:55 -04:00
										 |  |  | DEFER: (parse-c-type) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-15 05:37:17 -04:00
										 |  |  | ERROR: bad-array-type ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-15 04:51:55 -04:00
										 |  |  | : parse-array-type ( name -- c-type )
 | 
					
						
							| 
									
										
										
										
											2009-10-17 00:50:17 -04:00
										 |  |  |     "[" split unclip
 | 
					
						
							| 
									
										
										
										
											2011-10-01 19:42:37 -04:00
										 |  |  |     [ [ "]" ?tail [ bad-array-type ] unless parse-datum ] map ] | 
					
						
							| 
									
										
										
										
											2010-08-15 05:37:17 -04:00
										 |  |  |     [ (parse-c-type) ] | 
					
						
							|  |  |  |     bi* prefix ;
 | 
					
						
							| 
									
										
										
										
											2009-10-17 00:50:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-16 17:10:09 -04:00
										 |  |  | : (parse-c-type) ( string -- type )
 | 
					
						
							| 
									
										
										
										
											2009-09-15 22:43:18 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-08-15 04:51:55 -04:00
										 |  |  |         { [ "*" ?tail ] [ (parse-c-type) <pointer> ] } | 
					
						
							| 
									
										
										
										
											2010-08-15 05:37:17 -04:00
										 |  |  |         { [ CHAR: ] over member? ] [ parse-array-type ] } | 
					
						
							| 
									
										
										
										
											2011-10-14 15:31:06 -04:00
										 |  |  |         { [ dup search ] [ parse-word ] } | 
					
						
							|  |  |  |         [ parse-word ] | 
					
						
							| 
									
										
										
										
											2009-09-15 22:43:18 -04:00
										 |  |  |     } cond ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-15 04:51:55 -04:00
										 |  |  | : c-array? ( c-type -- ? )
 | 
					
						
							|  |  |  |     { [ array? ] [ first { [ c-type-word? ] [ pointer? ] } 1|| ] } 1&& ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-16 17:10:09 -04:00
										 |  |  | : valid-c-type? ( c-type -- ? )
 | 
					
						
							| 
									
										
										
										
											2010-08-15 04:51:55 -04:00
										 |  |  |     { [ c-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-07-06 16:20:08 -04:00
										 |  |  |     scan-token { | 
					
						
							| 
									
										
										
										
											2010-02-21 22:23:47 -05:00
										 |  |  |         { [ 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" | 
					
						
							|  |  |  |         "callback-effect" | 
					
						
							|  |  |  |         "callback-library" | 
					
						
							|  |  |  |     } reset-props ;
 | 
					
						
							| 
									
										
										
										
											2009-09-16 18:18:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-23 13:16:18 -05:00
										 |  |  | ERROR: *-in-c-type-name name ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : validate-c-type-name ( name -- name )
 | 
					
						
							|  |  |  |     dup "*" tail?
 | 
					
						
							|  |  |  |     [ *-in-c-type-name ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-10 02:48:22 -04:00
										 |  |  | : (CREATE-C-TYPE) ( word -- word )
 | 
					
						
							|  |  |  |     validate-c-type-name current-vocab create { | 
					
						
							| 
									
										
										
										
											2009-09-27 23:11:51 -04:00
										 |  |  |         [ fake-definition ] | 
					
						
							|  |  |  |         [ set-word ] | 
					
						
							|  |  |  |         [ reset-c-type ] | 
					
						
							|  |  |  |         [ ] | 
					
						
							|  |  |  |     } cleave ;
 | 
					
						
							| 
									
										
										
										
											2009-09-16 18:18:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-10 02:48:22 -04:00
										 |  |  | : CREATE-C-TYPE ( -- word )
 | 
					
						
							| 
									
										
										
										
											2011-10-01 19:42:37 -04:00
										 |  |  |     scan-token (CREATE-C-TYPE) ;
 | 
					
						
							| 
									
										
										
										
											2010-04-10 02:48:22 -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 ;
 | 
					
						
							| 
									
										
										
										
											2010-02-28 19:15:58 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : parse-pointers ( type name -- type' name' )
 | 
					
						
							|  |  |  |     "*" ?head | 
					
						
							|  |  |  |     [ [ <pointer> ] dip parse-pointers ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-13 00:42:48 -04:00
										 |  |  | : next-enum-member ( members name value -- members value' )
 | 
					
						
							| 
									
										
										
										
											2011-08-27 16:48:30 -04:00
										 |  |  |     [ define-enum-value ] | 
					
						
							|  |  |  |     [ [ 2array suffix! ] [ enum>number 1 + ] bi ] 2bi ;
 | 
					
						
							| 
									
										
										
										
											2010-04-13 00:42:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-13 02:58:58 -04:00
										 |  |  | : parse-enum-name ( -- name )
 | 
					
						
							| 
									
										
										
										
											2011-10-14 13:23:52 -04:00
										 |  |  |     CREATE-C-TYPE dup save-location ;
 | 
					
						
							| 
									
										
										
										
											2010-04-10 02:48:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-13 02:58:58 -04:00
										 |  |  | : parse-enum-base-type ( -- base-type token )
 | 
					
						
							| 
									
										
										
										
											2011-10-01 19:42:37 -04:00
										 |  |  |     scan-token dup "<" =
 | 
					
						
							|  |  |  |     [ drop scan-object scan-token ] | 
					
						
							| 
									
										
										
										
											2010-04-13 02:58:58 -04:00
										 |  |  |     [ [ int ] dip ] if ;
 | 
					
						
							| 
									
										
										
										
											2010-04-10 02:48:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-13 02:58:58 -04:00
										 |  |  | : parse-enum-member ( members name value -- members value' )
 | 
					
						
							|  |  |  |     over "{" =
 | 
					
						
							| 
									
										
										
										
											2011-10-01 19:42:37 -04:00
										 |  |  |     [ 2drop scan-token create-class-in scan-object next-enum-member "}" expect ] | 
					
						
							| 
									
										
										
										
											2010-09-29 17:24:25 -04:00
										 |  |  |     [ [ create-class-in ] dip next-enum-member ] if ;
 | 
					
						
							| 
									
										
										
										
											2010-04-13 00:58:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-13 02:58:58 -04:00
										 |  |  | : parse-enum-members ( members counter token -- members )
 | 
					
						
							|  |  |  |     dup ";" = not
 | 
					
						
							| 
									
										
										
										
											2011-10-01 19:42:37 -04:00
										 |  |  |     [ swap parse-enum-member scan-token parse-enum-members ] [ 2drop ] if ;
 | 
					
						
							| 
									
										
										
										
											2010-04-13 00:58:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-13 02:58:58 -04:00
										 |  |  | : parse-enum ( -- name base-type members )
 | 
					
						
							|  |  |  |     parse-enum-name | 
					
						
							|  |  |  |     parse-enum-base-type | 
					
						
							|  |  |  |     [ V{ } clone 0 ] dip parse-enum-members ;
 | 
					
						
							| 
									
										
										
										
											2010-04-10 02:48:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-28 19:15:58 -05:00
										 |  |  | : scan-function-name ( -- return function )
 | 
					
						
							| 
									
										
										
										
											2011-10-01 19:42:37 -04:00
										 |  |  |     scan-c-type scan-token parse-pointers ;
 | 
					
						
							| 
									
										
										
										
											2010-02-28 19:15:58 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | :: (scan-c-args) ( end-marker types names -- )
 | 
					
						
							| 
									
										
										
										
											2011-10-01 19:42:37 -04:00
										 |  |  |     scan-token :> type-str | 
					
						
							| 
									
										
										
										
											2010-02-28 19:15:58 -05:00
										 |  |  |     type-str end-marker = [ | 
					
						
							|  |  |  |         type-str { "(" ")" } member? [ | 
					
						
							|  |  |  |             type-str parse-c-type :> type | 
					
						
							| 
									
										
										
										
											2011-10-01 19:42:37 -04:00
										 |  |  |             scan-token "," ?tail drop :> name | 
					
						
							| 
									
										
										
										
											2010-02-28 19:15:58 -05:00
										 |  |  |             type name parse-pointers :> ( type' name' )
 | 
					
						
							|  |  |  |             type' types push name' names push
 | 
					
						
							|  |  |  |         ] unless
 | 
					
						
							|  |  |  |         end-marker types names (scan-c-args) | 
					
						
							|  |  |  |     ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : scan-c-args ( end-marker -- types names )
 | 
					
						
							|  |  |  |     V{ } clone V{ } clone [ (scan-c-args) ] 2keep [ >array ] bi@ ;
 | 
					
						
							| 
									
										
										
										
											2008-11-30 18:47:29 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : function-quot ( return library function types -- quot )
 | 
					
						
							|  |  |  |     '[ _ _ _ _ alien-invoke ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-28 19:15:58 -05:00
										 |  |  | : function-effect ( names return -- effect )
 | 
					
						
							|  |  |  |     [ { } ] [ return-type-name 1array ] if-void <effect> ;
 | 
					
						
							| 
									
										
										
										
											2009-04-18 03:37:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-14 15:06:45 -04:00
										 |  |  | : create-function ( name -- word )
 | 
					
						
							|  |  |  |     create-in dup reset-generic ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: (make-function) ( return function library types names -- quot effect )
 | 
					
						
							| 
									
										
										
										
											2010-02-28 19:15:58 -05:00
										 |  |  |     return library function types function-quot | 
					
						
							|  |  |  |     names return function-effect ;
 | 
					
						
							| 
									
										
										
										
											2009-09-21 12:59:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-14 15:06:45 -04:00
										 |  |  | :: make-function ( return function library types names -- word quot effect )
 | 
					
						
							|  |  |  |     function create-function | 
					
						
							|  |  |  |     return function library types names (make-function) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (FUNCTION:) ( -- return function library types names )
 | 
					
						
							|  |  |  |     scan-function-name current-library get ";" scan-c-args ;
 | 
					
						
							| 
									
										
										
										
											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-28 19:15:58 -05:00
										 |  |  | :: make-callback-type ( lib return type-name types names -- word quot effect )
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2010-02-28 19:15:58 -05:00
										 |  |  |     type-word names return function-effect "callback-effect" set-word-prop | 
					
						
							| 
									
										
										
										
											2009-09-28 20:27:28 -04:00
										 |  |  |     type-word lib "callback-library" set-word-prop | 
					
						
							| 
									
										
										
										
											2011-10-18 16:18:42 -04:00
										 |  |  |     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 )
 | 
					
						
							| 
									
										
										
										
											2010-04-12 19:09:26 -04:00
										 |  |  |     current-library get
 | 
					
						
							| 
									
										
										
										
											2010-02-28 19:15:58 -05:00
										 |  |  |     scan-function-name ";" scan-c-args make-callback-type ;
 | 
					
						
							| 
									
										
										
										
											2009-09-21 12:59:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-14 15:40:10 -04:00
										 |  |  | PREDICATE: alien-function-alias-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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-14 15:40:10 -04:00
										 |  |  | PREDICATE: alien-function-word < alien-function-alias-word | 
					
						
							|  |  |  |     [ def>> third ] [ name>> ] bi = ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-21 12:59:41 -04:00
										 |  |  | PREDICATE: alien-callback-type-word < typedef-word | 
					
						
							|  |  |  |     "callback-effect" word-prop ;
 | 
					
						
							| 
									
										
										
										
											2010-04-12 19:09:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : global-quot ( type word -- quot )
 | 
					
						
							| 
									
										
										
										
											2010-05-22 01:25:10 -04:00
										 |  |  |     swap [ name>> current-library get ] dip
 | 
					
						
							|  |  |  |     '[ _ _ address-of 0 _ alien-value ] ;
 | 
					
						
							| 
									
										
										
										
											2010-04-12 19:09:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-22 03:37:56 -04:00
										 |  |  | : set-global-quot ( type word -- quot )
 | 
					
						
							|  |  |  |     swap [ name>> current-library get ] dip
 | 
					
						
							|  |  |  |     '[ _ _ address-of 0 _ set-alien-value ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : define-global-getter ( type word -- )
 | 
					
						
							| 
									
										
										
										
											2011-10-18 16:18:42 -04:00
										 |  |  |     [ nip ] [ global-quot ] 2bi ( -- value ) define-declared ;
 | 
					
						
							| 
									
										
										
										
											2011-05-22 03:37:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : define-global-setter ( type word -- )
 | 
					
						
							|  |  |  |     [ nip name>> "set-" prepend create-in ] | 
					
						
							| 
									
										
										
										
											2011-10-18 16:18:42 -04:00
										 |  |  |     [ set-global-quot ] 2bi ( obj -- ) define-declared ;
 | 
					
						
							| 
									
										
										
										
											2011-05-22 03:37:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : define-global ( type word -- )
 | 
					
						
							|  |  |  |     [ define-global-getter ] [ define-global-setter ] 2bi ;
 |