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

94 lines
2.2 KiB
C++
Raw Normal View History

2021-01-23 16:10:29 -05:00
#include <string.h>
#include "luaconsole.hpp"
2021-02-10 16:22:24 -05:00
#include "util.hpp"
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-02-28 14:24:59 -05:00
lua_state_ = luaL_newstate();
2021-11-04 14:49:25 -04:00
clear_raw_input();
2021-01-23 16:10:29 -05:00
}
LuaConsole::~LuaConsole() {
lua_close(lua_state_);
}
2021-11-04 14:49:25 -04:00
static LuaConsole::StringVec split_words(const std::string &raw) {
LuaConsole::StringVec result;
2021-02-02 16:29:07 -05:00
std::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_ = "> ";
}
2021-01-23 16:10:29 -05:00
void LuaConsole::add(std::string line) {
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-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));
clear_raw_input();
return;
2021-01-23 16:10:29 -05:00
}
2021-02-07 15:35:31 -05:00
2021-02-10 16:22:24 -05:00
// Translate lua expression with leading '=' to 'return'
2021-02-02 16:29:07 -05:00
std::string partial;
2021-02-07 15:35:31 -05:00
if (raw_input_[0] == '=') {
partial = std::string("return ") + raw_input_.substr(1);
2021-02-02 16:29:07 -05:00
} else {
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
int top = lua_gettop(lua_state_);
2021-02-02 16:29:07 -05:00
int status = luaL_loadbuffer(lua_state_, partial.c_str(), partial.size(), "=stdin");
2021-01-23 16:10:29 -05:00
if (status == LUA_ERRSYNTAX)
{
2021-07-12 00:37:07 -04:00
const char *eof = "<eof>";
int leof = strlen(eof);
2021-01-23 16:10:29 -05:00
size_t lmsg;
const char *msg = lua_tolstring(lua_state_, -1, &lmsg);
2021-07-12 00:37:07 -04:00
const char *tp = msg + lmsg - leof;
if (strstr(msg, eof) != tp) {
words_.push_back("syntax");
words_.push_back(msg);
2021-11-04 14:49:25 -04:00
clear_raw_input();
2021-01-23 16:10:29 -05:00
}
} else {
words_.push_back("lua");
words_.push_back(partial);
2021-11-04 14:49:25 -04:00
clear_raw_input();
2021-01-23 16:10:29 -05:00
}
lua_settop(lua_state_, top);
}
2021-10-07 14:58:20 -04:00