Quines [remix]

As the quines in the previous post were criticized as boring and ordinary :-P, I did a remix:

#include <stdio.h>
#define s char s[]
s="#if 0\nimport json;r=json.dumps\nprint'#include <stdio.h>\\n#define s char s[]\\ns=%s;\\n%s'%(r(s),s)\n\"\"\" \"\n#elif 1\n#undef s\nint main(void)\n{\n  char *t = s;\n  printf(\"#include <stdio.h>\\n#define s char s[]\\ns=\\\"\");\n  while (*t)\n  {\n    if (*t == '\\n')\n      printf(\"\\\\n\");\n    else if (*t == '\"')\n      printf(\"\\\\\\\"\");\n    else if (*t == '\\\\')\n      printf(\"\\\\\\\\\");\n    else\n      printf(\"%c\", *t);\n    t++;\n  }\n  printf(\"\\\";\\n%s\\n\", s);\n  return 0;\n}\n#elif 0\n\" \"\"\"\n#endif";
#if 0
import json;r=json.dumps
print'#include <stdio.h>\n#define s char s[]\ns=%s;\n%s'%(r(s),s)
""" "
#elif 1
#undef s
int main(void)
  char *t = s;
  printf("#include <stdio.h>\n#define s char s[]\ns=\"");
  while (*t)
    if (*t == '\n')
    else if (*t == '"')
    else if (*t == '\\')
      printf("%c", *t);
  printf("\";\n%s\n", s);
  return 0;
#elif 0
" """

You can check that it works on the web or by downloading the file and testing it:

$ python polyquine.c | diff polyquine.c -
$ gcc -ansi -pedantic -Wall polyquine.c -o polyquine && ./polyquine | diff polyquine.c -

I’m aware that there are some impressive examples out there, but I haven’t analyzed them to avoid spoiling the fun.

What other language should I add? Reader contributions are welcome!

2 thoughts on “Quines [remix]

  1. Demian says:

    This is awesome!

    I had never heard of poliglot programs before; let alone poliglot quines =D

    The Ocaml/Haskell/Scheme and the C/Perl/Python quines are beyond insanity hehe

  2. Jason says:

    I’ve written quines in HPRPL on my HP50g than can change between “flavors” of different quines as well as add to a counter in an intron program. Is this considered a self-mod polyquine in a single language? Can an quine OS polyglot be written that can handle cross platform programs? Example is a OS polyglot that can change from MacOS –> Linux–> Windows Can someone please take the HPCALC HP50g+ emu and make an emu compatible and user friendly for windows? RPL/2 isn’t supported and I don’t want to use linux.

Leave a Reply to Jason Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s