(most of this README is ai generated)
polyglot is a small tool that merges two source files (for example C++ + Python) into a single ".cpp" output file that can be used with C++ toolchains while preserving the other-language portion.
The generated output wraps sections so the C/C++ compiler ignores the other-language code and the interpreter for that language sees the section as a normal script region. The main program (main.cpp) validates syntax using external checkers (for example g++ for C++ and pyflakes for Python) before merging. This tool can be used to produce a single file that behaves differently when run by Python (or another interpreter) or compiled as C++.
For example, to merge language A (e.g. C++) and language B (e.g. python), the comments in language B (r'''...''') needs to be ignored in language A (via wrapping in # if 0 ... #endif), and the comments in language A (#if 0 ... #endif) needs to be comments in language B.
- g++ (for C++ syntax checking and compiling the tool)
- pip (to install
pyflakesif absent) pyflakesfor Python syntax checking (optional, fallback =python3 -m py_compile)
The tool also supports syntax checking for other languages via system tools:
- Ruby (
ruby -c) — files with.rb - Bash (
bash -n) — files with.sh - Perl (
perl -c) — files with.pl
From the repository root:
- Compile the tool:
-
g++ main.cpp -o polyglot
-
polyglot expects two source files and an output file specified with the -o option:
polyglot <source1> <source2> -o <outputFile>The order of the two source files doesn't matter. The program determines which checks to run based on file extensions.
Example (using the sample files in this repo):
./polyglot ./test/test.cpp ./test/test.py -o ./test/out.cppAfter running, check your chosen output file (for example ./test/out.cpp) for the merged result.
Note that you need bash, ruby, and perl in addition to g++ and python installed for the test runner to work smoothly for all supported languages.
g++ test_runner.cpp -o runtests;
./runtestsNotes & Troubleshooting
- If
pyflakesis not installed the tool will print the error from the attempted check; install it or skip using Python source files.- Install manually:
python -m pip install pyflakes
- Install manually:
- On some systems the
pyflakesexecutable might not be on PATH; usepython -m pyflakes <file>.pyinstead. - The tool runs external commands (via popen). Ensure
g++,bash,ruby, andperlare available if you use those source file types. - The output file is a
.cppfile that will compile as C++ and can also be run by an interpreter (for examplepython out.cpp).
- test/test.cpp — simple C++ example
- test/test.py — simple Python example
- test/out.cpp — example of a previously merged output
g++ main.cpp -o polyglot
./polyglot ./test/test.cpp ./test/test.py -o ./test/out.cpp
python ./test/out.cpp
g++ ./test/out.cpp -o ./test/out
./test/outSmall fixes and improvements are welcome. Open an issue or submit a PR. Current issues:
- Find other pairs of languages (difficulty ~= 2)
MIT License