| 
									
										
										
										
											2009-02-06 11:06:22 -05:00
										 |  |  | ! (c) 2009 Joe Groff, see BSD license | 
					
						
							| 
									
										
										
										
											2010-10-20 18:42:53 -04:00
										 |  |  | USING: accessors alien alien.complex alien.c-types alien.data | 
					
						
							| 
									
										
										
										
											2010-05-18 18:36:47 -04:00
										 |  |  | alien.parser grouping alien.strings alien.syntax arrays ascii | 
					
						
							|  |  |  | assocs byte-arrays combinators combinators.short-circuit fry | 
					
						
							|  |  |  | generalizations kernel lexer macros math math.parser namespaces | 
					
						
							|  |  |  | parser sequences sequences.generalizations splitting | 
					
						
							|  |  |  | stack-checker vectors vocabs.parser words locals | 
					
						
							|  |  |  | io.encodings.ascii io.encodings.string shuffle effects | 
					
						
							|  |  |  | math.ranges math.order sorting strings system alien.libraries ;
 | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  | QUALIFIED-WITH: alien.c-types c | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | IN: alien.fortran | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-07 19:49:54 -04:00
										 |  |  | SINGLETONS: f2c-abi g95-abi gfortran-abi intel-unix-abi intel-windows-abi ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-09 15:35:44 -05:00
										 |  |  | <<  | 
					
						
							|  |  |  | : add-f2c-libraries ( -- )
 | 
					
						
							| 
									
										
										
										
											2010-03-31 22:20:35 -04:00
										 |  |  |     "I77" "libI77.so" cdecl add-library | 
					
						
							|  |  |  |     "F77" "libF77.so" cdecl add-library ;
 | 
					
						
							| 
									
										
										
										
											2009-02-09 15:35:44 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | os netbsd? [ add-f2c-libraries ] when
 | 
					
						
							|  |  |  | >> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | : alien>nstring ( alien len encoding -- string )
 | 
					
						
							|  |  |  |     [ memory>byte-array ] dip decode ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | ERROR: invalid-fortran-type type ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | DEFER: fortran-sig>c-sig | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  | DEFER: fortran-ret-type>c-type | 
					
						
							|  |  |  | DEFER: fortran-arg-type>c-type | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  | DEFER: fortran-name>symbol-name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: library-fortran-abis | 
					
						
							|  |  |  | SYMBOL: fortran-abi | 
					
						
							|  |  |  | library-fortran-abis [ H{ } clone ] initialize
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  | : lowercase-name-with-underscore ( name -- name' )
 | 
					
						
							|  |  |  |     >lower "_" append ;
 | 
					
						
							|  |  |  | : lowercase-name-with-extra-underscore ( name -- name' )
 | 
					
						
							|  |  |  |     >lower CHAR: _ over member?  | 
					
						
							|  |  |  |     [ "__" append ] [ "_" append ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: fortran-c-abi fortran-abi ( -- abi )
 | 
					
						
							| 
									
										
										
										
											2010-03-31 22:20:35 -04:00
										 |  |  | M: f2c-abi fortran-c-abi cdecl ;
 | 
					
						
							|  |  |  | M: g95-abi fortran-c-abi cdecl ;
 | 
					
						
							|  |  |  | M: gfortran-abi fortran-c-abi cdecl ;
 | 
					
						
							|  |  |  | M: intel-unix-abi fortran-c-abi cdecl ;
 | 
					
						
							|  |  |  | M: intel-windows-abi fortran-c-abi cdecl ;
 | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | HOOK: real-functions-return-double? fortran-abi ( -- ? )
 | 
					
						
							|  |  |  | M: f2c-abi real-functions-return-double? t ;
 | 
					
						
							| 
									
										
										
										
											2009-04-07 19:49:54 -04:00
										 |  |  | M: g95-abi real-functions-return-double? f ;
 | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  | M: gfortran-abi real-functions-return-double? f ;
 | 
					
						
							|  |  |  | M: intel-unix-abi real-functions-return-double? f ;
 | 
					
						
							|  |  |  | M: intel-windows-abi real-functions-return-double? f ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: complex-functions-return-by-value? fortran-abi ( -- ? )
 | 
					
						
							|  |  |  | M: f2c-abi complex-functions-return-by-value? f ;
 | 
					
						
							| 
									
										
										
										
											2009-04-07 19:49:54 -04:00
										 |  |  | M: g95-abi complex-functions-return-by-value? f ;
 | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  | M: gfortran-abi complex-functions-return-by-value? t ;
 | 
					
						
							|  |  |  | M: intel-unix-abi complex-functions-return-by-value? f ;
 | 
					
						
							|  |  |  | M: intel-windows-abi complex-functions-return-by-value? f ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: character(1)-maps-to-char? fortran-abi ( -- ? )
 | 
					
						
							|  |  |  | M: f2c-abi character(1)-maps-to-char? f ;
 | 
					
						
							| 
									
										
										
										
											2009-04-07 19:49:54 -04:00
										 |  |  | M: g95-abi character(1)-maps-to-char? f ;
 | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  | M: gfortran-abi character(1)-maps-to-char? f ;
 | 
					
						
							|  |  |  | M: intel-unix-abi character(1)-maps-to-char? t ;
 | 
					
						
							|  |  |  | M: intel-windows-abi character(1)-maps-to-char? t ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: mangle-name fortran-abi ( name -- name' )
 | 
					
						
							|  |  |  | M: f2c-abi mangle-name lowercase-name-with-extra-underscore ;
 | 
					
						
							| 
									
										
										
										
											2009-04-07 21:06:28 -04:00
										 |  |  | M: g95-abi mangle-name lowercase-name-with-extra-underscore ;
 | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  | M: gfortran-abi mangle-name lowercase-name-with-underscore ;
 | 
					
						
							|  |  |  | M: intel-unix-abi mangle-name lowercase-name-with-underscore ;
 | 
					
						
							|  |  |  | M: intel-windows-abi mangle-name >upper ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | TUPLE: fortran-type dims size out? ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | TUPLE: number-type < fortran-type ;
 | 
					
						
							|  |  |  | TUPLE: integer-type < number-type ;
 | 
					
						
							|  |  |  | TUPLE: logical-type < integer-type ;
 | 
					
						
							|  |  |  | TUPLE: real-type < number-type ;
 | 
					
						
							|  |  |  | TUPLE: double-precision-type < number-type ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: character-type < fortran-type ;
 | 
					
						
							|  |  |  | TUPLE: misc-type < fortran-type name ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: complex-type < number-type ;
 | 
					
						
							|  |  |  | TUPLE: real-complex-type < complex-type ;
 | 
					
						
							|  |  |  | TUPLE: double-complex-type < complex-type ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CONSTANT: fortran>c-types H{ | 
					
						
							|  |  |  |     { "character"        character-type        } | 
					
						
							|  |  |  |     { "integer"          integer-type          } | 
					
						
							|  |  |  |     { "logical"          logical-type          } | 
					
						
							|  |  |  |     { "real"             real-type             } | 
					
						
							| 
									
										
										
										
											2009-02-06 11:06:22 -05:00
										 |  |  |     { "double-precision" double-precision-type } | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  |     { "complex"          real-complex-type     } | 
					
						
							| 
									
										
										
										
											2009-02-06 11:06:22 -05:00
										 |  |  |     { "double-complex"   double-complex-type   } | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : append-dimensions ( base-c-type type -- c-type )
 | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  |     dims>> [ product 2array ] when* ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | MACRO: size-case-type ( cases -- )
 | 
					
						
							|  |  |  |     [ invalid-fortran-type ] suffix
 | 
					
						
							|  |  |  |     '[ [ size>> _ case ] [ append-dimensions ] bi ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : simple-type ( type base-c-type -- c-type )
 | 
					
						
							|  |  |  |     swap
 | 
					
						
							|  |  |  |     [ dup size>> [ invalid-fortran-type ] [ drop ] if ] | 
					
						
							|  |  |  |     [ append-dimensions ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | : new-fortran-type ( out? dims size class -- type )
 | 
					
						
							| 
									
										
										
										
											2010-05-05 16:52:54 -04:00
										 |  |  |     new [ [ size<< ] [ dims<< ] [ out?<< ] tri ] keep ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | GENERIC: (fortran-type>c-type) ( type -- c-type )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  | M: f (fortran-type>c-type) drop c:void ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 19:51:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | M: integer-type (fortran-type>c-type) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  |         { f [ c:int      ] } | 
					
						
							|  |  |  |         { 1 [ c:char     ] } | 
					
						
							|  |  |  |         { 2 [ c:short    ] } | 
					
						
							|  |  |  |         { 4 [ c:int      ] } | 
					
						
							|  |  |  |         { 8 [ c:longlong ] } | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  |     } size-case-type ;
 | 
					
						
							|  |  |  | M: real-type (fortran-type>c-type) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  |         { f [ c:float  ] } | 
					
						
							|  |  |  |         { 4 [ c:float  ] } | 
					
						
							|  |  |  |         { 8 [ c:double ] } | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  |     } size-case-type ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 11:06:22 -05:00
										 |  |  | M: real-complex-type (fortran-type>c-type) | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  |         {  f [ complex-float  ] } | 
					
						
							|  |  |  |         {  8 [ complex-float  ] } | 
					
						
							|  |  |  |         { 16 [ complex-double ] } | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  |     } size-case-type ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: double-precision-type (fortran-type>c-type) | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  |     c:double simple-type ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | M: double-complex-type (fortran-type>c-type) | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  |     complex-double simple-type ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | M: misc-type (fortran-type>c-type) | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  |     dup name>> parse-c-type simple-type ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  | : single-char? ( character-type -- ? )
 | 
					
						
							|  |  |  |     { [ drop character(1)-maps-to-char? ] [ dims>> product 1 = ] } 1&& ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | : fix-character-type ( character-type -- character-type' )
 | 
					
						
							|  |  |  |     clone dup size>> | 
					
						
							|  |  |  |     [ dup dims>> [ invalid-fortran-type ] [ dup size>> 1array >>dims f >>size ] if ] | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  |     [ dup dims>> [ ] [ f >>dims ] if ] if
 | 
					
						
							|  |  |  |     dup single-char? [ f >>dims ] when ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: character-type (fortran-type>c-type) | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  |     fix-character-type c:char simple-type ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : dimension>number ( string -- number )
 | 
					
						
							|  |  |  |     dup "*" = [ drop 0 ] [ string>number ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | : parse-out ( string -- string' out? )
 | 
					
						
							|  |  |  |     "!" ?head ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | : parse-dims ( string -- string' dim )
 | 
					
						
							|  |  |  |     "(" split1 dup
 | 
					
						
							|  |  |  |     [ ")" ?tail drop "," split [ [ blank? ] trim dimension>number ] map ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : parse-size ( string -- string' size )
 | 
					
						
							|  |  |  |     "*" split1 dup [ string>number ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | : (parse-fortran-type) ( fortran-type-string -- type )
 | 
					
						
							|  |  |  |     parse-out swap parse-dims swap parse-size swap
 | 
					
						
							| 
									
										
										
										
											2009-02-22 20:20:28 -05:00
										 |  |  |     >lower fortran>c-types ?at
 | 
					
						
							|  |  |  |     [ new-fortran-type ] [ misc-type boa ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : parse-fortran-type ( fortran-type-string/f -- type/f )
 | 
					
						
							|  |  |  |     dup [ (parse-fortran-type) ] when ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | GENERIC: added-c-args ( type -- args )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: fortran-type added-c-args drop { } ;
 | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  | M: character-type added-c-args fix-character-type single-char? [ { } ] [ { c:long } ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-05 17:39:23 -05:00
										 |  |  | GENERIC: returns-by-value? ( type -- ? )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | M: f returns-by-value? drop t ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 17:39:23 -05:00
										 |  |  | M: fortran-type returns-by-value? drop f ;
 | 
					
						
							|  |  |  | M: number-type returns-by-value? dims>> not ;
 | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  | M: character-type returns-by-value? fix-character-type single-char? ;
 | 
					
						
							|  |  |  | M: complex-type returns-by-value? | 
					
						
							|  |  |  |     { [ drop complex-functions-return-by-value? ] [ dims>> not ] } 1&& ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 17:39:23 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | GENERIC: (fortran-ret-type>c-type) ( type -- c-type )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  | M: f (fortran-ret-type>c-type) drop c:void ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 17:39:23 -05:00
										 |  |  | M: fortran-type (fortran-ret-type>c-type) (fortran-type>c-type) ;
 | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  | M: real-type (fortran-ret-type>c-type) | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  |     drop real-functions-return-double? [ c:double ] [ c:float ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 17:39:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | GENERIC: (fortran-arg>c-args) ( type -- main-quot added-quot )
 | 
					
						
							| 
									
										
										
										
											2009-02-06 11:06:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  | : args?dims ( type quot -- main-quot added-quot )
 | 
					
						
							|  |  |  |     [ dup dims>> [ drop [ ] [ drop ] ] ] dip if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | M: integer-type (fortran-arg>c-args) | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     [ | 
					
						
							|  |  |  |         size>> { | 
					
						
							| 
									
										
										
										
											2010-10-20 18:42:53 -04:00
										 |  |  |             { f [ [ c:int <ref>     ] [ drop ] ] } | 
					
						
							|  |  |  |             { 1 [ [ c:char <ref>    ] [ drop ] ] } | 
					
						
							|  |  |  |             { 2 [ [ c:short <ref>   ] [ drop ] ] } | 
					
						
							|  |  |  |             { 4 [ [ c:int <ref>     ] [ drop ] ] } | 
					
						
							|  |  |  |             { 8 [ [ c:longlong <ref> ] [ drop ] ] } | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |             [ invalid-fortran-type ] | 
					
						
							|  |  |  |         } case
 | 
					
						
							|  |  |  |     ] args?dims ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 11:06:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | M: logical-type (fortran-arg>c-args) | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     [ call-next-method [ [ 1 0 ? ] prepend ] dip ] args?dims ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: real-type (fortran-arg>c-args) | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     [ | 
					
						
							|  |  |  |         size>> { | 
					
						
							| 
									
										
										
										
											2010-10-20 18:42:53 -04:00
										 |  |  |             { f [ [ c:float <ref> ] [ drop ] ] } | 
					
						
							|  |  |  |             { 4 [ [ c:float <ref> ] [ drop ] ] } | 
					
						
							|  |  |  |             { 8 [ [ c:double <ref> ] [ drop ] ] } | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |             [ invalid-fortran-type ] | 
					
						
							|  |  |  |         } case
 | 
					
						
							|  |  |  |     ] args?dims ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 11:06:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | M: real-complex-type (fortran-arg>c-args) | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     [ | 
					
						
							|  |  |  |         size>> { | 
					
						
							|  |  |  |             {  f [ [ <complex-float>  ] [ drop ] ] } | 
					
						
							|  |  |  |             {  8 [ [ <complex-float>  ] [ drop ] ] } | 
					
						
							|  |  |  |             { 16 [ [ <complex-double> ] [ drop ] ] } | 
					
						
							|  |  |  |             [ invalid-fortran-type ] | 
					
						
							|  |  |  |         } case
 | 
					
						
							|  |  |  |     ] args?dims ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: double-precision-type (fortran-arg>c-args) | 
					
						
							| 
									
										
										
										
											2010-10-20 18:42:53 -04:00
										 |  |  |     [ drop [ c:double <ref> ] [ drop ] ] args?dims ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: double-complex-type (fortran-arg>c-args) | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     [ drop [ <complex-double> ] [ drop ] ] args?dims ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: character-type (fortran-arg>c-args) | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  |     fix-character-type single-char? | 
					
						
							| 
									
										
										
										
											2010-10-20 18:42:53 -04:00
										 |  |  |     [ [ first c:char <ref> ] [ drop ] ] | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  |     [ [ ascii string>alien ] [ length ] ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: misc-type (fortran-arg>c-args) | 
					
						
							|  |  |  |     drop [ ] [ drop ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  | GENERIC: (fortran-result>) ( type -- quots )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : result?dims ( type quot -- quot )
 | 
					
						
							|  |  |  |     [ dup dims>> [ drop { [ ] } ] ] dip if ; inline
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: integer-type (fortran-result>) | 
					
						
							| 
									
										
										
										
											2010-10-20 18:42:53 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         size>> { | 
					
						
							|  |  |  |             { f [ { [ c:int deref      ] } ] } | 
					
						
							|  |  |  |             { 1 [ { [ c:char deref     ] } ] } | 
					
						
							|  |  |  |             { 2 [ { [ c:short deref    ] } ] } | 
					
						
							|  |  |  |             { 4 [ { [ c:int deref      ] } ] } | 
					
						
							|  |  |  |             { 8 [ { [ c:longlong deref ] } ] } | 
					
						
							|  |  |  |             [ invalid-fortran-type ] | 
					
						
							|  |  |  |         } case
 | 
					
						
							|  |  |  |     ] result?dims ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: logical-type (fortran-result>) | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     [ call-next-method first [ zero? not ] append 1array ] result?dims ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: real-type (fortran-result>) | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     [ size>> { | 
					
						
							| 
									
										
										
										
											2010-10-20 18:42:53 -04:00
										 |  |  |         { f [ { [ c:float deref ] } ] } | 
					
						
							|  |  |  |         { 4 [ { [ c:float deref ] } ] } | 
					
						
							|  |  |  |         { 8 [ { [ c:double deref ] } ] } | 
					
						
							| 
									
										
										
										
											2009-02-06 11:06:22 -05:00
										 |  |  |         [ invalid-fortran-type ] | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     } case ] result?dims ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 11:06:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | M: real-complex-type (fortran-result>) | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     [ size>> { | 
					
						
							|  |  |  |         {  f [ { [ *complex-float  ] } ] } | 
					
						
							|  |  |  |         {  8 [ { [ *complex-float  ] } ] } | 
					
						
							|  |  |  |         { 16 [ { [ *complex-double ] } ] } | 
					
						
							| 
									
										
										
										
											2009-02-06 11:06:22 -05:00
										 |  |  |         [ invalid-fortran-type ] | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     } case ] result?dims ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 11:06:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | M: double-precision-type (fortran-result>) | 
					
						
							| 
									
										
										
										
											2010-10-20 18:42:53 -04:00
										 |  |  |     [ drop { [ c:double deref ] } ] result?dims ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: double-complex-type (fortran-result>) | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     [ drop { [ *complex-double ] } ] result?dims ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: character-type (fortran-result>) | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  |     fix-character-type single-char? | 
					
						
							| 
									
										
										
										
											2010-10-20 18:42:53 -04:00
										 |  |  |     [ { [ c:char deref 1string ] } ] | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  |     [ { [ ] [ ascii alien>nstring ] } ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: misc-type (fortran-result>) | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     drop { [ ] } ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | GENERIC: (<fortran-result>) ( type -- quot )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: fortran-type (<fortran-result>)  | 
					
						
							| 
									
										
										
										
											2010-12-25 19:54:45 -05:00
										 |  |  |     (fortran-type>c-type) \ heap-size \ <byte-array> [ ] 3sequence ;
 | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: character-type (<fortran-result>) | 
					
						
							|  |  |  |     fix-character-type dims>> product dup
 | 
					
						
							|  |  |  |     [ \ <byte-array> ] dip [ ] 3sequence ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : [<fortran-result>] ( return parameters -- quot )
 | 
					
						
							|  |  |  |     [ parse-fortran-type ] dip
 | 
					
						
							|  |  |  |     over returns-by-value? | 
					
						
							|  |  |  |     [ 2drop [ ] ] | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     [ [ (<fortran-result>) ] [ length \ ndip [ ] 3sequence ] bi* ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : [fortran-args>c-args] ( parameters -- quot )
 | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     [ [ ] ] [ | 
					
						
							|  |  |  |         [ parse-fortran-type (fortran-arg>c-args) 2array ] map flip first2
 | 
					
						
							|  |  |  |         [ [ \ spread [ ] 2sequence ] bi@ 2array ] [ length ] bi  | 
					
						
							|  |  |  |         \ ncleave [ ] 3sequence
 | 
					
						
							|  |  |  |     ] if-empty ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  | :: [fortran-invoke] ( [args>args] return library function parameters -- [args>args] quot )  | 
					
						
							| 
									
										
										
										
											2009-10-28 17:11:33 -04:00
										 |  |  |     return parameters fortran-sig>c-sig :> ( c-return c-parameters )
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  |     function fortran-name>symbol-name :> c-function | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     [args>args]  | 
					
						
							|  |  |  |     c-return library c-function c-parameters \ alien-invoke | 
					
						
							|  |  |  |     5 [ ] nsequence | 
					
						
							|  |  |  |     c-parameters length \ nkeep | 
					
						
							|  |  |  |     [ ] 3sequence ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : [fortran-out-param>] ( parameter -- quot )
 | 
					
						
							|  |  |  |     parse-fortran-type | 
					
						
							|  |  |  |     [ (fortran-result>) ] [ out?>> ] bi
 | 
					
						
							|  |  |  |     [ ] [ [ drop [ drop ] ] map ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : [fortran-return>] ( return -- quot )
 | 
					
						
							|  |  |  |     parse-fortran-type { | 
					
						
							|  |  |  |         { [ dup not ] [ drop { } ] } | 
					
						
							|  |  |  |         { [ dup returns-by-value? ] [ drop { [ ] } ] } | 
					
						
							|  |  |  |         [ (fortran-result>) ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : letters ( -- seq ) CHAR: a CHAR: z [a,b] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (shuffle-map) ( return parameters -- ret par )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  |         fortran-ret-type>c-type length swap void? [ 1 + ] unless
 | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |         letters swap head [ "ret" swap suffix ] map
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2009-08-13 20:21:44 -04:00
										 |  |  |         [ fortran-arg-type>c-type nip length 1 + ] map letters swap zip
 | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |         [ first2 letters swap head [ "" 2sequence ] with map ] map concat
 | 
					
						
							|  |  |  |     ] bi* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (fortran-in-shuffle) ( ret par -- seq )
 | 
					
						
							| 
									
										
										
										
											2009-08-02 21:13:59 -04:00
										 |  |  |     [ second ] sort-with append ;
 | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (fortran-out-shuffle) ( ret par -- seq )
 | 
					
						
							|  |  |  |     append ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : [fortran-result-shuffle] ( return parameters -- quot )
 | 
					
						
							|  |  |  |     (shuffle-map) [ (fortran-in-shuffle) ] [ (fortran-out-shuffle) ] 2bi <effect> | 
					
						
							|  |  |  |     \ shuffle-effect [ ] 2sequence ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : [fortran-results>] ( return parameters -- quot )
 | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     [ [fortran-result-shuffle] ] | 
					
						
							|  |  |  |     [ drop [fortran-return>] ] | 
					
						
							|  |  |  |     [ nip [ [fortran-out-param>] ] map concat ] 2tri
 | 
					
						
							|  |  |  |     append
 | 
					
						
							|  |  |  |     \ spread [ ] 2sequence append ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 11:06:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  | : (add-fortran-library) ( fortran-abi name -- )
 | 
					
						
							|  |  |  |     library-fortran-abis get-global set-at ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  | : add-fortran-library ( name soname fortran-abi -- )
 | 
					
						
							|  |  |  |     [ fortran-abi [ fortran-c-abi ] with-variable add-library ] | 
					
						
							|  |  |  |     [ nip swap (add-fortran-library) ] 3bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : fortran-name>symbol-name ( fortran-name -- c-name )
 | 
					
						
							|  |  |  |     mangle-name ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | : fortran-type>c-type ( fortran-type -- c-type )
 | 
					
						
							|  |  |  |     parse-fortran-type (fortran-type>c-type) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-05 17:39:23 -05:00
										 |  |  | : fortran-arg-type>c-type ( fortran-type -- c-type added-args )
 | 
					
						
							|  |  |  |     parse-fortran-type | 
					
						
							| 
									
										
										
										
											2010-02-21 19:27:36 -05:00
										 |  |  |     [ (fortran-type>c-type) <pointer> ] | 
					
						
							| 
									
										
										
										
											2009-02-05 17:39:23 -05:00
										 |  |  |     [ added-c-args ] bi ;
 | 
					
						
							|  |  |  | : fortran-ret-type>c-type ( fortran-type -- c-type added-args )
 | 
					
						
							|  |  |  |     parse-fortran-type dup returns-by-value? | 
					
						
							|  |  |  |     [ (fortran-ret-type>c-type) { } ] [ | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  |         c:void swap  | 
					
						
							| 
									
										
										
										
											2010-02-21 19:27:36 -05:00
										 |  |  |         [ added-c-args ] [ (fortran-type>c-type) <pointer> ] bi prefix
 | 
					
						
							| 
									
										
										
										
											2009-02-05 17:39:23 -05:00
										 |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-05 19:51:50 -05:00
										 |  |  | : fortran-arg-types>c-types ( fortran-types -- c-types )
 | 
					
						
							|  |  |  |     [ length <vector> 1 <vector> ] keep
 | 
					
						
							|  |  |  |     [ fortran-arg-type>c-type swapd [ suffix! ] [ append! ] 2bi* ] each
 | 
					
						
							|  |  |  |     append >array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : fortran-sig>c-sig ( fortran-return fortran-args -- c-return c-args )
 | 
					
						
							|  |  |  |     [ fortran-ret-type>c-type ] [ fortran-arg-types>c-types ] bi* append ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-10 17:39:24 -05:00
										 |  |  | : set-fortran-abi ( library -- )
 | 
					
						
							|  |  |  |     library-fortran-abis get-global at fortran-abi set ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  | : (fortran-invoke) ( return library function parameters -- quot )
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |         [ 2nip [<fortran-result>] ] | 
					
						
							|  |  |  |         [ nip nip nip [fortran-args>c-args] ] | 
					
						
							|  |  |  |         [ [fortran-invoke] ] | 
					
						
							|  |  |  |         [ 2nip [fortran-results>] ] | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     } 4 ncleave 4 nappend ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MACRO: fortran-invoke ( return library function parameters -- )
 | 
					
						
							| 
									
										
										
										
											2009-02-11 15:05:57 -05:00
										 |  |  |     { [ 2drop nip set-fortran-abi ] [ (fortran-invoke) ] } 4 ncleave ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 11:06:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-16 22:24:10 -04:00
										 |  |  | : parse-arglist ( parameters return -- types effect )
 | 
					
						
							| 
									
										
										
										
											2010-12-01 06:00:00 -05:00
										 |  |  |     [ | 
					
						
							|  |  |  |         2 group | 
					
						
							| 
									
										
										
										
											2010-11-30 20:34:15 -05:00
										 |  |  |         [ unzip [ "," ?tail drop ] map ] | 
					
						
							|  |  |  |         [ [ first "!" head? ] filter [ second "," ?tail drop "'" append ] map ] bi
 | 
					
						
							| 
									
										
										
										
											2010-12-01 06:00:00 -05:00
										 |  |  |     ] [ [ ] [ prefix ] if-void ] | 
					
						
							| 
									
										
										
										
											2009-09-16 22:24:10 -04:00
										 |  |  |     bi* <effect> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | :: define-fortran-function ( return library function parameters -- )
 | 
					
						
							|  |  |  |     function create-in dup reset-generic  | 
					
						
							| 
									
										
										
										
											2010-02-01 15:06:10 -05:00
										 |  |  |     return library function parameters return [ c:void ] unless* parse-arglist | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  |     [ \ fortran-invoke 5 [ ] nsequence ] dip define-declared ;
 | 
					
						
							| 
									
										
										
										
											2009-02-06 20:05:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 |  |  | SYNTAX: SUBROUTINE:  | 
					
						
							| 
									
										
										
										
											2010-04-12 19:09:26 -04:00
										 |  |  |     f current-library get scan ";" parse-tokens | 
					
						
							| 
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 |  |  |     [ "()" subseq? not ] filter define-fortran-function ;
 | 
					
						
							| 
									
										
										
										
											2009-02-09 14:29:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 |  |  | SYNTAX: FUNCTION: | 
					
						
							| 
									
										
										
										
											2010-04-12 19:09:26 -04:00
										 |  |  |     scan current-library get scan ";" parse-tokens | 
					
						
							| 
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 |  |  |     [ "()" subseq? not ] filter define-fortran-function ;
 | 
					
						
							| 
									
										
										
										
											2009-02-05 16:31:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 |  |  | SYNTAX: LIBRARY: | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  |     scan | 
					
						
							| 
									
										
										
										
											2010-04-12 19:09:26 -04:00
										 |  |  |     [ current-library set ] | 
					
						
							| 
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 |  |  |     [ set-fortran-abi ] bi ;
 | 
					
						
							| 
									
										
										
										
											2009-02-10 14:11:06 -05:00
										 |  |  | 
 |