Skip to main content
GameDev.net gamedev.net
🔒 Locked

Polyglot programs...

Started by Sneftel Aug 14, 2002 at 3:24 PM 7 replies 750+ views
Original Post
Sneftel
Sneftel
...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]
SabreMan
SabreMan
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.
Sneftel
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.

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]
SabreMan
SabreMan
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.
Beer Hunter
Beer Hunter
A zero-length file is both a quine and a polyglot of many different languages.
SabreMan
SabreMan
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?
Beer Hunter
Beer Hunter
Yes. Under some languages, it''s an error, but in others, it''s a program that does nothing.
SabreMan
SabreMan
I meant in the philosophical sense.
Beer Hunter
Beer Hunter
Okay, but what good has philosophy done for us lately?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.