【发布时间】:2016-01-23 01:22:10
【问题描述】:
我不确定在这里问这个问题是否合适,但我会尽量清楚地解释我的问题。
我有一台机器要控制。基本的操作功能(如移动、开门、等待命令等)、接口等都是用C++编写的,我们需要用lua编写一些服务脚本。服务脚本的伪代码如下:
* define the service name and give some description
* declare the task address
* declare the input and output class name
* list the input plugin files
* define a "run" function
-- publish the machine state
-- initialize the machine
-- wait for commands
-- call the "service" function
-- wait for stop command
* define a "service" function
-- set the service state to be true
-- move the car to destination
-- open the door for a while
-- close the door
* after the service is finished, send a "finish" flag to the center handler.
在这个伪代码中,我们有2个函数,以“--”开头的行通常是用C++编写的基本操作函数或其他lua插件文件中定义的函数。
我之前写过非常简单的 C++ 代码和 Lua 脚本并将它们连接起来。但它们就像将数字表从 C++ 传递给 lua,使用 lua 中的函数计算总和/阶乘/平方...然后将结果传回 C++ 并打印出来。在这样的练习中,我只是使用了诸如 lua_State *L; 之类的函数。 L = luaL_newstate(); luaL_openlibs(); luaL_loadfile(); lua_pcall(); lua_tostring(); lua_tonumber(); ......
但是现在我需要处理这么复杂的 lua 脚本和 C++ 代码系统。我认为仅仅做我以前做的事情是不够的......我想知道是否有一种特定的方法来解析 lua 脚本,获取每个组件的值(任务地址、函数等)并将它们保存在C++ 代码,那么我将提取的组件连接到现有的 C++ 函数可能会更容易......
或者这是否可能:只需从 lua 以表的形式返回所有内容(包括值、函数等),然后我编写 C++ 代码读取表并保存值,然后连接将项目提取到现有的 C++ 函数中......
我不确定我是否清楚地提出了我的问题...我是 Lua 的新手,我认为 Lua 有很多我还没有发现的伟大之处..我知道人们说 Lua 是一盏灯和快速的语言,我想充分利用它。如果您理解我的问题并有更好的想法来解决我的问题,我将非常乐意学习!
【问题讨论】: