More work on command parser

This commit is contained in:
2021-02-02 16:29:07 -05:00
parent df3ec8ab99
commit 0721d29c72
10 changed files with 239 additions and 106 deletions

View File

@@ -43,8 +43,8 @@ static void l_message(const char *msg)
}
static void doexp(lua_State *L, const std::string &exp)
{
void TextGame::do_lua(const std::string &exp) {
lua_State *L = viewer_.get_lua_state();
int status = luaL_loadbuffer(L, exp.c_str(), exp.size(), "=stdin");
assert(status == LUA_OK);
globalL = L;
@@ -76,18 +76,66 @@ static void doexp(lua_State *L, const std::string &exp)
}
}
void TextGame::do_view_command(const StringVec &cmd) {
if (cmd.size() != 1) {
std::cerr << "v command (view) takes no arguments" << std::endl;
return;
}
viewer_.update_view_map();
for (auto viewpair : viewer_.get_view_map()) {
int64_t id = viewpair.first;
AnimView &view = viewpair.second;
std::cerr << id << ": " << view.get_graphic() << " " << view.get_xyz() << std::endl;
}
}
void TextGame::do_menu_command(const StringVec &cmd) {
int64_t id;
if (cmd.size() == 1) {
id = 1;
} else if (cmd.size() == 2) {
id = util::strtoint(cmd[1], -1);
} else {
std::cerr << "m command (menu) expects a tangible ID or defaults to 1" << std::endl;
return;
}
std::cerr << "Menu command (id " << id << ") not implemented yet." << std::endl;
}
void TextGame::do_choose_command(const StringVec &cmd) {
int64_t index;
if (cmd.size() == 2) {
index = util::strtoint(cmd[1], -1);
} else {
std::cerr << "c command (choose) expects a menu line number" << std::endl;
return;
}
std::cerr << "Choose command (index " << index << ") not implemented yet." << std::endl;
}
void TextGame::do_command(const StringVec &words) {
switch (words[0][0]) {
case 'v': do_view_command(words); break;
case 'm': do_menu_command(words); break;
case 'c': do_choose_command(words); break;
default:
std::cerr << "Unknown command: " << words[0] << std::endl;
}
}
void TextGame::run()
{
LuaConsole console;
console_.clear();
while (true) {
console.add_stdin();
int action = console.action();
if (action == LuaConsole::DO_COMMAND) {
doexp(viewer_.get_lua_state(), console.expression());
} else if (action == LuaConsole::DO_SLASH) {
std::cerr << "Slash commands not supported yet." << std::endl;
console_.add_stdin();
int action = console_.action();
if (action == LuaConsole::DO_LUA) {
do_lua(console_.lua_expression());
} else if (action == LuaConsole::DO_COMMAND) {
do_command(console_.words());
} else if (action == LuaConsole::DO_SYNTAX) {
std::cerr << console.syntax() << std::endl;
std::cerr << console_.syntax() << std::endl;
}
}
}