Polyglot is a collection of examples of how to interface pieces of code written in different languages, and/or execute code in a runtime environment that's not traditional for its language. Calling a C++ library from a Python program, or executing OCaml code in a web browser, are two examples amongst what Polyglot demonstrates.
For the time being, it focuses on the following runtime environments:
- Native: code compiled for the native CPU
- Interpreted: code run by an interpreter compiled for the native CPU
And the following languages (with their traditional runtime environment):
Contributions are greatly appreciated.
Reasons to use several programming languages in the same program:
- reuse: a new program calls an existing code base in another language
- sharing: the same code runs on several environments
- performance: a part of a program is compiled natively for execution speed while other parts are interpreted to ease development
Examples by increasing complexity level
The examples demonstrates some problems encountered when interfacing several languages and/or runtime environments, and some solutions! Sometimes, several solutions are presented.
"Hello World" examples are written in a single language, but executed in a runtime environment that's not traditional for this language. This is most often achieved by some sort of cross-compilation.
Running on JS environment
C and C++
Many translators need to be evaluated.
- example: run a Python program in JS, using Transcrypt
Guest language calls
Show how to call code written in another language (the guest language), from code executed in its traditional runtime environment (the host language).
The guest language can either be compiled as usual and executed via a foreign call (e.g. a Python extension written in C), or it can be translated to the host language's runtime environment (e.g. using Js_of_ocaml).
C and C++
C and C++ have very similar tool chains, and C is mostly a subset of C++, but they are not directly binary-compatible. In particular, they have different calling convention and name mangling. This is referred globally as linkage.
Probably the simplest strategy to make C and C++ codes interact is to (re-)compile everything using a C++ compiler.
- example: call C code from a C++ program, compiling C code with C++ compiler
- example: call C++ code from a C program, compiling C code with C++ compiler
The next thing is to use
extern "C", which changes the linkage of a portion of the program.
This allows calling a pre-compiled library.
- example: call a C library from a C++ program, using
- example: call a C++ library from a C program, using
ctypes implements dynamic loading of C libraries. It allows calling a pre-compiled C library purely in Python.
- example: call a C library from a Python program, with a plain extension
- example: call a C++ library from a Python program, with a plain extension
- example: call a C++ library from a Python program, using Boost.Python
ctypes allows calling a pre-compiled C library purely in OCaml.
OCaml can also be extended in C.
Host language callbacks
Show how the code written in guest language can, in turn, call code written in the host language.