C, C++, BF, BASH and Ruby; 280 chars
Score is about 0.040
#include "stdio.h"
#define s "C"
#ifdef __cplusplus
#define s "C++"
#endif
#ifndef s
#"+++++++++[>++++++++++>+++++++++<<-]>>-.<++++.>-.++++++++.<----.>---.<+++++++.>---.++++++++.<<++++++++++.[-]"
if [ 1 == 2 ];then
puts "Ruby"
exit
fi
echo "BASH"
exit
end
#endif
main(){puts(s);}
Note that I am using a Linux system.
The code is run or compiled with the following commands (the file's name is test.c
)
C:
gcc test.c
When run with ./a.out
, output is C
C++:
c++ test.c
When run with ./a.out
, output is C++
BASH:
./test.c
Outputs: BASH
Ruby:
ruby test.c
Outputs: Ruby
BrainF***:
Verified using the following:
Outputs: brainfuck
Note that if the JS debugger is used, then the first two minus signs need to be removed. They were included to offset the plus signs in the string literal "C++"
. This was a very fun project, I'm working on adding more languages.
Just to add further clarity, here are my interpreter's/compiler's specs:
gcc version 4.6.3
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
GNU bash, version 4.2.24(1)-release (x86_64-pc-linux-gnu)
SIDE NOTE
Using @baby-rabbit's trick I was able to extend my code to be executable in 7 languages (objective-C and objective-c++ being added). This is not my solution since I did copy some, but I thought I would show it off.
Update 9.12
Added SmallTalk run with gnu-smalltalk!
SmallTalk, C, C++, Objective-C, Objective-C++, BASH, BF, Ruby; 384 chars (Score: 0.059)
#if (a)
##(true) ifTrue: ['SmallTalk' printNl]
##(ObjectMemory quit)
#"+++++++++++[>++++++++++>+++++++++<<-]>>-.<++++.>-.++++++++.<----.>---.<+++++++.>---.++++++++.<<++++++++++.[-]"
if [ 1 == 2 ];then
puts 'Ruby'
exit
fi
echo 'BASH'
exit
end
=begin
#endif
#include "stdio.h"
main(){puts(
#ifdef __OBJC__
"Objective-"
#endif
"C"
#ifdef __cplusplus
"++"
#endif
);}
#ifdef b
=end
#endif
In the above code you will need to rename the file to produce the langauge's name for objective-c, obj-c++, c and c++.
print()
for Perl, Python, PHP, Processing and R be considered trivial or is it acceptable? – Rob Sep 10 '12 at 23:34n * n / length
– vsz Sep 11 '12 at 18:47