Source files which work with ... several languages

One can write a single source code file which works unmodified with all of Perl, Python, Ruby, Tcl, C, and C++.

This is a somewhat silly extension of this page.

This code

#if 0 /* not-c-begin */ /*
#=====================================================================
["list" , {#} { { not-tcl-begin
}];
0 and eval('false') and length <<2;
"""  #"  not-python-begin
2
BEGIN{ 0 or eval('package _TMP; use Filter::Simple sub {
  my $wipe = sub { my $x="$1"; $x=~ tr/\n/ /c; $x };
  s{^(.*?\n__before_perl_code__[^\n]*)}{&$wipe($1)}se;
  s{(\n__after_perl_code__.*?\n#____END____[^\n]*)}{&$wipe($1)}se;
  }; import _TMP; undef(%_TMP::);'); }
#=====================================================================

def hello()
  print "Hi there ruby!\n";
end
hello();

#=====================================================================
__before_perl_code__ = <<'#____END____';  #not-ruby-begin
#=====================================================================

sub hello {
  print "Hi there perl!\n";
}
hello();

#=====================================================================
__after_perl_code__
 */
#endif /* not-c-end */
#ifdef __cplusplus
/*==================================================================*/

#include <iostream>
void hello() {
  cout << "Hi there C++!\n";
}
int main() { hello(); return 0; }

/*==================================================================*/
#else
/*==================================================================*/

#include<stdio.h>
void hello() {
  printf("Hi there C!\n");
}
int main() { hello(); return 0; }

/*==================================================================*/
#endif
#if 0 /* not-c-begin */ /*
} ; list "list"]  #not-tcl-end
#=====================================================================

proc hello {} {
  puts "Hi there tcl!"
}
hello

#=====================================================================
list {    not-tcl-begin
 """  #"  not-python-end
#=====================================================================

def hello() :
  print "Hi there python!"
hello();

#=====================================================================
0 and "} {" #} #not-tcl-end
#not-ruby-end
#____END____
#=====================================================================
# */
#endif /* not-c-end */
yields this
$ perl -w file.ppr; ruby -w file.ppr; python file.ppr ; tcl file.ppr
Useless use of single ref constructor in void context at file.ppr line 3.
Hi there perl!
Hi there python!
Hi there ruby!
Hi there tcl!
$ cp file.ppr file.c; gcc -Wall file.c; ./a.out ; g++ -Wall file.c; ./a.out
Hi there C!
Hi there C++!
At least with perl 5.6.0 and 5.8.2, ruby 1.8.1, python 1.5.2 and 2.1.1, tcl 8.3.3, and gcc/g++ 2.96.

Notes

Yes, this is getting a bit silly. I stumbled on a language "puzzle" site this afternoon and was curious whether a few more languages could be stuffed in. Though if tcl was working without perl warnings, I'd be tempted to add it to the "real" version.

I don't know of a way to include tcl with python and perl while avoiding perl's "useless {}" warning. :(

Including C/C++ adds a bit of fragility to the file. If other languages' code both terminates the C/C++ comment (ie, "*/"), and uses single quotes in a non-C-ish way, then the C/C++ preprocessor's tokenizer will choke. One could work around this by adding additional #/* to one's non-C/C++ code (having excess ones is fine).


Mitchell N Charity <mncharity@vendian.org>
Notes:

Doables:
 Discuss design.  Colorize and popups for clarity?

History:
 2004-Feb-19  Created and online.