Lua script to C++ code

14,106

Solution 1

Try LuaToCee.

Solution 2

Perhaps toLua could help.

Solution 3

From C#, something I've done is written code to convert LUA datastructures in JSON format. Then I can load the data via any JSON library, with all their bells and whistles. Its kind of a round-about solution but you only have to write the JSON Encoding code once.

Share:
14,106
Max
Author by

Max

Updated on June 04, 2022

Comments

  • Max
    Max almost 2 years

    I am writing C functions for Lua. I have many calls like lua_gettable, lua_touserdata, etc

    My C function may receive complex structures like table with tables as fields.

    It is hard for me to program stacked machine.

    Is there way to write Lua script that would be converted to C code.

    Or some other tools that may help me to code such C functions for lua scripts.

    Thanks.

    PS

    Here is example:-

    local data = {}
    data.x = {}
    data.x.y = 1
    myCfunc(data)
    

    int myCfunc(lua_State * L){
     lua_pushstring(L, "x");
     lua_gettable(L, 2);
     lua_pushstring(L, "y");
     lua_gettable(L, -2);
     double y = lua_tonumber(L, -1);
     lua_pop(L, 2);
    }
    

    instead of

    function myCfunc(data)
     y = data.x.y
    end
    

    My real code is much more complex and I am looking for some automated code generation that will help me.

  • Glenn McAllister
    Glenn McAllister over 13 years
    Err... that isn't the question he asked at all. He wants to know how to convert Lua code into C, not Lua tables into JSON objects.
  • Frank Schwieterman
    Frank Schwieterman over 13 years
    He states "My C function may receive complex structures like table with tables as fields." and that this is hard. Though the suggestion for simplifying the that usage is not a direct answer to his question, it might still be useful.
  • Max
    Max over 13 years
    JSON is too hacky. I can write C code but I believe LUA interpreter converts LUA script to C calls much more efficiently. So it is possible to get that done some how. LUA is not only stack machine. There must be some tools that help to code such systems.
  • Max
    Max over 13 years
    Thanks. But it is not what I am looking for. tolua will generate wrapper arround C classes when I wish to read complex lua strcust in simpler way.
  • Max
    Max over 13 years
    Thank You! That is what I asked!