1

Is there any existing project or easy approach to running lua code in a common lisp program? I've seen a few Lisp in Lua projects but to my knowledge none of them implement full common lisp so don't offer an acceptable bridge.

I would think it wouldn't be that hard to either write a full on Lua interpreter in CL or to access Lua via CFFI but so far I've been unable to find any code bases that do this. Do they not exist or what? If not are there any existing code bases that would be a good place to start, e.g., having C code for embedding lua convenient for CFFI?

EDIT: To avoid confusion note that by CFFI I was referring to this lisp project for calling foreign functions not the python one.

12
  • Off-topic question (even if interesting one), since asking for resources. The memory model in Lisp and in Lua is quite different. But why do you need to call Lua from Lisp? Why can't you profit from Lisp homoiconicity and call Lisp scripts (not Lua ones)? Commented Jan 20, 2018 at 6:14
  • Sorry if it is offtopic. Is there a more appropriate place to ask? Commented Jan 20, 2018 at 6:15
  • As for the bit about why not just call Lisp it's about executing existing code written in Lua (particularly I'm wondering about the feasibility of running existing LuaTex code in common lisp though that of course involves all sort of other gunk) Commented Jan 20, 2018 at 6:16
  • Probably not here. But I recommend a wider approach, and then you might ask a different question, perhaps on softwareengineering.stackexchange.com. You need to explain why you specifically want to call Lua (given that Lisp has a similar, but slightly more powerful, semantics). The mention of LuaTeX should go into your question. I would recommend using an external process (for which some Lisp implementations have solutions) Commented Jan 20, 2018 at 6:17
  • 1
    Because I mean the common foreign function interface: common-lisp.net/project/cffi Commented Jan 20, 2018 at 9:15

1 Answer 1

6

If you consider the lua implementation (the actual lua-5*.tar.gz) as a library coded in C that you want to call from Common Lisp, your question becomes how to call a C foreign function from Common Lisp (i.e. asks about foreign function interface of your Common Lisp implementation). The answer is of course implementation specific. For SBCL, read its §8 Foreign Function Interface chapter. For CLisp, read §32.3. The Foreign Function Call Facility. The Common Lisp CFFI might be helpful. You might want to interface the Lua API to your Common Lisp implementation (but I'm guessing you don't need to; you probably want to run the lualatex program in a different process).

If you consider Lua as a programming language specification (with its syntax and semantics, written in English in some report) you could also write your own Lua interpreter in Lisp. Since Lua is simple, that might be easy (but is it really worthwhile? Probably you'll need to reimplement many Lua primitives).

According to your comments, you might be interested in LuaTeX or LuaLaTeX. Then you really want to start a different process running that. And several Lisp implementations provide some way for this, for example SBCL provides run-program and you might want more inter-process communication (on Linux, that might be unix(7) sockets, or fifo(7), or pipe(7)...). Many Lisp implementations provide some ways to use these. See their documentation.

If you need to understand more how to have several processes working together on Linux -one of them being e.g. lualatex- , read some Linux programming book, perhaps the old ALP (freely downloadable), then intro(2) & syscalls(2). The poll(2) multiplexing system call is relevant, to be used in your event loop. Many Lisp implementations provide ways to do such system calls; for SBCL look into sb-posix

(asking for explicit software resources is off-topic on SO)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks,however, I was assuming that directly engaging with the foreign function interface to handle Lua as basically C code would have a fair bit of overhead managing memory and stuff (maybe I'm wrong) and I was wondering if there was any tool or existing library that would handle that part without me having to muss with C level cruft.
Indeed, Lua and Lisp have different memory semantics. How to deal with that is a different (and too broad) question
You really should improve your question with more motivation and context. I am guessing (perhaps wrongly) that you want to make LuaLaTeX and some Common Lisp application work together. You need explain that much more (and if my guess is wrong, you need to explain why you are asking, and provide some context and explain what kind of software you have in mind, on which implementation, etc)
Many interesting questions (like yours) are lacking some context & motivation. We can't guess what you really have in mind and what is your software project, and knowing that is generally helpful (at least to avoid XY problems...) If your code is free software -as it is the case for many academics- providing some link in your question is really helpful (for others to answer it). Just a few sentences explaining the context can be tremendously useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.