-1

I am using Cent OS 7.

I want to write a program(written in Java or other languages) that can interact with Lua interpreter. I hope that my program can feed statements to Lua interpreter, and get executed real-time, while previous variables are still available.

For example, my program feeds a = 4; print(a); to Lua interpreter 4 is printed on the screen. Then the program does other work. Later it feeds n = 0; for i=1,4 do n = n + i; end; print(n); to the interpreter, and 10 is printed on the screen.

Note: All I want is that Lua interpreter execute the statements when my program feeds it one, while keeping its previous status. My program does not need to access variables in Lua interpreter.

I tried calling Lua interpreter separately, but it doesn't work as expected. Another solution is to record all previous statements, and run them before a new statement is going to run. But this is obviously not efficient.

Is there a easy way to do this? Such as just creating sub-process and making system calls?

Zehui Lin
  • 186
  • 9
  • I don't know specifically about the Lua interpreter, but if you can call it from the terminal, you can probably use the `subprocess` module to access it. https://docs.python.org/2/library/subprocess.html – Patrick Haugh Sep 22 '16 at 15:56
  • Please read [What topics can I ask about here?](http://stackoverflow.com/help/on-topic) and [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). Your question is very broad, and not a specific programming problem. Also, you provide none of your research results, some searching and researching will give you more insights. – Martin Nyolt Sep 22 '16 at 16:10
  • I will flag this as too broad. Is it immoral or a bad example if I answer a question which I am going to flag? – Martin Nyolt Sep 22 '16 at 16:11
  • @PatrickHaugh It seems that `subprocess.call` can not keep previous status, including variables. – Zehui Lin Sep 22 '16 at 16:12
  • @MartinNyolt I edit the problem to show what I have done. Perhaps you may think this a broad question, but it does stuck me. It's OK that you don't tell me the solution, but please tell me where I can get answered. Other people who asked a broad question would also his answer, and will not bother you on stackoverflow. – Zehui Lin Sep 22 '16 at 16:30

1 Answers1

1

As your question is very broad (interpret Lua or Python, in any language), I can only give you some hints.

If you write in C or C++, you can use the Lua library directly. It probably allows you to execute Lua statements, make values in C visible to the Lua code, make C function available to the Lua code, and access values of the Lua code.

If you write in Java, you may either write a JNI wrapper for the Lua library, or use another Lua implementation. See how can I embed lua in java?.

For other languages, you essentially have the same options: either use (if available) some other implementation in your favourite language, or find a way how you can access C library functions from your language. The latter is possible for most relevant programming languages.

For Python, the situation is similar. See, for example, Java Python Integration and Integrating Python With Other Languages.

Community
  • 1
  • 1
Martin Nyolt
  • 4,463
  • 3
  • 28
  • 36
  • I know why you think the question is broad now. I hope I can achieve this by creating subprocess and make some system calls, which are irrelevant with the bridge between Lua and the language I use. – Zehui Lin Sep 22 '16 at 16:33
  • I have edited the problem so that it would not look so broad. – Zehui Lin Sep 22 '16 at 16:56
  • How does this answer not help you? I have outlined several choices for your problem and linked to a related question. All these solutions work for your problem of keeping the state. – Martin Nyolt Sep 23 '16 at 07:34