add extra:size-of

db4
Eduardo Cavazos 2008-02-27 16:08:57 -06:00
parent 5b0ab02d65
commit fdc812d307
1 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,46 @@
USING: kernel namespaces sequences
io io.files io.launcher bake builder.util
accessors vars ;
IN: size-of
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
VAR: headers
: include-headers ( -- seq )
headers> [ { "#include <" , ">" } bake to-string ] map ;
: size-of-c-program ( type -- lines )
{
"#include <stdio.h>"
include-headers
{ "main() { printf( \"%i\\n\" , sizeof( " , " ) ) ; }" }
}
bake to-strings ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: c-file ( -- path ) "size-of.c" temp-file ;
: exe ( -- path ) "size-of" temp-file ;
: answer ( -- path ) "size-of-answer" temp-file ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: size-of ( type -- n )
c-file
[ size-of-c-program [ print ] each ]
with-file-writer
{ "gcc" c-file "-o" exe } to-strings
[ "Error compiling generated C program" print ] run-or-bail
<process*>
{ exe } to-strings >>arguments
answer >>stdout
>desc run-process drop
answer eval-file ;