osb/source/utility/game_repl.cpp

54 lines
1.4 KiB
C++
Raw Normal View History

2023-06-20 04:33:09 +00:00
#include "StarRootLoader.hpp"
#include "StarRootLuaBindings.hpp"
#include "StarUtilityLuaBindings.hpp"
#include "StarRootLuaBindings.hpp"
using namespace Star;
int main(int argc, char** argv) {
RootLoader rootLoader({{}, {}, {}, LogLevel::Error, false, {}});
RootUPtr root;
OptionParser::Options options;
tie(root, options) = rootLoader.commandInitOrDie(argc, argv);
auto engine = LuaEngine::create(true);
auto context = engine->createContext();
context.setCallbacks("sb", LuaBindings::makeUtilityCallbacks());
context.setCallbacks("root", LuaBindings::makeRootCallbacks());
String code;
bool continuation = false;
while (!std::cin.eof()) {
auto getline = [](std::istream& stream) -> String {
std::string line;
std::getline(stream, line);
return String(std::move(line));
2023-06-20 04:33:09 +00:00
};
if (continuation) {
std::cout << ">> ";
std::cout.flush();
code += getline(std::cin);
code += '\n';
} else {
std::cout << "> ";
std::cout.flush();
code = getline(std::cin);
code += '\n';
}
try {
auto result = context.eval<LuaVariadic<LuaValue>>(code);
for (auto r : result)
2023-06-27 10:23:44 +00:00
coutf("{}\n", r);
2023-06-20 04:33:09 +00:00
continuation = false;
} catch (LuaIncompleteStatementException const&) {
continuation = true;
} catch (std::exception const& e) {
2023-06-27 10:23:44 +00:00
coutf("Error: {}\n", outputException(e, false));
2023-06-20 04:33:09 +00:00
continuation = false;
}
}
return 0;
}