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).

jueves, 11 de abril de 2013

Running a shell command on current file

Before being an emacs evangelist, I was a vim evangelist for a few years (next month sublime text), and one feature that I missed in emacs was having a way to reference the current file when executing a command.

In vim, it's pretty common to run commands like

:!gcc %

It's pretty simple and the syntax is really easy to remember, ":" for command mode, "!" to run something, and in the command "%" will be replaced by your filename.

I haven't found anything similar to "%" for emacs, so let's write some elisp to fix it.


(defun shell-execute ()
  (interactive)
  (let ((file-buffer (or (buffer-file-name) ""))
        (command (read-shell-command "Shell command: " nil nil nil)))
    (shell-command (replace-regexp-in-string "%" file-buffer command))))

(global-set-key (kbd "M-!") 'shell-execute)

domingo, 7 de abril de 2013

Guile's deprecation warnings

I've been hacking a bit of Guile (for embedding purposes), and somehow I got to use some deprecated functions: scm_int2num and scm_num2int. The amazing thing is that along with the result of the program, you get the following warning message:
Some deprecated features have been used.  Set the environment
variable GUILE_WARN_DEPRECATED to "detailed" and rerun the
program to get more information.  Set it to "no" to suppress
this message.
Nice, no? Let's set the variable and rerun.
`scm_int2num' is deprecated. Use scm_from_int instead.
`scm_num2int' is deprecated. Use scm_to_int instead.
These kind of things make usin guile a pleasure. Now back to embedding.