简体   繁体   English

程序如何让Lua(或python)解释器执行程序提供的语句?

[英]How can programs make Lua(or python) interpreter execute statements provided by the program?

I am using Cent OS 7.我正在使用 Cent OS 7。

I want to write a program(written in Java or other languages) that can interact with Lua interpreter.我想编写一个可以与 Lua 解释器交互的程序(用 Java 或其他语言编写)。 I hope that my program can feed statements to Lua interpreter, and get executed real-time, while previous variables are still available.我希望我的程序可以将语句提供给 Lua 解释器,并实时执行,而以前的变量仍然可用。

For example, my program feeds a = 4; print(a);例如,我的程序输入a = 4; print(a); a = 4; print(a); to Lua interpreter 4 is printed on the screen.到 Lua 解释器4打印在屏幕上。 Then the program does other work.然后程序执行其他工作。 Later it feeds n = 0; for i=1,4 do n = n + i; end; print(n);稍后它提供n = 0; for i=1,4 do n = n + i; end; print(n); n = 0; for i=1,4 do n = n + i; end; print(n); to the interpreter, and 10 is printed on the screen.到解释器,屏幕上会打印10

Note: All I want is that Lua interpreter execute the statements when my program feeds it one, while keeping its previous status.注意:我想要的只是 Lua 解释器在我的程序提供一个语句时执行语句,同时保持其先前的状态。 My program does not need to access variables in Lua interpreter.我的程序不需要访问 Lua 解释器中的变量。

I tried calling Lua interpreter separately, but it doesn't work as expected.我尝试单独调用 Lua 解释器,但它没有按预期工作。 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?比如只是创建子进程和进行系统调用?

As your question is very broad (interpret Lua or Python, in any language), I can only give you some hints.由于您的问题非常广泛(用任何语言解释 Lua 或 Python),我只能给您一些提示。

If you write in C or C++, you can use the Lua library directly.如果用 C 或 C++ 编写,则可以直接使用 Lua 库。 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.它可能允许您执行 Lua 语句,使 C 中的值对 Lua 代码可见,使 C 函数对 Lua 代码可用,以及访问 Lua 代码的值。

If you write in Java, you may either write a JNI wrapper for the Lua library, or use another Lua implementation.如果您使用 Java 编写,您可以为 Lua 库编写 JNI 包装器,或者使用另一个 Lua 实现。 See how can I embed lua in java?看看我如何在 java 中嵌入 lua? . .

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.对于其他语言,您基本上有相同的选择:要么使用(如果可用)您喜欢的语言中的其他一些实现,要么找到一种方法来从您的语言访问 C 库函数。 The latter is possible for most relevant programming languages.后者适用于大多数相关的编程语言。

For Python, the situation is similar.对于 Python,情况类似。 See, for example, Java Python Integration and Integrating Python With Other Languages .例如,请参阅Java Python 集成将 Python 与其他语言集成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM