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.

Peter Gerdes
  • 2,288
  • 1
  • 20
  • 28
  • 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)? – Basile Starynkevitch Jan 20 '18 at 06:14
  • Sorry if it is offtopic. Is there a more appropriate place to ask? – Peter Gerdes Jan 20 '18 at 06: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) – Peter Gerdes Jan 20 '18 at 06:16
  • Probably not here. But I recommend a wider approach, and then you might ask a *different* question, perhaps on https://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) – Basile Starynkevitch Jan 20 '18 at 06:17
  • Ok, I just presumed this was acceptable to ask here since this question asking for essentially the same info re: python and lisp seems to be upvoted and not flagged. If I phrased my question that way would it make a difference? https://stackoverflow.com/questions/5174199/is-there-a-simple-way-to-use-python-libraries-from-common-lisp – Peter Gerdes Jan 20 '18 at 06:21
  • That [question about Python & Lisp](https://stackoverflow.com/questions/5174199/is-there-a-simple-way-to-use-python-libraries-from-common-lisp) is slightly different, since Python is understood as an *implementation* so that question could be understood as "how to do" something, not as "find a resource for something" question. However, indeed that other question could also be seen as off-topic – Basile Starynkevitch Jan 20 '18 at 08:07
  • Your question might need an additional paragraph about your motivation & context. If you want to run `lualatex` from Common Lisp, you should write that in your question (and give more details). So please **edit your question** to improve it – Basile Starynkevitch Jan 20 '18 at 08:34
  • [CFFI](http://cffi.readthedocs.io/en/latest/#) is for Python. Why do you mention it in a Common Lisp question? – Basile Starynkevitch Jan 20 '18 at 08:53
  • 1
    Because I mean the common foreign function interface: https://common-lisp.net/project/cffi/ – Peter Gerdes Jan 20 '18 at 09:15
  • Anyway since it off topic should I delete it or not? I was going to do so but the delete messages suggests caution there. – Peter Gerdes Jan 20 '18 at 09:31
  • Don't delete your (interesting) question, but take a few minutes to improve it. It might be some [XY problem](http://xyproblem.info), but I am not sure.. Once you improve it I will retract my close vote. A paragraph for motivation and context could make your question on topic. I had to read in your mind to answer. Avoid commenting your own question, but do take time to improve it several times. – Basile Starynkevitch Jan 20 '18 at 09:33
  • After several hours, I downvoted your interesting question, because you still did not give additional *context* and *motivation*; it would be really nice if you could give them by editing and improving your question, please. The more I think about your question, the more it smells like an [XY problem](http://xyproblem.info) – Basile Starynkevitch Jan 20 '18 at 14:12

1 Answers1

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)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 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. – Peter Gerdes Jan 20 '18 at 09:18
  • Indeed, Lua and Lisp have different memory semantics. How to deal with that is a different (and too broad) question – Basile Starynkevitch Jan 20 '18 at 09:22
  • 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) – Basile Starynkevitch Jan 20 '18 at 09:24
  • 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](http://xyproblem.info/)...) If your code is [free software](https://en.wikipedia.org/wiki/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. – Basile Starynkevitch Jan 20 '18 at 09:28