Programming/Chimera code
From Skypher
|
▼Main Page |
|
Chimera code is (script) code that can be compiled/interpreted/run successfully in multiple languages. It can be divided into two categories:
- Code that is interpreted in exactly the same way by both languages.
- Code that is interpreted differently in one language than it is in the other.
I've found it easy to create chimeric code of the second category; If you can create a header that causes the first language interpreter to skip a large chunk of the file, and the second language to not skip it, you can effectively split the file in two sections, once for each language. The following examples all use this technique:
Examples
HTML & JavaScript
example.js.html:
<!--/* HTML PART OF FILE STARTS HERE --><HTML>
This is a valid HTML page that loads itself as a JavaScript through a SCRIPT tag.
<SCRIPT language="JavaScript" src="?"></SCRIPT>
<!-- JAVASCRIPT PART OF FILE STARTS HERE */
alert("This is a valid JavaScript that displays a popup");
//--></HTML>
Batch script, Python and JavaScript
example.py.js.cmd:
@if (!@_jscript) == (!@_jscript) (ECHO OFF & GOTO :CMD) else X
### PYTHON #####################################################################
if __name__ == "__main__":
import sys
print 'Python, arguments: %s.' % repr(sys.argv[1:])
### PYTHON #####################################################################
"""
:CMD
::: BATCH ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO Batch, arguments: %0 %*
CSCRIPT /Nologo /e:jscript "%~f0" %0 %*
Python -x -B "%~f0" %0 %*
::: BATCH ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
GOTO :EOF
@end
/// JAVASCRIPT /////////////////////////////////////////////////////////////////
WScript.StdOut.Write("JavaScript, arguments:");
for (var i = 0; i < WScript.Arguments.length; i++) {
WScript.StdOut.Write(" \"" + WScript.Arguments(i) + "\"");
}
WScript.StdOut.WriteLine();
/// JAVASCRIPT /////////////////////////////////////////////////////////////////
//"""
Detailed example
Here is a more detailed example, in which I've created code in all three scripting languages that can be used to call code in any of the other languages. The example takes a bunch of command line options, each of which must have the value "cmd" "js" or "py". It takes the first argument and invokes the batch script, Python script or JavaScript part based on its value. It passes the remaining arguments to the script, which in turn takes the first, etc... Here's some example output:
E:\Chimera code>example.py.js.cmd py cmd js cmd py js py cmd js Batch -> Python Python -> Batch Batch -> JavaScript JavaScript -> Batch Batch -> Python Python -> JavaScript JavaScript -> Python Python -> Batch Batch -> JavaScript JavaScript: done
And here is the code, written for use with Python 2.6 (for 2.4, you'll need to remove the "-B" argument from the command line for Python in the Batch and JavaScript):
@if (!@_jscript) == (!@_jscript) (ECHO OFF & GOTO :MAIN)
### PYTHON #####################################################################
def RunAsJavaScript(argv):
import subprocess, sys
cscript_args = ['cscript.exe', '/nologo', '/e:jscript', sys.argv[0]]
cscript_args.extend(argv)
subprocess.call(args=cscript_args, shell=True, bufsize=0)
def RunAsBatch(argv):
import subprocess, sys
batch_args = [sys.argv[0]]
batch_args.extend(argv)
subprocess.call(args=batch_args, shell=True, bufsize=0)
def Main():
import sys
if len(sys.argv) == 1:
print 'Python: done'
elif sys.argv[1] == 'cmd':
print 'Python -> Batch'
RunAsBatch(sys.argv[2:])
elif sys.argv[1] == 'js':
print 'Python -> JavaScript'
RunAsJavaScript(sys.argv[2:])
else:
print 'Python -> unknown:%s' % repr(sys.argv[1])
### PYTHON #####################################################################
if __name__ == "__main__": Main(), """
::: BATCH ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:RunAsJavaScript
CScript.exe /nologo /e:jscript "%~f0" %*
GOTO :EOF
:RunAsPython
Python.exe -x -u -B "%~f0" %*
GOTO :EOF
:MAIN
IF "%~1"=="" (
ECHO Batch: done
) ELSE IF "%~1"=="py" (
ECHO Batch -^> Python
CALL :RunAsPython %2 %3 %4 %5 %6 %7 %8 %9
) ELSE IF "%~1"=="js" (
ECHO Batch -^> JavaScript
CALL :RunAsJavaScript %2 %3 %4 %5 %6 %7 %8 %9
) ELSE (
ECHO Batch -^> unknown:%1
)
::: BATCH ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
GOTO :EOF ;@end main()
/// JAVASCRIPT /////////////////////////////////////////////////////////////////
function RunCommand(command, argv) {
var command_line = ['"' + command + '"'].concat(argv).join(' ');
var wss = WScript.CreateObject("WScript.Shell");
var command = wss.Exec(command_line);
do {
var char = new String(command.StdOut.Read(1));
if (char == "") break;
WScript.StdOut.Write(char);
} while (1)
}
function RunAsPython(argv) {
python_argv = ['-x', '-u', '-B', '"' + WScript.ScriptFullName + '"']
for (i in argv) {
python_argv.push('"' + argv[i] + '"')
}
RunCommand("Python.exe", python_argv)
}
function RunAsBatch(argv) {
batch_argv = []
for (i in argv) {
batch_argv.push('"' + argv[i] + '"')
}
RunCommand(WScript.ScriptFullName, batch_argv)
}
function main() {
var argv = []
for (var i = 0; i < WScript.Arguments.length; i++) {
argv.push(WScript.Arguments(i));
}
if (argv.length == 0) {
WScript.StdOut.WriteLine("JavaScript: done");
} else if (argv[0] == "py") {
WScript.StdOut.WriteLine("JavaScript -> Python");
argv.shift()
RunAsPython(argv)
} else if (argv[0] == "cmd") {
WScript.StdOut.WriteLine("JavaScript -> Batch");
argv.shift()
RunAsBatch(argv)
} else {
WScript.StdOut.WriteLine("JavaScript -> unknown: " + argv[0]);
}
}
/// JAVASCRIPT /////////////////////////////////////////////////////////////////
//"""
(As you may notice in the code, stderr is not handled properly. Feel free to contribute a solution for this.)
Ruby script, C & C++
#define A "" /*
puts("This is Ruby!")
=begin */
#ifdef __cplusplus
#include <iostream>
using namespace std;
int main() {
cout << "This is C++!";
return 0;
}
#else
#ifdef __STDC__
#include <stdio.h>
int main(void) {
printf("This is C!");
return 0;
}
#endif
#endif /*
=end
# */
