I've written a program that prints "The best programming language ever is X"
replacing X with an appropriate string depending on the language used to compile/interpret it
see comments for the supported languages
#//<?php echo "\r \r";
#// Polyglot script
#// Copyright Mattia Basaglia 2011
#// Distributed under the Boost Software License, Version 1.0.
#// ( See http://www.boost.org/LICENSE_1_0.txt )
#// This file can be used as source/script for
#// C
#// C++
#// Objective-C
#// Objective-C++
#// Perl
#// PHP
#// Python
#// Ruby
#// POSIX Shell
#define function int
#ifdef __cplusplus
#include <iostream>
#define print std::cout <<
#ifdef __OBJC__
#include<stdio.h>
#endif
#else
#define print const char* c=
#include <stdio.h>
#endif
#/*?><?php /*
q="""
#{
# =+'
=begin
}'+'
#*/
function main()
{
/*
#{
# ";q=0;print() { echo $1 Shell; exit; # ?><?php /*
# =+'+'
=end
} # "
# """
#';
# */
#// The following line is executed by all languages
print "The best programming language ever is ";
#//?><?php echo "PHP\n"; return; ?>
#/*
q="""
#{
print "Ruby\n";
exit;
}
";q=+0;
print "Perl\n";
__END__=0;
'
"""
print "Python ( if only it didn\'t add trailing newlines :^/ )";
exit();
# ?>
q=""""
# */
#ifndef __cplusplus
printf("%s",c);
#endif
#ifdef __OBJC__
printf("Objective-");
#endif
#ifdef __cplusplus
std::cout << "C++" << std::endl ;
#else
printf("C\n");
#endif // <?php
}
#//?><?php main(); #'" """
It has been tested with the following:
GCC 4.5.2
PHP 5.3.5
perl v5.10.1
Python 2.7.1
GNU bash, version 4.2.8(1)
dash 0.5.5.1
ruby 1.8.7
On Trisquel 5.0 x86_64
If your favorite language is not listed feel free to try to include it :^P
PS: Does anyone know how to fix Python output without breaking other languages?
At first I thougt this was a question from some new kid that was posted to the wrong section(I rarely check the names until after I've read the headline). Then I read the post and thought "So what? Big deal." (Still not having read the name yet). Then I read the code... not a single jump statement of any kind or any dynamic links... Well done sir.
One question though, you have a preprocessor instructions that define "print" as "std::cout", why don't you use them? This is a really cool concept Bazzy I am truely impressed.
@chrisname
Making line 49 a function call will break shell and languages that will not have a way to rename print
@Computergeek01
What do you mean? I use print for C++ on line 49. I have preprocessed enough to make C-family compatible with all the others.
I've never liked the CPP much but it makes C-family very easy to be integrated with completely different languages