Richard Suchenwirth 2001-01-02 - A couple of holidays without work or family gave me the opportunity to install the Cygwin suite (gcc and lotsa Unix tools) on my W95/P200 box at home, and play with C. I soon noticed that some monotonous tasks could be better delegated to a program, and that would of course be in Tcl. So in a few days, I wrote three code generators with the following features:
cproc strrev {s} { /* Revert a string */ char *cp = s+strlen(s); while(cp > s) putchar(*--cp); } strrev "A man, a plan, a canal: Panama" ;#= amanaP :lanac a ,nalp a ,nam A
gcc mywish.c -o mywish -s -Wall -ltcl80 -ltk80
The -s switch strips all symbols from the executable, bringing it down from over 300k to just 3.5k - pretty amazing for a custom wish... Compilation time is 2..3 sec, starting up an executable from Tcl takes ~90 msec.
Here's how a wish extended with strrev (in a different implementation, swapping chars in place this time) is specified:
cxtend -Tk 1 -name mywish -cc gcc -ccflags {-s -Wall} -cmd { strrev {char* s} { char *cp0, *cp1, t; for(cp0=s, cp1=s+strlen(s)-1; cp1 > cp0; cp0++, cp1--) { t=*cp0; *cp0=*cp1; *cp1=t; } } {char* s} }
AK: Note GNU Lightning, a library for on-the-fly generation of executable machine code. - RS 2007-10-02: I like Odyce for that purpose most, these days...
I suppose that this would also be appropriate to mention here?
What: Embedded C (EC) Where: ftp://ftp.reed.edu/pub/users/greaber/ec-0.1.tar.gz Description: Allows you to include C code in your Tcl scripts, compiling and dynamically loading it on the fly. The code will also be cached so the next time you run the program, you don't wait for it to compile. Known to work on DEC OSF/1 V3.2 and SunOS 5.5. Not yet ported to Macintosh or Windows. Updated: 09/1996 Contact: mailto:[email protected] (Grant Reaber)
Also, Critcl builds C extensions on-the-fly.