adamsol / Pyxell

Multi-paradigm programming language compiled to C++, written in Python.
MIT License
54 stars 6 forks source link

C/C++ FFI? #8

Open phillvancejr opened 3 years ago

phillvancejr commented 3 years ago

Hello, I've skimmed Pyxell's documentation but didn't notice anything about an ffi, can Pyxell call C or C++? I assume that it has an easy cpp ffi since it compiles to c++.

adamsol commented 3 years ago

There is an undocumented extern keyword for declaring functions defined elsewhere. You can write something like this:

func sinh(x: Float): Float extern

After this declaration you can just use the sinh function in your Pyxell code. It works because cmath library is included by default.

To call functions from other libraries, you would also need to include a proper header file when compiling the C++ file (and probably also link the library if it's not header-only). Currently the only way to do so is to first transpile Pyxell code to C++ (with -s option), and then manually compile the C++ code with GCC or Clang (with -include <header_file> and optionally linking the library).

It's a good idea to add the full support for FFI (without 2-step compilation) and describe it in the documentation. Thanks!

adamsol commented 3 years ago

Actually, this issue can remain open so that I don't forget about it.