Archived

This topic is now archived and is closed to further replies.

Sneftel

Polyglot programs...

Recommended Posts

...by which I mean programs that will compile and execute in TWO or more programming languages, and do similar or identical things. Anyone got any? here's one to start off... use it with C or Perl.
    
#include <stdio.h>

#define sub int

int main();
sub main() {
        printf("Just another %s hacker.\n",2/3?"Perl":"C");
}
    

Don't listen to me. I've had too much coffee. [edited by - sneftel on August 14, 2002 5:33:31 PM]

Share this post


Link to post
Share on other sites
There''s likely to be very little common syntax between different languages. At a guess, the "mostest polyglot" program is likely to be something like:


print "Hello world"


or the extremely useful:


0

which I''m pretty sure will run in Python, Ruby, Perl, Rebol, Lisp and Rexx and several others.

Share this post


Link to post
Share on other sites
Nahh... the trick is to make the programs work differently, but accomplish the same thing, through creatively getting each language to ignore part of the program.

Here's another one, between C++ and HTML:


  //<script language="none">

#include <stdio.h>

#include <string.h>

int main(){char text[14];
strncpy(text,"</script>Hello, World!<script language="+9, 13);//">

printf(text);return 0;}




Don't listen to me. I've had too much coffee.

[edited by - sneftel on August 14, 2002 7:51:28 PM]

[edited by - sneftel on August 14, 2002 7:52:18 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
Nahh... the trick is to make the programs work differently, but accomplish the same thing, through creatively getting each language to ignore part of the program.

The problem with that is either two different languages will recognise the same trick as meaning it is to ignore the same code, or the trick used will produce a syntax error.

Share this post


Link to post
Share on other sites
quote:
Original post by Beer Hunter
A zero-length file is both a quine and a polyglot of many different languages.

Ah, but is *no program* a valid program?

Share this post


Link to post
Share on other sites