domingo, 14 de abril de 2013

Embedding Lua, embedding Guile

Lately I've spent a quite few hours hacking on Lua. I love its simplicity, and the way it exposes lots of inner aspects of the programming language, that are usually hidden from the user in other programming languages.

One of the cool aspects of lua is the easiness of embedding it into your C app. But let's make it more fun. Embed Guile also inthere.

Lua

The process to embed Lua in a C app is quite easy, and simple (for really simple things), but I guess it gets more cumbersome when the complexity of the embedding system increases. Being a stack based vm makes it non-trivial to write some embedding functions (recursive functions, for example).

Guile.

The way to embed guile into an app is also, really easy. And Powerful. And you're not bound to interact with it using a stack based machine, but you just register your functions, and use the generic SCM type for all inputs and outputs. I find it easier than Lua

The Code

Here's a minimal example that does some trivial calculations and the flux of the code passes from C to guile, guile calls another C function, and after this, we call lua which also calls a function we have defined in C. In the end it's doing something like (n! + 1).