So it’s possible to write polyglot code that both compiles in C and runs in PHP, with a couple of tricks and helper functions. Example code here: pholyglot/polyglot.c at main · olleharstedt/pholyglot · GitHub
Now I’m thinking about writing a transpiler that will eat a subset of modern PHP code, and spit out polyglot PHP+C code. Of course using Menhir and OCaml.
- Performance in my snippet is 400% faster in the C version than PHP, despite being “same” code, so it could be a way to optimize parts of a code base, without people having to learn a new language (I chose testing input/ouput instead of numerical calculations, because obviously C will be faster in the latter case).
- Associative array in PHP has to go, instead do C arrays, linked lists and “normal” hashtables with string keys.
- Typing, but can probably be solved by requiring arguments to be typed in functions, and infer types of variables based on usage.
- Inheritance and interfaces, I’d have to test some more here… AFAIK, you can do OOP in C by manually writing a vtable; interfaces would just be
void*, I guess? Classes without methods can be compiled to structs easily.
- Memory management. Just don’t collect…? And make sure the program is short, lol. Or slap Boehm GC on it.
- Partial compilation in C meets include files in PHP. Dunno how to solve yet. Maybe just glue everything together as one file upon compilation.
This is similar to Facebook’s project to compile PHP to C++, but they were hindered by the fact that they had to support the full language (or 99%). By applying only a subset, I think you can squeeze out a much higher performance gain.
Why compile to polyglot code instead of just C or C++? To smooth out the transition between two languages or ecosystems, maybe? And lower the threshold of commitment; you can easily “rollback”, in a sense.
Maybe more related to programming language design than OCaml itself, but, there you go.