小编Sam*_*lan的帖子

Lua语法错误的描述性错误消息

我有一个Lua解释器,每当我在代码中出现语法错误时,返回的错误消息就是简单的attempted to call a string value,而不是有意义的错误消息.例如,如果我运行这个lua代码:

for a= 1,10
   print(a)
end
Run Code Online (Sandbox Code Playgroud)

它不会返回有意义'do' expected near 'print'的行号,而只会返回错误attempted to call a string value.

我的C++代码如下:

void LuaInterpreter::run(std::string script) {
    luaL_openlibs(m_mainState);

    // Adds all functions for calling in lua code
    addFunctions(m_mainState);

    // Loading the script string into lua
    luaL_loadstring(m_mainState, script.c_str());

    // Calls the script
    int error =lua_pcall(m_mainState, 0, 0, 0);
    if (error) {
        std::cout << lua_tostring(m_mainState, -1) << std::endl;
        lua_pop(m_mainState, 1);
    }
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

c++ error-handling lua

-1
推荐指数
1
解决办法
444
查看次数

标签 统计

c++ ×1

error-handling ×1

lua ×1