Running OK with luaconsole.cpp

This commit is contained in:
2021-01-23 16:31:16 -05:00
parent b897c10506
commit 37a05fe1ac
4 changed files with 30 additions and 37 deletions

View File

@@ -55,51 +55,45 @@ static int report(lua_State *L, int status)
return status;
}
static int docall(lua_State *L, int narg, int nret)
static void doexp(lua_State *L, const std::string &exp)
{
int status = luaL_loadbuffer(L, exp.c_str(), exp.size(), "=stdin");
assert(status == LUA_OK);
globalL = L;
signal(SIGINT, laction);
int status = traceback_pcall(L, narg, nret);
status = traceback_pcall(L, 0, LUA_MULTRET);
signal(SIGINT, SIG_DFL);
/* force a complete garbage collection in case of errors */
if (status != LUA_OK)
if (status != LUA_OK) {
report(L, status);
lua_gc(L, LUA_GCCOLLECT, 0);
return status;
}
if (status == LUA_OK && lua_gettop(L) > 0) {
lua_getglobal(L, "pprint");
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_getglobal(L, "print");
}
lua_insert(L, 1);
if (lua_pcall(L, lua_gettop(L) - 1, 0, 0) != 0) {
l_message(
lua_pushfstring(L, "error calling 'print' (%s)",
lua_tostring(L, -1)));
}
}
}
static void dotty(lua_State *L)
void TextGame::run()
{
LuaConsole console;
int status;
while (true) {
console.add_stdin();
switch (console.state()) {
case LuaConsole::STATE_INCOMPLETE:
console.add_stdin();
break;
case LuaConsole::STATE_COMPLETE: {
const std::string &exp = console.expression();
status = luaL_loadbuffer(L, exp.c_str(), exp.size(), "=stdin");
assert(status == LUA_OK);
status = docall(L, 0, LUA_MULTRET);
report(L, status);
if (status == LUA_OK && lua_gettop(L) > 0) {
// any result to print?
lua_getglobal(L, "pprint");
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_getglobal(L, "print");
}
lua_insert(L, 1);
if (lua_pcall(L, lua_gettop(L) - 1, 0, 0) != 0)
{
l_message(
lua_pushfstring(L, "error calling 'print' (%s)",
lua_tostring(L, -1)));
}
}
case LuaConsole::STATE_COMPLETE:
doexp(viewer_.get_lua_state(), console.expression());
console.clear();
break;
}
case LuaConsole::STATE_SLASHCOMMAND:
std::cerr << "Slash commands not supported yet." << std::endl;
console.clear();
@@ -112,8 +106,3 @@ static void dotty(lua_State *L)
}
}
void TextGame::run()
{
dotty(viewer_.get_lua_state());
}