factor/extra/size-of/size-of.factor

39 lines
970 B
Factor
Raw Normal View History

2008-02-27 17:08:57 -05:00
USING: kernel namespaces sequences
2008-03-07 18:53:20 -05:00
io io.files io.launcher io.encodings.ascii
bake builder.util
accessors vars
math.parser ;
2008-02-27 17:08:57 -05:00
IN: size-of
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
VAR: headers
: include-headers ( -- seq )
2008-06-30 11:26:18 -04:00
headers> [ `{ "#include <" , ">" } to-string ] map ;
2008-02-27 17:08:57 -05:00
: size-of-c-program ( type -- lines )
2008-06-30 11:26:18 -04:00
`{
2008-02-27 17:08:57 -05:00
"#include <stdio.h>"
include-headers
2008-03-07 18:53:20 -05:00
{ "main() { printf( \"%i\" , sizeof( " , " ) ) ; }" }
2008-02-27 17:08:57 -05:00
}
2008-06-30 11:26:18 -04:00
to-strings ;
2008-02-27 17:08:57 -05:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: c-file ( -- path ) "size-of.c" temp-file ;
: exe ( -- path ) "size-of" temp-file ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: size-of ( type -- n )
2008-03-07 18:53:20 -05:00
size-of-c-program c-file ascii set-file-lines
2008-02-27 17:08:57 -05:00
{ "gcc" c-file "-o" exe } to-strings
[ "Error compiling generated C program" print ] run-or-bail
exe ascii <process-reader> contents string>number ;