FascinatedBox / lily

Interpreted language focused on expressiveness and type safety.

Home Page:http://lily-lang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calling external functions/FFI

mindbound opened this issue · comments

What would be the MWE for calling external (C) functions from Lily code? I can't seem to find a sample or a documentation entry for this. TIA.

So far what I've done to bridge the gap between C and Lily is to write bridge modules. Tiny modules that are wrappers over the library that you're interested in and which export the data back in a way that the calling interpreter can make sense of it.

I have a repository: https://github.com/FascinatedBox/lily-parsekit. The relevant tool in there is 'bindgen.lily'. That repository contains some examples in its documentation of how to write bindings. The other tool in the repository, 'docgen.lily' can generate a documentation page using the same information. For that, you'll need to pull the lily-mkdir repo I have.

The lily-mkdir repo (https://github.com/fascinatedbox/lily-mkdir) is the simplest binding that there is. It wraps only over a single function, and it does not have requirements. If what you're looking to bind over doesn't have classes or something like a class (ex: It's not like gtk/qt, but more 'flat'), then it's a good start. All you'd need to do is to add more functions.

Writing bindings is a cycle of: Writing a special double-star comment over the target function explaining what it maps to in Lily, calling bindgen to add the entry to the table, then completing the function to do what's advertised.

Thank you, I think I got the idea.