Files
integration/luprex/cpp/core/luaconsole.cpp

109 lines
2.8 KiB
C++
Raw Normal View History

#include "wrap-string.hpp"
#include "wrap-vector.hpp"
#include "eng-malloc.hpp"
#include "luastack.hpp"
2021-01-23 16:10:29 -05:00
#include "luaconsole.hpp"
2021-02-10 16:22:24 -05:00
#include "util.hpp"
#include <cstring>
2021-10-07 14:58:20 -04:00
#include <iostream>
2021-02-10 16:22:24 -05:00
2021-01-23 16:10:29 -05:00
LuaConsole::LuaConsole() {
2021-11-04 14:49:25 -04:00
clear_raw_input();
2021-01-23 16:10:29 -05:00
}
LuaConsole::~LuaConsole() {
}
static LuaConsole::StringVec split_words(const eng::string &raw) {
2021-11-04 14:49:25 -04:00
LuaConsole::StringVec result;
eng::string acc;
2021-11-04 14:49:25 -04:00
for (char c : raw) {
2021-10-07 14:58:20 -04:00
if ((c == ' ')||(c == '\n')||(c == '\r')) {
2021-02-02 16:29:07 -05:00
if (!acc.empty()) {
2021-11-04 14:49:25 -04:00
result.push_back(acc);
2021-02-02 16:29:07 -05:00
acc = "";
}
} else {
acc += c;
}
}
if (!acc.empty()) {
2021-11-04 14:49:25 -04:00
result.push_back(acc);
2021-02-02 16:29:07 -05:00
}
2021-11-04 14:49:25 -04:00
return result;
2021-02-02 16:29:07 -05:00
}
2021-11-04 14:49:25 -04:00
LuaConsole::StringVec LuaConsole::get_command() {
StringVec result = words_;
words_.clear();
2021-10-07 14:58:20 -04:00
return result;
}
2021-11-04 14:49:25 -04:00
void LuaConsole::clear_raw_input() {
raw_input_ = "";
lines_ = 0;
prompt_ = "> ";
}
void LuaConsole::add(eng::string line) {
2021-01-23 16:10:29 -05:00
for (int i = 0; i < int(line.size()); i++) {
if (line[i] == '\n') line[i] = ' ';
}
2021-02-02 16:29:07 -05:00
raw_input_ += line;
raw_input_ += '\n';
2021-01-23 16:10:29 -05:00
lines_ += 1;
2021-11-04 14:49:25 -04:00
prompt_ = ">> ";
2021-11-16 12:20:11 -05:00
words_.clear();
2021-01-23 16:10:29 -05:00
// Try to interpret it as a slash-command.
if ((lines_ == 1)&&(raw_input_[0] == '/')) {
words_ = split_words(raw_input_.substr(1));
if ((words_.size() == 1) && (sv::valid_int64(words_[0]))) {
eng::string num = words_[0];
words_.clear();
words_.push_back("choose");
words_.push_back(num);
}
clear_raw_input();
return;
2021-01-23 16:10:29 -05:00
}
2021-02-07 15:35:31 -05:00
// Translate lua expression, deal with initial prefix.
eng::string partial;
eng::string lua_mode;
if (sv::has_prefix(raw_input_, "?=")) {
lua_mode = "luaprobe";
partial = eng::string("return ") + raw_input_.substr(2);
} else if (sv::has_prefix(raw_input_, "?")) {
lua_mode = "luaprobe";
partial = raw_input_.substr(1);
} else if (sv::has_prefix(raw_input_, "=")) {
lua_mode = "luainvoke";
partial = eng::string("return ") + raw_input_.substr(1);
2021-02-02 16:29:07 -05:00
} else {
lua_mode = "luainvoke";
2021-02-07 15:35:31 -05:00
partial = raw_input_;
2021-02-02 16:29:07 -05:00
}
2021-01-23 16:10:29 -05:00
2021-02-10 16:22:24 -05:00
// Try to parse the lua expression
2021-01-23 16:10:29 -05:00
{
LuaVar result;
LuaExtStack LS(lua_state_, result);
eng::string errmsg = LS.load(result, partial, "stdin");
if (errmsg.empty()) {
words_.push_back(lua_mode);
words_.emplace_back(sv::rtrim(partial));
clear_raw_input();
} else if (errmsg.find("<eof>") != std::string::npos) {
// We have an incomplete expression.
// Do nothing, just let the user type more stuff.
} else {
words_.push_back("syntax");
words_.push_back(errmsg);
2021-11-04 14:49:25 -04:00
clear_raw_input();
2021-01-23 16:10:29 -05:00
}
}
}
2021-10-07 14:58:20 -04:00