slige/runtime/main.cpp

65 lines
1.9 KiB
C++
Raw Normal View History

2024-11-27 12:28:45 +00:00
#include "actions.hpp"
#include "json.hpp"
#include "rpc_server.hpp"
2024-12-11 02:11:00 +00:00
#include "vm.hpp"
#include "vm_provider.hpp"
2024-12-11 02:11:00 +00:00
#include <cstdint>
#include <format>
#include <fstream>
#include <string>
#include <vector>
2024-12-12 09:17:09 +00:00
bool print_stack_debug = false;
2024-12-11 02:11:00 +00:00
int execute_file_and_exit(std::string filename)
2024-11-08 11:22:42 +00:00
{
2024-12-11 02:11:00 +00:00
auto file = std::ifstream();
file.open(filename.c_str());
if (!file) {
std::cout << std::format("error: could not open file '{}'\n", filename);
return 1;
}
auto text = std::string(std::istreambuf_iterator<char> { file }, {});
auto parsed = sliger::json::parse_json(text);
if (not parsed.ok()) {
std::cout << std::format("error: {} at {}:{}\n", parsed.err().msg,
parsed.err().pos.line, parsed.err().pos.col);
return 1;
}
auto program = std::vector<uint32_t>();
for (auto& v : parsed.val()->as<sliger::json::Array>().values) {
program.push_back(
static_cast<uint32_t>(v->as<sliger::json::Number>().value));
}
auto vm = sliger::VM(program,
{
.flame_graph = false,
.code_coverage = false,
2024-12-12 09:17:09 +00:00
.print_stack_debug = print_stack_debug,
2024-12-11 02:11:00 +00:00
});
vm.run_until_done();
return 0;
}
int main(int argc, char** argv)
{
if (argc >= 3 && std::string(argv[1]) == "run") {
return execute_file_and_exit(argv[2]);
}
auto state = sliger::rpc::vm_provider::VmProvider();
2024-11-26 12:04:40 +00:00
auto rpc = sliger::rpc::RpcServer(
[&](std::unique_ptr<sliger::json::Value> req,
2024-11-26 12:04:40 +00:00
std::unique_ptr<sliger::rpc::BufferedWriter> writer) {
2024-11-27 12:28:45 +00:00
auto action = sliger::rpc::action::action_from_json(std::move(req));
2024-12-11 02:11:00 +00:00
action->perform_action(std::move(writer), state);
});
2024-12-12 09:44:27 +00:00
2024-12-12 10:33:59 +00:00
std::cout << "binding on 127.0.0.1:13370\n";
2024-12-12 09:44:27 +00:00
auto res = rpc.listen();
if (!res.is_ok()) {
std::cout << res.err().msg << "\n";
}
2024-11-08 11:22:42 +00:00
}