0
votes

I am not sure whether it is suitable to ask this question here, but I will try to explain my question as clearly as possible.

I have a machine to control. Basic operation functions (e.g. move, open doors, wait for commands, etc.), interfaces, etc are all writen in C++, and we need to write some service scripts in lua. The pseudo codes of service scripts are like:

 * 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.

In this pseudo code, we have 2 functions, and lines started with "--" are usually basic operation functions written in C++ or functions defined in other lua plugin files.

I have written very simple C++ code and Lua script before and connected them. But they are just like pass a table of numbers from C++ to lua, use functions in lua to calculate the sum/factorial/sqaure... and then pass the result back to C++ and print out. In such exercises, I just used functions such as lua_State *L; L = luaL_newstate(); luaL_openlibs(); luaL_loadfile(); lua_pcall(); lua_tostring(); lua_tonumber(); ......

But now I need to handle such a complicated lua script and C++ code system. I think simply doing what I used to do is not enough....I was wondering whether there is a specific way to parse the lua script, get value of each component (task address, function, etc..) and save them in C++ code, then probably it will be easier for me to connect the extracted component to existing C++ functions....

Or is this possible: just return everything (including values, functions, etc...) from lua in the form of table, then I write a C++ code to read the table and save the values, and then I connect the extracted items to existing C++ functions....

I am not sure whether I make my question clearly...I am newbie in Lua and I think there is a large deal of greatness of Lua that I haven't found..I know people say that Lua is a light and fast language and I want to make the most of it. If you understand my question and have better ideas to solve my problem, I will be more than happy to learn!

2

2 Answers

0
votes

In this case I would not just stick to Lua, but rather prepare some Domain Specific Language. In example you provided there is informal syntax of your language that will need formal record.

You will need to define tokens (atoms of your language) and then grammar rules (how to build services out of those tokens). Something that will look more or less like:

%tokens {
    NUMBER = [0-9]+
    NAME = [A-Z]+
    STRING = \"[^\"]\"
}

%rules {
    script: service
          | service script

    service: "def" NAME "doc" STRING "address" NUMBER io body

    io: //and much more here
}

There is lots of tools that can convert that formal definition into parser, or interpreter. As a C++ developer you might like Bison/Flex. This is Compiler Compiler written in C, it compiles files that contains mixed C/C++ codes with definition of tokens and rules. If you do not need to write your parser in C++ then I will recommend you to use ANTLR which is IMO simpler to use.

0
votes

You should look into the ability of the lua API to receive C functions directly into the environment when they are pushed as function pointers:

lua_pushcfunction(L, func);

where func has signature:

int func(lua_State * L);

Using this mechanism, you can make an API to control the doors and such from the scripts, and it will be very natural for the lua scripter to control it. That will be much better than having lua compute a bunch of tables that encode what should happen, I think.