On Sat, Nov 14, 2009 at 12:48 PM, Marvin Humphrey
<[email protected]> wrote:
> Benefits:
>
>  * Big gain in terms of conceptual simplicity.
>  * The statement "Charmonizer uses C to bootstrap C" would be 100% true.
>  * We'd rid ourselves of the trunk/charmonizer/bin/metaquote utility app,
>    which is currently implemented in Perl.
>    * No more Perl dependency for Charmonizer.
>    * No more need to pre-generate files when prepping non-Perl distros.
>  * Slightly simpler build processes, since our build scripts will be able to
>    use the source files directly and won't have to run metaquote to get
>    usable C files.
>
> To achieve those gains, we have to resign ourselves to writing code like
> this...
>
>   "        printf(\"%%d\", 1);\n"
>
> ... instead of this...
>
>            printf("%%d", 1);
>
> ... to put this line into our mini test-compile:
>
>   printf("%d", 1);
>
> Thoughts?

Yes, ditch it.  The benefits are real, and the outside world really
dislikes Perl dependencies.  Quoting by hand isn't that hard, and you
can probably even make the preprocessor do most of the work for you:

#define QUOTE(x) #x "\n"
static char lseek_code[] =
    QUOTE(%s)
    QUOTE(#include "_charm.h")
    QUOTE(int main() {)
    QUOTE(  int fd;)
    QUOTE(  Charm_Setup;)
    QUOTE(  fd = open("_charm_lseek", O_WRONLY | O_CREAT, 0666);)
    QUOTE(  if (fd == -1) { exit(-1); })
    QUOTE(  %s(stdout, 0, SEEK_SET);))
    QUOTE(  printf("%%d", 1);)
    QUOTE(  if (close(fd)) { exit(-1); })
    QUOTE(  return 0;)
    QUOTE(})
    ;

I'm not sure whether "stringification" is standard or a GCC extension,
but Google seems to confirm that Visual Studio supports the same
syntax.

You can even do very clear multiline stuff if you aren't worried about
losing the newlines:
    QUOTE(
        int main() {
            int fd;
            Charm_Setup;
            fd = open("_charm_lseek", O_WRONLY | O_CREAT, 0666);
            if (fd == -1) { exit(-1); }
            %s(stdout, 0, SEEK_SET);
            printf("%%d", 1);
            if (close(fd)) { exit(-1); }
            return 0;
        }
      );

There are some oddities with regard to comments getting dropped, but
these can be manually quoted.  There's also an oddity with using
directives like "#include" as a multiline argument, but one can work
around this too (manual or single line QUOTE).  In my opinion it's
worth the small hassle for the greater simplicity.

Nathan Kurz
[email protected]

Reply via email to