How can programs make Lua(or python) interpreter execute statements provided by the program?
<p>I am using Cent OS 7.</p>

<p>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.</p>

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

<p>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.</p>

<p>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.</p>

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

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

<p>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.</p>

<p>If you write in Java, you may either write a JNI wrapper for the Lua library, or use another Lua implementation. See <a href="http://stackoverflow.com/q/2113432/1314743">how can I embed lua in java?</a>.</p>

<p>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.</p>

<p>For Python, the situation is similar. See, for example, <a href="http://stackoverflow.com/q/1119696/1314743">Java Python Integration</a> and <a href="https://wiki.python.org/moin/IntegratingPythonWithOtherLanguages" rel="nofollow">Integrating Python With Other Languages</a>.</p>

java python linux lua