polyglot programming

via a comment thread at Good Math, Bad Math comes this polyglot program that compiles in 8 different programming languages:

  1. COBOL (ANSI)
  2. Pascal (ISO)
  3. Fortran (ANSI, f77)
  4. C (ANSI-ish)
  5. PostScript
  6. Linux/Unix shell script (bash, sh, csh)
  7. x86 machine language (MS-DOS, Win32, Linux)
  8. Perl (version 5)

Hideous and elegant simultaneously. All it does is write, “Hello, polyglot.” to stdout. The trick is clever use of comment strings.

Why yes, it is indeed 4am and the baby has been keeping us awake forever.

2 thoughts on “polyglot programming”

  1. There was a C program I saw about 30 years ago which, when compiled and run, would print out its own source.

    That’s a hell of a lot tougher than it sounds. The trick in that case was careful (ab)use of macros.

  2. Steven, that is called a _quine_ – check out “the original post”:http://scienceblogs.com/goodmath/2007/04/true_pathology_a_multilingual.php and comments at Good Math, Bad Math for many more examples. There’s even an entire programming language where “the only way to loop is to quine the program”:http://scienceblogs.com/goodmath/2006/11/friday_pathological_programmin_5.php! Cool but esoteric 🙂

    Here’s a link to quines in all the major programming languages. And here’s a simple quine in C:

    #include
    main(){char*c="\\\"#include
    %cmain(){char*c=%c%c%c%.102s%cn%c;
    printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]);exit(0);}\n";printf(c+2,c[102],c[1],*c,*c,c,*c,c[1]);exit(0);}

Comments are closed.