Polyglot Programming - HacKSU Lesson
print as reference · Ctrl+P
python demo.c and gcc demo.c -o demo && ./demo - tell them both actually run right now from the same unmodified file.#if 0), and file-format-level rules (JPEG/ZIP).<style> to the CSS parser and <script> to the JS engine. Three parsers, one file, by design.<style> tags. Everything outside is invisible to it."""...""" hides HTML from Python, and <!-- --> comments hide Python from the browser.examples/bash_python.sh they can run right now.cat image.jpg archive.zip > both.jpg literally works - try it live if you want.open image and unzip work on the result.#if 0 is a preprocessor directive. The preprocessor runs before the compiler even sees the code. It strips out the entire block. The compiler never knows the Python code existed.#if 0 is just a hash comment - the # character makes the rest of the line invisible to Python. Then x = """ opens a triple-quoted string and everything until the closing """ is just string data assigned to x..c extension. It reads file content, not filenames. Same with GCC.python examples/demo.cgcc examples/demo.c -o demo && ./demo
"exec" is still the exec builtin."exec" "python3" are silently concatenated at compile time into one string - this is a little-known Python feature.$0 and $@ are NOT expanded by Python - they're literal dollar signs. Only bash expands shell variables.exec replaces the entire shell process with Python - bash is gone and Python takes over running the same file from the top.bash examples/bash_python.shpython3 examples/bash_python.sh
.py.js - Node.js refuses to execute files with other extensions in certain module modes.npm install -g polycompilerecho 'print("Hello from Python!")' > hello.pyecho 'console.log("Hello from JavaScript!")' > hello.jspolycompiler hello.py hello.js out.py.jspython3 out.py.js # prints: Hello from Python!node out.py.js # prints: Hello from JavaScript!cat out.py.js1 // (lambda: exec("print('Hello from Python!')", globals()) or 1)()lambda: eval("console.log('Hello from JavaScript!')")// is the pivot point. In Python it's an operator - both sides are evaluated. In JS it's a comment - everything after it on that line disappears.label: statement) are a rarely-used feature - most JS developers have never written one. lambda: is a perfectly valid label in JS because lambda is not a reserved word there.lambda: starts a zero-argument lambda expression. Since it's never followed by a call (), the lambda object is created and immediately discarded.python3 examples/python_js.py.jsnode examples/python_js.py.jscat examples/python_js.py.js
"""...<!-- """ to hide the HTML header and <!-- ... --> to hide the Python footer. See examples/html_python.html.return 0;, putting Python code inside the C block, or not closing the triple-quoted string before print().greet() function in C and a matching def greet() in Python inside their respective sections.#if 0
x = """
#endif
#include <stdio.h>
int main(void) {
printf("Hello from C!\n");
return 0;
}
#if 0
"""
print("Hello from Python!")
#endif
#if 0
x = """
#endif
#include <stdio.h>
void greet(const char *name) {
printf("Hello, %s! (from C)\n", name);
}
int main(void) {
printf("Hello from C!\n");
greet("HacKSU");
return 0;
}
#if 0
"""
def greet(name):
print(f"Hello, {name}! (from Python)")
print("Hello from Python!")
greet("HacKSU")
#endif